Contents

Docker Containers and Images

Original article

Commands

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
# View all currently running containers
docker ps -a

# Stop all containers so that images can be deleted:
docker stop $(docker ps -a -q)

# If you want to delete all containers, add another command:
docker rm $(docker ps -a -q)

# View current images
docker images

# Delete images by specifying image id
docker rmi <image id>

# To delete untagged images, i.e., those with <None> as id
docker rmi $(docker images | grep "^<none>" | awk "{print $3}")

# To delete all images
docker rmi $(docker images -q)

Overview

As shown in the figure, images are read-only layers, while containers are read-write layers.

/posts/image_and_container/2021-12-22-16-54-17.png

Definitions

Image

An image is a unified view of a stack of read-only layers. Except for the bottom layer, all other layers have a pointer to the next layer. From the user’s perspective, this is a unified file system.

/posts/image_and_container/2021-12-22-16-55-58.png

Container

Container = Image + Read-write layer. The definition of a container does not mention whether the container needs to be running.

/posts/image_and_container/2021-12-22-16-56-24.png

Running Container

Running Container = Container + Isolated process space + Processes.

/posts/image_and_container/2021-12-22-17-20-59.png

As shown below, the behavior of processes in a container acts on the read-write layer.

/posts/image_and_container/2021-12-22-17-24-44.png

Image Layer

Understanding with Commands

docker create image-id

Add a read-write layer to an image to form a container. The container is not running after creation.

/posts/image_and_container/2021-12-22-17-30-22.png /posts/image_and_container/2021-12-22-17-31-58.png

docker start container-id

Create a process isolation space for the container.

/posts/image_and_container/2021-12-22-17-33-59.png

docker run image-id

run = create + start

/posts/image_and_container/2021-12-22-17-36-05.png

/posts/image_and_container/2021-12-22-17-36-18.png

docker ps

List running containers

/posts/image_and_container/2021-12-22-17-37-16.png

docker ps –a

List all containers

/posts/image_and_container/2021-12-22-17-44-47.png

docker images

List all top-level images. Top-level images refer to images used to create containers or images that can be pulled directly.

/posts/image_and_container/2021-12-22-19-44-26.png

docker images –a

List all images.

/posts/image_and_container/2021-12-22-19-45-35.png

docker stop container-id

Send SIGTERM signal to stop all processes in the running container.

/posts/image_and_container/2021-12-22-19-46-39.png

docker kill container-id

Send SIGKILL signal to stop all processes in the running container.

/posts/image_and_container/2021-12-22-19-48-47.png

docker pause container-id

Pause all processes in the container using cgroups freezer. Sends SIGTSTP signal.

/posts/image_and_container/2021-12-22-20-12-25.png

docker rm container-id

Remove the read-write layer (container).

/posts/image_and_container/2021-12-22-20-49-27.png

docker rmi image-id

Remove image.

OPTIONS description:

  • -f: Force deletion;
  • –no-prune: Do not remove intermediate images of this image, remove by default;

When the same image has multiple tags, executing the docker rmi command only deletes the specified tag from the image’s many tags and does not affect the original image file. If an image does not have multiple tags, and there is only one tag, be careful when executing the delete command, as this will completely delete all file layers of the image.

/posts/image_and_container/2021-12-22-21-16-29.png

docker commit container-id

Convert the container’s read-write layer to a read-only layer, transforming the container into an image.

/posts/image_and_container/2021-12-22-21-17-05.png /posts/image_and_container/2021-12-22-21-17-16.png

docker build

The build command gets the image from the FROM instruction in the Dockerfile, then repeatedly:

  1. run (create and start)
  2. modify
  3. commit

Each step in the loop generates a new layer, so many new layers are created.

/posts/image_and_container/2021-12-22-21-30-30.png /posts/image_and_container/2021-12-22-21-30-52.png

docker exec running-container-id

Execute a process in a running container

/posts/image_and_container/2021-12-22-21-32-35.png

docker inspect container-id or image-id

Extract metadata from a container or image.

/posts/image_and_container/2021-12-22-21-42-51.png

docker save image-id

Create a compressed file from the image. This compressed file preserves each layer and its metadata.

/posts/image_and_container/2021-12-22-22-16-41.png

docker export container-id

Create a compressed file from the image. Compresses multiple image layers into one layer.

/posts/image_and_container/2021-12-22-22-16-54.png

docker history image-id

View the creation history of the specified image.

/posts/image_and_container/2021-12-22-22-17-08.png

State Transitions

/posts/image_and_container/2021-12-22-23-00-03.png