Docker: Image Push and Pull

Docker: Image Push and Pull

Learn how to push and pull Docker images with this step-by-step guide

Table of contents

No heading

No headings in the article.

Introduction

Docker is a tool that helps developers package and run applications in a consistent way. One of the key features of Docker is the ability to share images with others. This can be done by pushing images to a registry, such as Docker Hub.

To push an image to a registry, you can use the following command:

docker push <image_name>

For example, to push the image named “my-app” to Docker Hub, you would use the following command:

docker push my-app

Once an image has been pushed to a registry, it can be pulled by other users. To pull an image, you can use the following command:

docker pull <image_name>

For example, to pull the image named “my-app” from Docker Hub, you would use the following command:

docker pull my-app

Here is an example of how to push and pull a Docker image:

1. Create a Dockerfile

The first step is to create a Dockerfile. A Dockerfile is a text file that describes how to build a Docker image. The following is an example of a Dockerfile for a simple web application:

FROM nginx
COPY index.html /usr/share/nginx/html

This Dockerfile tells Docker to build an image based on the nginx image and to copy the index.html file to the nginx web root.

2. Build the image

Once you have created a Dockerfile, you can build the image using the following command:

docker build -t my-app .

The -t flag tells Docker to tag the image with the name “my-app”. The . flag tells Docker to build the image from the current directory.

3. Push the image

Once you have built the image, you can push it to a registry using the following command:

docker push my-app

This will push the image to the default registry, which is Docker Hub.

4. Pull the image

To pull the image from Docker Hub, you can use the following command:

docker pull my-app

This will download the image from Docker Hub and store it on your local machine.

5. Run the image

Once you have pulled the image, you can run it using the following command:

docker run -d -p 80:80 my-app

This will run the image as a container and expose port 80 on your local machine.

You can now access the web application at http://localhost:80.

Code and Images

Here is the code for the Dockerfile and index.html file used in this example:

Dockerfile

FROM nginx
COPY index.html /usr/share/nginx/html

index.html

<!DOCTYPE html>
<html>
<head>
<title>My App</title>
</head>
<body>
<h1>Hello World!</h1>
</body>
</html>

I hope this blog post has helped you understand how to push and pull Docker images. Happy coding!

References:

https://docs.docker.com/engine/reference/commandline/push/

https://docs.docker.com/engine/reference/commandline/pull/