Build a Docker Image and Push It To Docker Hub

- By Manisha Sahasrabudhe on May 21, 2018

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!


Step-by-step instructions

Follow the steps below in order to build and push your Docker image.


Step 1: Prep your machine

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
ADD . /app
RUN npm install

ENTRYPOINT ["/nodejs/bin/npm", "start"]
  • Build your image by executing the docker build command. DOCKER_ACC is the name of your account $DOCKER_REPO is your image name and $IMG_TAG is your tag
docker build -t $DOCKER_ACC/$DOCKER_REPO:$IMG_TAG .
  • Now, you can push this image to your hub by executing the docker push command.
sudo docker push $DOCKER_ACC/$DOCKER_REPO:$IMG_TAG


Topics: Docker, continuous integration (CI), Docker Hub