Image: o_m/Shutterstock

If you’re just starting your journey with containers and Docker, you’re probably doing some of your work within your on-prem data center, which is one of the many places to venture into this new containerized world. At some point, however, you’re going to venture out of that data center and into the expanded world of cloud-hosted containers.

Before then, you’re going to want to know how to build your own Docker image and push it to Docker Hub. Why? Because you might want to make that image available to your teammates or even the rest of the world.

But how do you do that?

I’m going to show you.

It’s quite simple, you just have to know how to build a Dockerfile.

SEE: Kubernetes: A cheat sheet (free PDF) (TechRepublic)

What you’ll need

To make this work you’re going to need the following:

  • A Docker Hub account.
  • A running instance of the Docker engine.
  • A user that belongs to the docker group (so you can issue the docker command without using sudo).

That’s it. Let’s get to work.

How to build your image

The image we’re going to build is based on python:3.8-buster and will include the following libraries:

  • numpy 1.14.3
  • matplotlib 2.2.2
  • seaborn 0.8.1

It’s very basic, but it’ll illustrate the task just fine. First, let’s create a directory to work in with the command:

mkdir ~/DOCKER

Change into that directory with:


Now, we’ll create our Dockerfile with the command:

nano Dockerfile

In that file, paste the following:

FROM python:3.8-buster
RUN pip install --upgrade pip
COPY requirements.txt .
RUN pip install -r requirements.txt
COPY src/ .
CMD [ "python", ""]

Save and close the file.

The above command will pull down the python:3.8-buster image, use pip to upgrade it, and then read the contents of a new file (requirements.txt) and install everything listed in that file. To create the requirements.txt file, issue the command:

nano requirements.txt

In that file, paste the following:


Save and close the file.

Create a new sub-directory with:

mkdir src

How to log into your Docker Hub account

We have to log into our Docker Hub account to push the new image. To successfully log into Docker Hub from the command line, you must first create an access token. Log in to Docker Hub and click your profile image. From the popup menu, select Account Settings. On the resulting page, click Security in the left navigation and then click New Access Token (Figure A).

Figure A

Creating a new access token in Docker Hub.

Once you’ve generated the access token, copy it to your clipboard. Go back to the terminal window and issue the command:

docker login -u NAME

Where NAME is your Docker Hub username. You will be prompted for your Docker Hub password, where you’ll use the access token you just generated.

How to build your image

It’s time to build our image. We’re going to name the image trtest. To do this, issue the command:

docker build -t trtest .

When the build completes, you’ll have a new image, named trtest.

How to tag and push the image

Finally, we’re going to tag our new image and then push it to Docker Hub. First tag the image with :latest using the command:

docker image tag trtest USER/trtest:latest

Where USER is your Docker Hub username.

Now that the image is tagged, we can push it to Docker Hub with:

docker image push USER/trtest:latest

Where USER is your Docker Hub username.

When the push completes, you should find the trtest:latest image in your Docker Hub repository.

And that’s all there is to building a Docker image and pushing it to your Docker Hub repository.

Subscribe to TechRepublic’s How To Make Tech Work on YouTube for all the latest tech advice for business pros from Jack Wallen.