当前位置:网站首页>Container (4) operating container method [17]

Container (4) operating container method [17]

2020-11-10 18:59:51 cuiyongchao007

( One ) Run container method

​ In the last chapter, we learned how to construct Docker Mirror image , And run the container through the mirror . This chapter will discuss containers in depth : Learn how to operate containers , How to change the state of the container , And the underlying technology to implement the container .

(1) Run container

docker run It's the way to start the container . In the discussion Dockerfile We have learned that , There are three ways to specify the command to execute when the container starts :

  1. CMD Instructions .
  2. ENDPOINT Instructions .
  3. stay docker run The command line specifies .

Such as , Run one Ubuntu Execute after container pwd command :

root@cuiyongchao:/dockerfile# docker run ubuntu pwd
/
root@cuiyongchao:/dockerfile#

​ Execute... When container starts  pwd, Back to  /  Is the current directory in the container . perform  docker ps  or  docker container ls  You can see Docker host The container currently running in :

root@cuiyongchao:/dockerfile# docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                    NAMES

Why , There's no container ? use  docker ps -a  or  docker container ls -a  have a look .

root@cuiyongchao:/dockerfile# docker ps -a
CONTAINER ID        IMAGE               COMMAND                  CREATED              STATUS                          PORTS                    NAMES
fe757aa05f42        ubuntu              "pwd"                    About a minute ago   Exited (0) About a minute ago                            happy_saha

-a All status containers will be displayed , You can see , The previous container has exited , Status as Exited. such “ In a flash ” The container is usually not what we want , We want the container to keep runing state , So we can use it .

(2) The container runs for a long time

​ How to make the container save and run ? Because the lifecycle of the container depends on the command executed at startup , As long as the order doesn't end , The container doesn't exit . Understand this principle , We can keep the container running by executing a long-running command . For example, execute the following command :

root@cuiyongchao:/dockerfile# docker run ubuntu /bin/bash -c "while true;do sleep 1;done"

while  Statement to bash Will not quit . We can open another terminal to see the status of the container :

CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                    NAMES
27e457c2454a        ubuntu              "/bin/bash -c 'while…"   12 seconds ago      Up 11 seconds                                adoring_hoover

It can be seen that the container is still running . But there's a drawback to this approach : It takes up a terminal . We can add parameters -d Start the container in background mode .

root@cuiyongchao:~# docker run -d ubuntu /bin/bash -c "while true;do sleep 1;done"
1d48fd6d1a40e101d74c2d6f8fc92b59a8f87b73fa1a66131a2b1ebb8605e83f
root@cuiyongchao:~# 

The container started and went back to docker host Terminal . See here docker Returned a string of characters , This is the container ID. adopt  docker ps  Look at the container :

root@cuiyongchao:/dockerfile# docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                    NAMES
1d48fd6d1a40        ubuntu              "/bin/bash -c 'while…"   55 seconds ago      Up 49 seconds                                priceless_lovelace
27e457c2454a        ubuntu              "/bin/bash -c 'while…"   3 minutes ago       Up 3 minutes                                 adoring_hoover

​ Now we have two running containers . Here, pay attention to the container CONTAINER ID and NAMES These two fields .

CONTAINER ID It's a container “ short ID”, When the container is started, it returns “ Long ID”. short ID It's long ID Before 12 Characters .NAMES Field displays the name of the container , You can start the container by --name The parameters explicitly name the container , If you don't specify ,docker The container is automatically assigned a name .

For subsequent operations on the container , We need to pass “ Long ID”、“ short ID” perhaps “ name ” To specify the container to operate on . For example, stop a container below :

root@cuiyongchao:/dockerfile# docker stop 1d48fd6d1a40
1d48fd6d1a40

​ Here we are through “ short ID” Specifies the container to stop .

​ adopt while The booted container can keep running , But actually nothing meaningful has been done . The common use of containers is to run background services , For example, we have already seen http server:

root@cuiyongchao:/dockerfile# docker run -d --name "my_http_server" httpd
Unable to find image 'httpd:latest' locally
latest: Pulling from library/httpd
Digest: sha256:b82fb56847fcbcca9f8f162a3232acb4a302af96b1b2af1c4c3ac45ef0c9b968
Status: Downloaded newer image for httpd:latest
638740d1042a54b577c992991e7255fc07d35aa229eb8487b3a414a8dc49f0a5

​ This time we use  --name  Specifies the name of the container . We also see that the command that the container runs is httpd-foreground, adopt  docker history  We can see that the order was passed through CMD designated .

root@cuiyongchao:/dockerfile# docker ps 
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                    NAMES
638740d1042a        httpd               "httpd-foreground"       29 seconds ago      Up 27 seconds       80/tcp                   my_http_server

​ We often need to go into the container to do some work , For example, check the log 、 debugging 、 Start other processes, etc . The next section will learn how to get inside the container .

版权声明
本文为[cuiyongchao007]所创,转载请带上原文链接,感谢