Kubernetes Certification Exam Self-Study Series | Creating Containers

Book source: "CKA/CKAD Test Guide: From Docker to Kubernetes Complete Raiders"

Organize the reading notes while studying, and share them with everyone. If the copyright is violated, it will be deleted. Thank you for your support!

Attach a summary post: Kubernetes certification exam self-study series | Summary_COCOgsta's Blog-CSDN Blog


See how many containers are currently running.

[root@vms100 ~]# docker ps 
CONTAINER ID   IMAGE COMMAND CREATED STATUS PORTS        NAMES 
[root@vms100 ~]#
复制代码

This command shows only the running containers. If you want to view all (running and non-running) containers, you need to write the command docker ps -a, where you need to add the -a option to indicate all.

1.4.1 Create container

Run a minimal container.

[root@vms100 ~]# docker run hub.c.163.com/library/centos 
[root@vms100 ~]#
[root@vms100 ~]# docker ps 
CONTAINER ID   IMAGE COMMAND CREATED STATUS PORTS        NAMES 
[root@vms100 ~]# docker ps -a 
CONTAINER ID   IMAGE                        COMMAND      CREATED        STATUS                   PORTS    NAMES 
bfa8fa89f288   hub.c.163.com/library/centos "/bin/bash"  7 seconds ago Exited(0)6 seconds ago          confident_curie
[root@vms100 ~]#
复制代码

From here, you can see that a container has been created. The ID of the container is bfa8fa89f288. The container name is a randomly generated name, which is confident_curie. The image used is hub.c.163.com/library/centos, and the container runs The process is /bin/bash (that is, specified by CMD in the image).

It cannot be seen by docker ps, but can be seen by docker ps -a, and the status is Exited, indicating that the container is closed. The container shuts down immediately after running, why? Then let's take a look at the life cycle of the container.

1.4.2 The life cycle of the container

We understand the container as the human body, and the process running in it as the human soul. If the soul of a person is down, the body will also be down. Only when the soul is running normally, the body can run normally, as shown in Figure 1-11.

In the same way, only when the process in the container is running normally can the container run normally, and if the process in the container hangs up, the container will also hang up. Because there is no terminal, /bin/bash is executed just like executing the ls command, so the lifetime of the container expires.

Syntax to delete a container:

docker rm 容器ID/容器名
复制代码

If you delete a running container, you can use the -f option:

docker rm -f 容器ID/容器名
复制代码

Delete the previous container.

[root@vms100 ~]# docker rm bfa8fa89f288
bfa8fa89f288
[root@vms100 ~]#
复制代码

Recreate the container, add the -i -t option, it can be written as -it or -i -t,

-t: Simulate a terminal.

-i: allows the user to interact, otherwise the user will be stuck after seeing a prompt.

Step 1: Create a container.

[root@vms100 ~]# docker run -it hub.c.163.com/library/centos 
[root@c81c978cdf1f /]# 
[root@c81c978cdf1f /]# exit 
[root@vms100 ~]#
复制代码

After the container is created, it automatically enters the container to exit the container through exit.

[root@vms100 ~]# docker ps -q #-q选项可以只显示容器id,不会显示太多信息
[root@vms100 ~]# docker ps-a-q 
c81c978cdf1f
[root@vms100 ~]#
复制代码

However, once the container is exited, the container is no longer running.

Step 2: Delete this container.

[root@vms100 ~]# docker rm c81c978cdf1f 
c81c978cdf1f
[root@vms100 ~]# docker ps -a -q 
[root@vms100 ~]#
复制代码

If you want to not automatically enter the container after the container is created, you can add the -d option.

Step 3: Create a container again.

[root@vms100 ~]# docker run -dit hub.c.163.com/library/centos
4aa86357a3df164f985a82e358a1961fe50f7be401bb984d006c09e2957f3175
[root@vms100 ~]#
[root@vms100 ~]# docker ps-q 
4aa86357a3df
[root@vms100 ~]# 
复制代码

Because of the -d option, the container is not automatically entered after the container is created.

[root@vms100 ~]# docker attach 4aa86357a3df 
[root@4aa86357a3df /]# 
[root@4aa86357a3df /]# exit #再执行exit退出
[root@vms100 ~]# docker ps -q 
[root@vms100 ~]# docker ps -a -q 
4aa86357a3df
[root@vms100 ~]#
复制代码

You can see that as long as you exit, the container will automatically close.

Step 4: Delete this container.

[root@vms100 ~]# docker rm 4aa86357a3df 
4aa86357a3df 
[root@vms100 ~]#
复制代码

Add the --restart=always option when running the container to solve the problem of automatic shutdown when exiting the container.

Step 5: Create a container and add the --restart=always option.

[root@vms100 ~]# docker run -dit --restart=always hub.c.163.com/library/cento
75506e8581955448dfa61f16678d1b364e997fa265947a2ede532c323e501f0e 
[root@vms100 ~]# docker ps -q 
75506e858195
[root@vms100 ~]#
复制代码

into the container and exit.

[root@vms100 ~]# docker attach 75506e858195
[root@75506e858195 /]# exit 
exit
[root@vms100 ~]# docker ps-q 
75506e858195
[root@vms100 ~]#
复制代码

You can see that the container is still alive.

Step 6: Delete this container.

[root@vms100 ~]# docker rm 75506e858195
Error response from daemon: You cannot remove a running container 75506e8581955448dfa61f16678d1b364e997fa265947a2ede532c323e501f0e. Stop the container before attempting removal or use -f
复制代码

Because the container is active, it cannot be deleted directly, and the -f option needs to be added.

