Push and Pull docker image

» Prerequisites

Before getting started, make sure you have Docker installed on your machine. You can download and install Docker by following the instructions provided on the official Docker website.

» What is Docker Hub?

  1. Docker Hub is a cloud-based registry provided by Docker. It serves as a central repository for Docker images, allowing developers to store, share, and retrieve container images easily. Docker Hub offers both public and private repositories, catering to the needs of individual developers, teams, and organizations.

  2. Public repositories on Docker Hub are accessible to anyone, and they host a vast collection of Docker images contributed by the community. These images cover a wide range of applications, frameworks, and tools, enabling developers to quickly access and utilize pre-built solutions.

  3. Private repositories, on the other hand, provide a secure and controlled environment for storing Docker images. They are ideal for teams and organizations working on proprietary projects or sensitive applications that require restricted access.

  4. Docker Hub simplifies the process of collaborating on containerized applications.

» Pushing a Docker Image

Pushing Docker images enables you to share your containers with others or store them in a remote repository for future use. Follow these steps to push your Docker image:-

  1. Build your Docker Image: Use a Dockerfile or any other method to build your image locally on your machine. (See the previous post for this.)

  2. Sign Up for Docker Hub: Visit hub.docker.com, and sign up and create your account. Provide the required information, such as your username, password, and email address. Complete the registration process by verifying your email.

Docker SignUp

  1. Sign in to Docker Hub: Once you have created your account, go back to the Docker Hub website. Click on the “Sign In” button and enter your Docker Hub username and password. This will authenticate you and allow you to interact with Docker Hub from the command line.

Docker SignIn

  1. Check for a docker Image: Open the terminal and run the command
docker images

This will show you all the images that you have created.

  1. Log in to a Docker in terminal: Sign in to the Docker in terminal using the command:
docker login

You will see the message login succeeded.

  1. Tag our Docker image: Before pushing, it’s essential to tag your image with the appropriate Docker Hub repository information. Use the following command to tag your image:
docker tag <image-name> <docker-hub-username>/<repository-name>:<tag>

Replace image-name with the name of your local Docker image, docker-hub-username with your Docker Hub username, repository-name with the name of the repository you want to push the image to, and tag with an optional tag for versioning (e.g: “latest”).

Now when you run the command

docker images

you will see another repository with the name: docker-hub-username/repository-name.

  1. Push the Docker image: We’re ready to push our image to Docker Hub. Use the following command:
docker push <docker-hub-username>/<repository-name>:<tag>

Replace docker-hub-username with your Docker Hub username, repository-name with the name of the repository you want to push the image to, and tag with an optional tag for versioning (e.g: “latest”).

Docker will upload your image to Docker Hub. This might take a few moments depending on the image size and your internet speed.

Now when you visit hub.docker.com, and refresh it, you will one repository.

» Pull Docker Image

In the repository click on the public view you will have a docker pull command.

As you already have this image stored in your local machine, it won’t do anything, so first delete the image you pushed with the command:

docker rmi <docker-hub-username>/<repository-name>

When you run the command the following you will see you don’t have a repository docker-hub-username/repository-name anymore.

docker images

Pull that image back with the command you copied from docker hub website:

docker pull <docker-hub-username>/<repository-name>

Docker will download the image from Docker Hub and store it locally on your machine, and now you can run this image.

» Run Docker Image

To run a Docker container with port mapping, you can use the docker run command with the -p or –publish option. The -p option allows you to specify the mapping between the host and container ports.

Here’s the basic syntax for the command:

docker run -p <host_port>:<container_port> <image_name>

host_port: This is the port number on the host machine that you want to map to the container. container_port: This is the port number inside the container that you want to expose. image_name: This is the name or ID of the Docker image you want to run.

For example,

docker run -p 8080:80 <docker-hub-username>/<repository-name>

This command would start a container based on the docker-hub-username/repository-name image, and the application running inside the container on port 80 would be accessible on the host machine at http://localhost:8080.


And That’s it! You’ve successfully learned how to push and pull Docker images to and from Docker.

Thank You and Happy coding in containers!😊