Managing Docker Images
Pulling Docker Images
To pull a Docker image from a registry, you can use the docker pull
command:
docker pull ubuntu:22.04
This will pull the ubuntu:22.04
image from the Docker Hub registry.
Building Docker Images
You can build your own Docker images using a Dockerfile, which is a text file that contains instructions for building the image. Here's an example Dockerfile:
FROM ubuntu:22.04
RUN apt-get update && apt-get install -y nginx
COPY index.html /var/www/html/
EXPOSE 80
CMD ["nginx", "-g", "daemon off;"]
To build the image, run the following command:
docker build -t my-nginx-app .
This will create a new Docker image named my-nginx-app
.
Listing Docker Images
You can list all the Docker images on your system using the docker images
command:
docker images
This will display a table with information about each image, including the image name, tag, image ID, creation time, and size.
Tagging Docker Images
You can add tags to Docker images to help organize and manage them. To tag an image, use the docker tag
command:
docker tag my-nginx-app:latest my-nginx-app:v1.0
This will create a new tag v1.0
for the my-nginx-app
image.
Pushing Docker Images
Once you've built and tagged your Docker image, you can push it to a registry using the docker push
command:
docker push my-nginx-app:v1.0
This will push the my-nginx-app:v1.0
image to the default registry (Docker Hub).
Removing Docker Images
To remove a Docker image, use the docker rmi
command:
docker rmi my-nginx-app:v1.0
This will remove the my-nginx-app:v1.0
image from your system.
Now that you've learned how to manage Docker images, let's move on to the next section to learn how to run and deploy Docker containers.