This tutorial explains how to manually build and push an image to Docker Hub. As an example, we will build a Docker image for a simple Node.js application that has basic CI tests as well as code coverage reports. The Dockerfile is a part of the application repository on Github.
You can also use your own Dockerfile to follow the tutorial.
Before you start, you should be familiar with the following concepts:
Even though executing this workflow manually is a great way to start and understand how Docker works, we recommend automating it with a Continuous Integration platform to achieve frictionless software development. The advantages of automation are as follows:
- Your CI workflow will execute every time you push a new commit or a pull request is opened for the repository containing your application source code and Dockerfile. This means your code will always be tested immediately and you'll find bugs as soon as they're introduced.
- You can configure your CI to trigger a multi-stage workflow which deploys the application into successive environments like Test, Staging, etc, and runs several test suites along the way. This will take you closer to Continuous Delivery.
If you're interested in automation, please read our tutorial on Automating Docker build and Push to Docker Hub.
That said, let's get this tutorial started!
Follow the steps below in order to build and push your Docker image.
Step 1: Prep your machine
- Create a GitHub repository that will hold the code to build the image. If you do not have your own Dockerfile or application, please feel free to clone our sample here: https://github.com/devops-recipes/node_app
- Create an account on Docker Hub.
- Install Docker on your local machine. [Refer to the Docker Getting Started Guide]
- Login to Docker Hub:
docker loginwith your credentials. [Refer to the docker login docs for a complete reference]
docker login --username=yourhubusername --password=yourpassword
Step 2: Build and Push image
- Your Dockerfile will look something like this:
FROM readytalk/nodejs # Add our configuration files and scripts WORKDIR /app ADD . /app RUN npm install EXPOSE 80 ENTRYPOINT ["/nodejs/bin/npm", "start"]
- Build your image by executing the
DOCKER_ACCis the name of your account
$DOCKER_REPOis your image name and
$IMG_TAGis your tag
docker build -t $DOCKER_ACC/$DOCKER_REPO:$IMG_TAG .
- Now, you can push this image to your hub by executing the
sudo docker push $DOCKER_ACC/$DOCKER_REPO:$IMG_TAG