Docker learning | 4 Docker container

Posted by Serpent_Guard on Wed, 15 Dec 2021 16:11:02 +0100

catalogue

1. Start the container

1.1 create and start

1.2 start terminated container

2. Background operation

3. Terminate the container

4. Enter the container

attach command

exec command

-i - t {parameter

5. Import and export containers

Export container

Import container snapshot

6. Delete container

Delete a container

Clean all containers in the terminated state

A container is an application or group of applications running independently and their running environment. Correspondingly, virtual machines can be understood as a complete set of operating systems (providing running environment and other system environments) and applications running on them.

1. Start the container

There are two ways to start a container. One is to create a new container based on the image and start it, and the other is to restart the container in the terminated state

1.1 create and start

The required commands are mainly "docker run".

For example, the following command outputs a "Hello World" and then terminates the container.

$ docker run centos /bin/echo 'hello world'
hello world

This is almost no different from directly executing / bin/echo 'hello world' locally.

The following command starts a bash terminal to allow users to interact. In interactive mode, the user can enter commands through the created terminal

The - t , option allows Docker to assign a pseudo TTY and bind it to the standard input of the container, - i , then keeps the standard input of the container open.

$ docker run -t -i centos /bin/bash
[root@1d0cb2d2a601 /]# 

When you create a container using docker run, the standard operations of Docker running in the background include:

  • Check whether the specified image exists locally. If it does not exist, start from registry Download
  • Create and start a container using a mirror
  • Allocate a file system and mount a read-write layer outside the read-only image layer
  • Bridge a virtual interface from the bridge interface configured by the host to the container
  • Configure an ip address from the address pool to the container
  • Execute user specified applications
  • The container is terminated after execution

1.2 start terminated container

You can directly start an exited container by using the {docker container start} command.

The docker container restart command will terminate a running container and then restart it.

2. Background operation

More often, you need to let docker run in the background rather than directly output the result of executing the command to the current host. At this point, you can add the - d # parameter. Whether the container will run for a long time is related to the command specified by {docker run}, and has nothing to do with the - d} parameter.

If you do not use the - d} parameter, run the container. The container will print the output result (STDOUT) to the host

$ docker run centos /bin/sh -c "while true; do echo hello world; sleep 1; done"\\
hello world
hello world
hello world
hello world

If the - d} parameter is used, run the container. The container will run in the background and will not print the output result (STDOUT) to the host computer (the output result can be viewed with {docker logs})

$ docker run -d centos /bin/sh -c "while true; do echo hello world; sleep 1; done"
5731efb18473f6a81043bbbffdac55d7891e9a1685473952095fe18fe1908d78

A unique id will be returned after startup with the - d , parameter. You can also view the container information through the , docker container ls , command.

To obtain the output information of the container, you can use the {docker container logs} command.

$ docker container logs 10b797fd6a86
hello world
hello world
hello world
hello world
hello world
hello world
hello world
hello world
hello world

3. Terminate the container

Use {docker container stop} to terminate a running container.

$ docker container stop 1d0cb2d2a601
1d0cb2d2a601

In addition, when the application specified in the Docker container terminates, the container also terminates automatically.

Only the container of one terminal is started. When the user exits the terminal through the "exit" command or "Ctrl+d", the created container will terminate immediately.

Containers in the terminated state can be seen with the {docker container ls -a} command

$ docker container ls -a

4. Enter the container

You need to enter the container for operations, including using the , docker attach , command or , docker exec , command. It is recommended that you use the , docker exec , command. The reasons will be explained below

attach command

The following example shows how to use the docker attach command.

$ docker run -dit centos
1efeacd2c71fb1616121ddbb39f9596b3649bb1829a588941bf97e83b1fdf056

$ docker container ls 
CONTAINER ID   IMAGE  COMMAND      CREATED          STATUS       PORTS       NAMES
1efeacd2c71f   centos "/bin/bash" 34 seconds ago  Up 32 seconds        affectionate_jemison                             

$ docker attach  1efeacd2c71f
[root@1efeacd2c71f /]# 

Note: if you exit from this stdin, it will cause the container to stop.

exec command

-i - t {parameter

docker exec , can be followed by multiple parameters. Here we mainly describe the - i - t , parameter.

When using only the - i # parameter, since no pseudo terminal is assigned, the interface does not have the familiar Linux command prompt, but the command execution result can still be returned.

When you use the - i - t} parameter together, you can see the familiar Linux command prompt.

$ docker run -dit centos
d7f12479410b507f69e7f60339d095aff41e903a953eabfe48abbe45205eb651

$ docker container ls 
CONTAINER ID   IMAGE  COMMAND      CREATED          STATUS       PORTS       NAMES
d7f12479410b   centos "/bin/bash" About a minute ago   Up About a minute pedantic_murdock

$ docker exec -it d7f12479410b   bash
[root@d7f12479410b /]# 

If you exit from this stdin, it will not cause the container to stop. This is why we recommend using docker exec.

5. Import and export containers

Export container

If you want to export a local container, you can use the docker export command.

$ docker container ls -a
CONTAINER ID   IMAGE  COMMAND      CREATED          STATUS       PORTS       NAMES
d7f12479410b   centos "/bin/bash" About a minute ago   Up About a minute pedantic_murdock

$ docker export d7f12479410b   > centos.tar
$ ll
 Total consumption: 232984
-rw-r--r--. 1 root root 238572544 12 October 15:31 centos.tar

This exports the container snapshot to a local file.

Import container snapshot

You can use {docker import} to import from the container snapshot file as an image, for example

$ cat centos.tar | docker import - test/centos:v1.0
sha256:4f38630c88a4003265bedb88454e5c1b8e1d4753bea7b667da744b3edbeebe67

$ docker image ls 
REPOSITORY     TAG       IMAGE ID       CREATED          SIZE
test/centos    v1.0      4f38630c88a4   32 seconds ago   231MB
hello-world    latest    feb5d9fea6a5   2 months ago     13.3kB
centos         latest    5d0da3dc9764   3 months ago     231MB

You can also import by specifying a URL or a directory, such as

$ docker import http://example.com/exampleimage.tgz example/imagerepo

Note: you can use , docker load , to import image storage files to the local image library, or , docker import , to import a container snapshot to the local image library. The difference between the two is that the container snapshot file will discard all history and metadata information (that is, only the snapshot state of the container at that time), while the mirror storage file will save the complete record and have a large volume. In addition, metadata information such as labels can be reassigned when importing from the container snapshot file.

6. Delete container

Delete a container

You can use docker container rm to delete a container in a terminated state. for example

$ docker container rm d7f12479410b
d7f12479410b

If you want to delete a running container, you can add the - f} parameter. Docker will send a SIGKILL signal to the container.

Clean all containers in the terminated state

Use the docker container ls -a command to view all containers that have been created, including those in terminated status. If there are too many containers, it may be troublesome to delete them one by one. Use the following command to clean up all containers in terminated status.

$ docker container prune
WARNING! This will remove all stopped containers.
Are you sure you want to continue? [y/N] y
Deleted Containers:
1efeacd2c71fb1616121ddbb39f9596b3649bb1829a588941bf97e83b1fdf056
5731efb18473f6a81043bbbffdac55d7891e9a1685473952095fe18fe1908d78
10b797fd6a865033d309b637437f4f93e9067ccb763e94ce46427ffe9f23f231
1d0cb2d2a601b57602a2a60de5c217c2166060e18488322d81f41f69266c5ca0
8a8fd6c25bff9a7fac0d175f8f6057e2e25d19305ce70efcfbb9b258a0aacc43

Topics: Operation & Maintenance Docker Container