Skip to main content

Build an image from a DockerFile and push it to DockerHub.

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.