Docker Image Basics: A Comprehensive Tutorial for Beginners

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.

Listing images

To list all Docker images on your local machine, use the docker images or docker image ls command:

docker images

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.

Filtering images

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_PASSWORD variable 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.