Docker 5 - containers and container files

catalogue

background

container

Containers and container files

Container status

Container list

be careful

operation

Create container

Start container

Enter container

The difference between {attach and exec when entering the container

Stop container

Restart container

Delete container

Export container

Import container

background

In the last article, I explained how to run the image based on the image file. The operation result of the image is to generate a container instance. This article is to explain the contents of containers and container files.

container

Containers and container files

The container instance generated by the image file is also a file, which is called a container file. That is, once the container is generated, there will be two files at the same time: image file and container file. Moreover, closing the container does not delete the container file, but the container stops running. Therefore, the concept of container state is involved.

Container status

It includes the following:

Container status:
created
restarting
running
removing (migrating)
paused
exited
dead

Container list

docker run ... After that, a container will be generated. After multiple docker runs of the same image file, different containers will be generated, but they are instantiated based on the same image file.

To view commands for the current container:

# Lists the containers that are running on this machine
$ docker container ls
 
# List all containers of the machine, including those that terminate operation
$ docker container ls --all

be careful

1. Only containers that are running in the background or stopped will be listed.

2. After mirroring docker run, enter interactive mode. If you enter exit at this time, the container will not be deleted, but the status of the container changes from running to stopping.

eg, if the - a parameter is not added, there are no running containers to enumerate at this time:

C:\Users\32631>docker container ls
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
 
C:\Users\32631>docker container ls -a
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS                      PORTS               NAMES
d9137260f9ed        ubuntu              "/bin/bash"         29 minutes ago      Exited (0) 24 minutes ago                       sweet_maxwell

Explanation of the results returned above:

1. In the above list, the CONTAINER ID column is the ID number that uniquely identifies the container, and the NAMES column is a name that uniquely identifies the container, which is randomly generated by docker. STATUS indicates the STATUS of the current container. PORTS indicates the open port of the current container (this port will be explained later)

2. Use docker container logs d9137260f9ed or docker container logs sweet_maxwell can display the log information of this container.

3. Note that for any command that needs to use container ID, its ID can be replaced by the Name of the container. ID or Name can uniquely identify a container.

operation

Create container

One container can be created by running the image. At this time, the container will start automatically and be in the running state:

docker run -it ubuntu

Start container

Startup and creation are different concepts. Startup means that after the container is created, it is stopped and can be restarted at this time. It is generally used when the container that exits the operation is restarted.

Start the command through the container ID or container name:

docker start ID/NAME

eg:

C:\Users\32631>docker start d9137260f9ed
d9137260f9ed
 
C:\Users\32631>docker container ls
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
d9137260f9ed        ubuntu              "/bin/bash"         47 minutes ago      Up 29 seconds                           sweet_maxwell

Enter container

If we are creating a container, we will automatically enter the interactive window of the container after creation.

But we started the container above. At this time, the container was only started and did not immediately enter the exchange window.

There are four ways to enter the container:

1. Enter when creating:

docker run -it ubuntu

2. Enter at startup:

docker start -i ID/NAME

3. Enter in attach mode (when the - d parameter is used, the container will enter the background after startup. At this time, you can enter the container through the following instructions)

docker attach ID/NAME

eg, which demonstrates running in the background, obtaining id, and entering attach:

C:\Users\32631>docker container run -itd ubuntu
9fa9f8469ecf73b4f0574b4417b838aca9913417a011e72fd33850cb92f60bdb
 
C:\Users\32631>docker container ls
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
9fa9f8469ecf        ubuntu              "/bin/bash"         22 seconds ago      Up 21 seconds                           affectionate_elbakyan
 
C:\Users\32631>docker attach  9fa9f8469ecf
root@9fa9f8469ecf:/#

4. Enter in exec mode (when the - d parameter is used, the container will enter the background after startup. At this time, you can enter the container through the following instructions)

docker exec -it ID/NAME /bin/bash

Where - it is fixed and / bin/bash is fixed

The difference between {attach and exec when entering the container

The four methods are described above. The third one is similar to the fourth one, but it is recommended to use exec.

difference:

attach After entering the container, if you exit from the container, it will cause the container to stop (i.e. execute) exit Will exit and stop the container)
exec After entering the container, if you exit from the container, it will not stop the container. That's why it is recommended docker exec The reason for this.

Stop container

docker stop ID/NAME

Restart container

docker restart ID/NAME

Delete container

Before deleting a container, you must ensure that the container is stopped.

docker container rm ID/NAME

Export container

The reason why docker can ignore the host and make the software run seamlessly on different systems is that the container can be exported and imported. For example, if the container runs normally, we can package the container and import it to another machine. It is to export the container in this state, which is similar to the backup operation of the database, Not only the mirrored information (similar to table structure) will be retained, but also the current status information (similar to table data).

Command:

docker export -o output.tar ID/NAME

Where - o means output to file, output tar is the target file (usually exported as a. tar file), and ID/NAME represents the ID/NAME of the container

Note: the exported location is where the current command is executed

eg:

C:\Users\32631>docker export -o output.tar 96266b69e2fd
 
C:\Users\32631>

Import container

Is the inverse process of exporting containers

Command:

docker import output.tar IMAGENAME:VERSION

Where output Tar indicates the container to be imported, IMAGENAME indicates the image name (because the container is imported, it will be automatically used as an image), and VERSION indicates the VERSION number. If there is no VERSION number, it is latest by default, which can not be specified

eg, the following examples respectively demonstrate the process from entering container - deleting container - viewing the current container list - importing a container (no image information is specified) - specifying image information - viewing the current container list:

C:\Users\32631>docker attach 96266b69e2fd
root@96266b69e2fd:/# exit
exit
 
C:\Users\32631>docker container rm 96266b69e2fd
96266b69e2fd
 
C:\Users\32631>docker container ls -a
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
 
C:\Users\32631>docker import output.tar
sha256:e816e4dc14d5b58428f8b289421a8a03f22d5f7e41bab5334b1c97919c77cac5
 
C:\Users\32631>docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
<none>              <none>              e816e4dc14d5        12 seconds ago      73.9MB
ubuntu              latest              4e2eef94cd6b        10 days ago         73.9MB
 
C:\Users\32631>docker import output.tar newubuntu
sha256:d60aa43d53cfdd69615366b5eb9bdd88f76b78648e0ca57be8851355037f7200
 
C:\Users\32631>docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
newubuntu           latest              d60aa43d53cf        8 seconds ago       73.9MB
<none>              <none>              e816e4dc14d5        53 seconds ago      73.9MB
ubuntu              latest              4e2eef94cd6b        10 days ago         73.9MB
 
C:\Users\32631>docker container ls -a
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
 
C:\Users\32631>

Added by james_4k2 on Fri, 14 Jan 2022 09:50:16 +0200