Understanding the basics of Docker image is important for beginners who want to learn about using containers for their applications. In this tutorial, I will teach you how to work with Docker images, providing you with the skills to create, manage, and optimize them effectively.
By the end of this tutorial, you’ll have a solid understanding of Docker images and feel confident in applying your newfound skills to your own projects. So let’s jump in and start exploring the world of containerization together.
To learn more about Docker, please check Docker Tutorials page.
Understanding Docker Images
In this section, I’ll help you understand Docker images, their structure, and their interaction with containerization platforms. You’ll learn about image layers, the Union File System, and image registries like Docker Hub.
What is a Docker image?
A Docker image is a blueprint for your containerized application. It contains all the necessary components, such as the application code, runtime environment, libraries, and dependencies, bundled together in a single package. When you run a Docker container, it’s created from a Docker image, providing a consistent and reproducible environment for your application to run in.
Image Layers and the Union File System
Docker images are made up of multiple layers, each representing a specific instruction or modification to the image. When you build an image, Docker creates a new layer for each command in your Dockerfile. These layers are stacked on top of each other, forming the final image. The Union File System enables Docker to merge these layers together in a way that makes them appear as a single, unified filesystem. This approach helps to keep images lightweight and makes them faster to build and deploy.
Image registries (Docker Hub, etc.)
Docker images can be stored and shared through image registries, which are like repositories for your Docker images. The most popular registry is Docker Hub, a public registry hosted by Docker Inc. It contains a vast collection of official and community-contributed images that you can use as a base for your own projects. There are also other public and private registries available, such as Google Container Registry and Amazon Elastic Container Registry.
In the following sections, I’ll teach you how to interact with these registries, pull images, and create your custom images using Dockerfiles.
Working with Docker Images
In this section, I’ll guide you through the process of working with Docker images, from searching for images on Docker Hub to managing and running containers. We’ll be using the MySQL Docker image as an example throughout this section.
Searching for images on Docker Hub
To search for a Docker image, head over to Docker Hub. In the search bar, type “MySQL” and press Enter. You’ll see a list of MySQL images, with the official MySQL image at the top. You can also find other community-contributed MySQL images with various configurations.
Pulling images from a registry
Pulling a Docker image means downloading it from a registry, such as Docker Hub, and storing it on your local machine. To pull an image, you’ll use the
docker pull command followed by the image name, which usually includes the username of the person or organization that created the image and the image’s repository name, separated by a slash. You can also specify a particular version or tag of the image by appending a colon and the tag name.
For example, to pull the official MySQL image, run the following command:
docker pull mysql
By default, this command will pull the latest version of the MySQL image. If you want to pull a specific version, you can specify the version tag, like this:
docker pull mysql:8.0.33
This command will download the MySQL image with the 8.0.33 tag, which corresponds to MySQL version 8.0.
Docker will then download the MySQL image from Docker Hub and store it on your local machine. If you run
docker images, you should see the MySQL image listed.
It’s important to note that if you try to run a container with an image that isn’t present on your local machine, Docker will automatically attempt to pull the image from the default registry (Docker Hub) before running the container. However, it’s a good practice to explicitly pull the image first, especially if you want to ensure you’re using a specific version.
Listing and managing images
Once you’ve pulled Docker images to your local machine, you might want to view, inspect, or manage them. In this section, we’ll cover how to list, filter, and inspect images, as well as how to remove unwanted images.
To list all Docker images on your local machine, use the
docker images or
docker image ls command:
This command will display a table with information about your local images, including the repository name, tag, image ID, creation date, and size. You’ll see the MySQL image you pulled earlier in this list.
You can filter the list of images based on certain criteria, such as the image name or a specific label. For example, to display only MySQL images, you can use the
-f flag followed by the filter criteria:
docker images -f "reference=mysql*"
This command will display only images with repository names that start with “mysql”.
Inspecting Docker Image
To get more detailed information about a specific image, you can use the
docker image inspect command followed by the image name or ID:
docker image inspect mysql
This command will output a JSON object containing information about the image, such as its layers, environment variables, and configuration.
Removing an Image
If you no longer need a Docker image or want to free up some disk space, you can remove the image from your local machine using the
docker rmi or
docker image rm command. Before you remove an image, ensure that no containers are running or created using that image. To remove the image, run:
docker rmi mysql
This command will remove the MySQL image from your local system. If you have multiple tags of the same image, you’ll need to remove them one by one or use a wildcard to remove them all at once.
Running Container from Docker Image
In this section, I’ll show you how to create and run a container using the
docker run command. Running a container is essentially like creating a running instance of an application based on a Docker image.
To run a container from an image, you’ll use the
docker run command, followed by various options, flags, and the image name. Let’s break down the command we used earlier to create and start a container using the MySQL image:
docker run --name my-mysql -e MYSQL_ROOT_PASSWORD=my-secret-pw -d mysql
Here’s what each part of the command does:
docker run: This is the command that tells Docker to create and run a container.
--name my-mysql: This flag assigns a custom name, “my-mysql”, to the container. You can choose any name you like, but it’s a good practice to use a meaningful name that reflects the container’s purpose.
-e MYSQL_ROOT_PASSWORD=my-secret-pw: This flag sets an environment variable inside the container. In this case, we’re setting the
MYSQL_ROOT_PASSWORDvariable to “my-secret-pw”. This is required to initialize the MySQL container with a root password.
-d: This flag tells Docker to run the container in detached mode, meaning it will run in the background and won’t block your terminal.
mysql: This is the name of the Docker image we want to use to create the container.
Once you run this command, Docker will create a new container based on the MySQL image and start it. You can check the status of your running containers using the
docker ps command.
To learn more, read the following tutorial: Start MySQL in a Docker container.
Container lifecycle: start, stop, remove
Throughout a container’s lifecycle, you’ll create, start, stop, and eventually remove it. To stop a running container, run:
docker stop my-mysql
To start a stopped container, run:
docker start my-mysql
And finally, to remove a container, first, stop it and then run:
docker stop my-mysql docker rm my-mysql
Removing images with the
docker rmi command
If you no longer need a Docker image, you can remove it from your local machine using the
docker rmi command. First, ensure no containers are running using the image. Then run:
docker rmi mysql
This command will remove the MySQL Docker image from your local system.
Frequently Asked Questions
- What is the difference between a Docker image and a container?
A Docker image is a blueprint or template that contains the application code, runtime environment, libraries, and dependencies, whereas a container is a running instance of an image. Think of an image as a class in programming and a container as an object created from that class.
- How do I update a Docker image?
To update a Docker image, you’ll need to update its source (e.g., the Dockerfile or the base image it’s built from), rebuild the image using the
docker buildcommand, and push the updated image to a registry. After that, you can pull the updated image and create new containers using the updated version.
- Can I use multiple registries to store my Docker images?
Yes, you can use multiple registries to store your Docker images. There are public registries, like Docker Hub and Google Container Registry, as well as private registries that you can host yourself or use as part of a cloud service.
- How do I run a container on a specific port?
You can map a container’s internal port to a host port using the
--publishflag followed by the host port, a colon, and the container port. For example, to run a web application on port 8080, you can use the following command:
docker run -p 8080:80 my-web-app
- How do I run a container with a specific user?
You can run a container with a specific user by using the
--userflag followed by the user ID or username. For example, to run a container with the user “myuser”, you can use the following command:
docker run -u myuser my-image
- How can I see the logs of a running container?
You can view the logs of a running container using the
docker logscommand followed by the container name or ID. For example:
docker logs my-mysql
- How do I clean up unused Docker images and containers?
To clean up unused Docker images, containers, networks, and volumes, you can use the
docker system prunecommand. To remove only unused images, use
docker image prune, and to remove only stopped containers, use
docker container prune.Remember, these commands can free up disk space but can also cause loss of important data if not used carefully. Always make sure to backup any essential data before running these commands.