[root@vms100 ~]#
[root@vms100 ~]# docker rm -f 75506e85819575506e858195
[root@vms100 ~]#
复制代码

It is cumbersome to use the container ID every time you delete the container. You can use --name to specify the container name when creating the container.

Step 7: Create a container, use --name to specify the name of the container.

[root@vms100 ~]# docker run -dit --restart=always --name=c1 hub.c.163.com/library/centos
798a43c4f26cda49653c292a4566097a9344c8c20fd00938ce9f5a8do1abdd61
[root@vms100 ~]# 
[root@vms100 ~]# docker ps 
CONTAINER ID   IMAGE                        COMMAND      CREATED        STATUS                   PORTS    NAMES       
798a43c4f26c   hub.c.163.com/library/centos "/bin/bash"  2 seconds ago  Up 1second                        c1
[root@vms100 ~]#
复制代码

In this way, the name of the container is c1, which is more convenient to manage in the future, such as switching to the container and then exiting.

[root@vms100 ~]# docker attach c1
[root@798a43c4f26c /]# exit 
exit
[root@vms100 ~]#
复制代码

Step 8: Delete this container.

[root@vms100 ~]# docker rm -f c1
c1
[root@vms100 ~]# 
[root@vms100 ~]# docker ps -q -a 
[root@vms100 ~]#
复制代码

1.4.3 Create a temporary container

If you want to temporarily create a test container, and you are afraid that you will forget to delete it when it is used up, you can add the --rm option.

Create a temporary container.

[root@vms100 ~]# docker run -it --name=c1 --rm hub.c.163.com/library/centos 
[root@4067418eebf0 /]# 
[root@4067418eebfo /]# exit 
exit
[root@vms100 ~]#
复制代码

Add --rm when creating the container, and the container will be automatically deleted after exiting the container.

[root@vms100 ~]# docker ps -a -q 
[root@vms100 ~]#
复制代码

You can see that this container is automatically deleted. Note that --rm and --restart=always cannot be used at the same time.

1.4.4 Specify the command to run in the container

When creating a container, what process is running in the container is defined by the CMD command in the image. How to build an image will be explained in detail in a special chapter later. If you want to customize the process running in the container, you can specify it at the end of the command to create the container, for example:

[root@vms100 ~]# docker run -it --name=c1 --rm hub.c.163.com/library/centos sh
sh-4.2#
sh-4.2#
sh-4.2# exit 
exit
[root@vms100 ~]#
复制代码

Here is run in sh mode, not bash.

Run sleep 10 in the container.

[root@vms100 ~]# docker run -it --name=c1 --rm hub.c.163.com/library/centos sleep
[root@vms100 ~]# 
复制代码

The command running in the container is sleep 10. After 10s, the command ends, and the container will also shut down. At this time, the lifespan of the container is 10s.

1.4.5 Using variables when creating containers

When using some images to create containers, variables need to be passed. For example, when using mysql images and wordpress images to create containers, you need to specify some necessary information through variables. If you need a variable, use -e to specify it, and you can use -e multiple times to specify multiple variables.

Create a container c1, which passes two variables.

[root@vms100 ~]# docker run -it --name=c1 --rm -e aa=123 -e bb=456 hub.c.163.com/library/centos
[root@13a417ebc9c3 /]#
[root@13a417ebc9c3 /]# echo $aa
123
[root@13a417ebc9c3 /]# echo $bb
456
[root@13a417ebc9c3 /]# exit
exit
[root@vms100 ~]#
复制代码

When creating the container, two variables aa and bb are specified by -e, and you can see these two variables when you enter the container.

1.4.6 Map container ports to physical machines

The external host (that is, other hosts other than this machine) cannot communicate with the container. If you want the external host to access the contents of the container, you need to use -p to map the port of the container to the physical machine, and then access the corresponding physical machine The port can access the container, as shown in Figure 1-12.

grammar:

-p N: The physical machine randomly generates a port to map to port N of the container.

-p M:N: Map the port N of the container to the port M specified by the physical machine.

Step 1: Create a container and map the container port 80 to a random port on the physical machine.

[root@vms100 ~]# docker run -d --name=web --restart=always -p 80 docker.io/nginx
d267651019fdf1475d444cd43b01826958b4a5fb691024567bb7991d4a606339
[root@vms100 ~]#
复制代码

Here, port 80 of the container web is mapped to a random port of the physical machine. This port number can be queried by the following command.

[root@vms100 ~]# docker ps
CONTAINER ID   IMAGE                        COMMAND                 CREATED         STATUS            PORTS                   NAMES       
d207651019fd   docker.io/nginx              "nginx -g 'daemon ..."  42 seconds ago  Up 42 seconds     0.0.0.0:32770->80/tcp   web  
[root@vms100 ~]#
复制代码

You can see that it is mapped to the port 32770 of the physical machine. If you access the port 32770 of the physical machine, you can access the web container, as shown in Figure 1-13.

Delete this container yourself: docker rm -f web.

If you want to map to the specified port of the physical machine, please use the following command.

[root@vms100 ~]# docker run -d --name=web --restart=always -p 88:80 docker.io/nginx 
305500d7b5008f7a41de5c6415991b2788a932e744c74d3ba5cb0f71b1a5fb31
[root@vms100 ~]#
复制代码

Here, port 80 of the container is mapped to port 88 of the physical machine (you can specify the port yourself, such as 80), then accessing port 88 of the physical machine can access port 80 of the web container, as shown in Figure 1-14.

Guess you like

Origin blog.csdn.net/guolianggsta/article/details/130180219