Skip to content

Docker - Public Repositories

Docker Hub allows you to share your custom Docker images with the world by pushing them to public repositories. This is useful for open-source projects or for distributing tools and applications.

This chapter outlines the steps to build a local image, tag it appropriately for Docker Hub, and push it to a public repository associated with your Docker ID.

Let’s assume you have already built a local image, for example, myimage:0.1 (created in the “Building Docker Files” chapter).

# Verify the local image exists
docker images

Step 1: Log in to Docker Hub via the CLI

You need to authenticate with your Docker Hub credentials before you can push images.

docker login

Enter your Docker ID (username) and password when prompted. A successful login message will be displayed.

Step 2: Tag Your Local Image

Before pushing, you must tag your local image with a name that includes your Docker Hub username and the desired repository name. The format is your-dockerhub-username/repository-name:tag.

Let’s tag our local myimage:0.1 to be pushed to a repository named demorep under your username (replace yourdockerhubusername with your actual Docker ID).

# docker tag <source_image>:<source_tag> <hub_user>/<repo_name>:<dest_tag>
docker tag myimage:0.1 yourdockerhubusername/demorep:1.0

This command creates a new tag (yourdockerhubusername/demorep:1.0) that points to the same image ID as myimage:0.1. It doesn’t duplicate the image data.

You can verify the new tag exists using docker images.

Step 3: Push the Tagged Image (docker push)

Now, use the docker push command with the fully qualified tag name to upload the image layers to Docker Hub.

docker push yourdockerhubusername/demorep:1.0

Docker will display the progress as it pushes each layer. If the repository (demorep) doesn’t exist yet under your Docker Hub account, Docker Hub will typically create it automatically as a public repository during the first push.

Step 4: Verify on Docker Hub (Optional)

Log in to the Docker Hub website (https://hub.docker.com/). Navigate to your repositories, and you should find the demorep repository containing the 1.0 tag.

Now, anyone (including yourself on a different machine or after removing the local image) can pull and run your image from Docker Hub.

Step 5: Remove Local Images (Optional, for testing pull)

# Remove the specific tag pointing to the Docker Hub repo
docker rmi yourdockerhubusername/demorep:1.0
# Optionally remove the original local tag as well
# docker rmi myimage:0.1

Step 6: Pull the Image from Docker Hub

docker pull yourdockerhubusername/demorep:1.0

Docker will download the image from Docker Hub. You have now successfully built, pushed, and pulled a public Docker image.