Docker容器管理

本文章內容:

Docker容器管理。

 

1. 容器創建

執行docker container create [–name busybox1] [-it] [--rm] busybox:latest  #--name:給容器取的名,如果不取也可用"CONTAINER ID"識別,-i:啟動時要進入交互模式,-t:我們要進交互模式就要給它一個終端,--rm:容器一停止就自動刪除。busybox:latest鏡像默認啟動時運行sh,如果創建時沒加-it,那啟動時無法進交互模式

[root@localhost ~]# docker container create --name busybox1 -it busybox:latest
WARNING: IPv4 forwarding is disabled. Networking will not work.
fbd23bc76490ffbb30098303e8f2a87ca0e91892cdb2634e53a692ea87db0a0d
[root@localhost ~]# docker container ls -a  #命令ls的選項-a才能顯示所有容器(包含沒在運行的)
CONTAINER ID   IMAGE            COMMAND   CREATED          STATUS    PORTS     NAMES
fbd23bc76490   busybox:latest   "sh"      23 seconds ago   Created             busybox1

2. 容器創建並啟動

執行docker container run [--name busybox1] [-it] [--rm] busybox:latest  #--name:給容器取的名,如果不取也可用"CONTAINER ID"識別,-i:啟動時要進入交互模式,-t:我們要進交互模式就要給它一個終端,--rm:容器一停止就自動刪除。busybox:latest鏡像默認啟動時運行sh,如果創建時沒加-it,那啟動時無法進交互模式

[root@localhost ~]# docker container run --name busybox1 -it --rm busybox:latest
WARNING: IPv4 forwarding is disabled. Networking will not work.
/ #

執行docker container run [--name nginx1] [-d] nginx:stable-alpine  #-d:nginx:stable-alpine鏡像默認啟動時運行nginx程序,如果不要讓它佔據前台可以加-d選項讓它運行於後台,如果不加-d並加-it,會看到它執行默認程序顯示的訊息

[root@localhost ~]# docker container run --name nginx1 -d nginx:stable-alpine
WARNING: IPv4 forwarding is disabled. Networking will not work.
7952032a42b716e9b3af431d8870ba2b8aa38f5ea36da634c2d54052b35c5691  #創建的容器
[root@localhost ~]# docker container ls --no-trunc 
CONTAINER ID                                                       IMAGE                 COMMAND                                          CREATED         STATUS         PORTS     NAMES
7952032a42b716e9b3af431d8870ba2b8aa38f5ea36da634c2d54052b35c5691   nginx:stable-alpine   "/docker-entrypoint.sh nginx -g 'daemon off;'"   4 minutes ago   Up 4 minutes   80/tcp    nginx1
#說明:COMMAND為"/docker-entrypoint.sh nginx -g 'daemon off;'",daemon off表示不要運行在後台,一個容器就是為了運行一個程序,如果這個程序跑後台去運行外面的系統會以為這個程序終止了,那容器就結束了。在容器中跑任何程序包括服務程序一定不要讓它在容器中運行在後台的,只要運行在後台一啟動就終止,千萬要記住這一點,因為它是唯一進程再說一遍,它是用來支撐一個容器的骨架(PID號為1)。

3. 容器創建並啟動並不運行默認程序

執行docker container run [--name nginx1] -it nginx:stable-alpine /bin/sh  #啟動運行指定的/bin/sh要加-it才會進入交互模式

[root@localhost ~]# docker container run --name nginx1 -it nginx:stable-alpine /bin/sh
WARNING: IPv4 forwarding is disabled. Networking will not work.
/ #

4. 容器啟動

執行docker container start -ia busybox1  #-i:附加容器的STDIN,-a:連接STDOUT/STDERR和轉發信號,如果容器默認運行的是sh,那加上-ia即可進入交互模式,如果默認運行的不是sh,那就沒必要加-ia了

[root@localhost ~]# docker container ls -a
CONTAINER ID   IMAGE            COMMAND   CREATED          STATUS    PORTS     NAMES
3753c0ad19e4   busybox:latest   "sh"      11 seconds ago   Created             busybox1
[root@localhost ~]# docker container start -ia busybox1
/ #

5. 容器重啟

執行docker container restart busybox1

[root@localhost ~]# docker container ls
CONTAINER ID   IMAGE     COMMAND   CREATED          STATUS                  PORTS     NAMES
0a6083fcf9a1   busybox   "sh"      54 seconds ago   Up Less than a second             busybox1
[root@localhost ~]# docker container restart busybox1
busybox1
[root@localhost ~]# docker container ls
CONTAINER ID   IMAGE     COMMAND   CREATED              STATUS         PORTS     NAMES
0a6083fcf9a1   busybox   "sh"      About a minute ago   Up 6 seconds             busybox1

6. 容器停止

[root@localhost ~]# docker container ls
CONTAINER ID   IMAGE            COMMAND   CREATED              STATUS         PORTS     NAMES
3753c0ad19e4   busybox:latest   "sh"      About a minute ago   Up 8 seconds             busybox1

執行docker conatiner stop busybox1  #相當於發送-9訊號,如果沒有出什麼異常狀態一定不要強制終止,否則會導致數據丟失的(尤其是資料庫系統)

[root@localhost ~]# docker container stop busybox1
busybox1
[root@localhost ~]# docker container ls -a
CONTAINER ID   IMAGE            COMMAND   CREATED         STATUS                        PORTS     NAMES
3753c0ad19e4   busybox:latest   "sh"      2 minutes ago   Exited (137) 17 seconds ago             busybox1

7. 容器強行停止

[root@localhost ~]# docker container ls
CONTAINER ID   IMAGE            COMMAND   CREATED              STATUS         PORTS     NAMES
3753c0ad19e4   busybox:latest   "sh"      About a minute ago   Up 8 seconds             busybox1

執行docker container kill busybox1  #相當於發送-15訊號

[root@localhost ~]# docker container kill busybox1
busybox1
[root@localhost ~]# docker container ls -a        
CONTAINER ID   IMAGE            COMMAND   CREATED         STATUS                       PORTS     NAMES
3753c0ad19e4   busybox:latest   "sh"      3 minutes ago   Exited (137) 3 seconds ago             busybox1

8. 容器暫停

[root@localhost ~]# docker container ls
CONTAINER ID   IMAGE            COMMAND   CREATED              STATUS         PORTS     NAMES
3753c0ad19e4   busybox:latest   "sh"      About a minute ago   Up 8 seconds             busybox1

執行docker container pause busybox1

[root@localhost ~]# docker container pause busybox1
busybox1
[root@localhost ~]# docker container ls
CONTAINER ID   IMAGE            COMMAND   CREATED         STATUS                   PORTS     NAMES
3753c0ad19e4   busybox:latest   "sh"      4 minutes ago   Up 19 seconds (Paused)             busybox1

9. 容器取消暫停

[root@localhost ~]# docker container pause busybox1
busybox1
[root@localhost ~]# docker container ls
CONTAINER ID   IMAGE            COMMAND   CREATED         STATUS                   PORTS     NAMES
3753c0ad19e4   busybox:latest   "sh"      4 minutes ago   Up 19 seconds (Paused)             busybox1

執行docker container unpause busybox1

[root@localhost ~]# docker container unpause busybox1 
busybox1
[root@localhost ~]# docker container ls
CONTAINER ID   IMAGE            COMMAND   CREATED         STATUS         PORTS     NAMES
3753c0ad19e4   busybox:latest   "sh"      6 minutes ago   Up 2 minutes             busybox1

10. 容器刪除

執行docker container rm busybox1  #容器必須為停止狀態才能刪除

[root@localhost ~]# docker container ls -a
CONTAINER ID   IMAGE     COMMAND   CREATED          STATUS                     PORTS     NAMES
8c8086b6151d   busybox   "sh"      12 seconds ago   Exited (0) 4 seconds ago             busybox1
[root@localhost ~]# docker container  rm busybox1
busybox1
[root@localhost ~]# docker container ls -a       
CONTAINER ID   IMAGE     COMMAND   CREATED   STATUS    PORTS     NAMES

11. 容器查看

11.1  查看運行中的容器

執行docker container ls

[root@localhost ~]# docker container ls -a
CONTAINER ID   IMAGE                 COMMAND                  CREATED              STATUS                          PORTS     NAMES
e113315928d9   busybox               "sh"                     About a minute ago   Exited (0) About a minute ago             busybox1
08ef90251924   nginx:stable-alpine   "/docker-entrypoint.…"   28 minutes ago       Up 42 seconds                   80/tcp    nginx1

11.2  查看全部的容器

執行docker container ls -a

[root@localhost ~]# docker container ls -a
CONTAINER ID   IMAGE                 COMMAND                  CREATED              STATUS                          PORTS     NAMES
e113315928d9   busybox               "sh"                     About a minute ago   Exited (0) About a minute ago             busybox1
08ef90251924   nginx:stable-alpine   "/docker-entrypoint.…"   28 minutes ago       Up 42 seconds                   80/tcp    nginx1

11.3  查看運行中的容器中的進程

執行docker container top busybox1

[root@localhost ~]# docker container top busybox1
UID                 PID                 PPID                C                   STIME               TTY                 TIME                CMD
root                21591               21571               0                   23:18               pts/0               00:00:00            sh

11.4    查看容器的詳細資訊

執行docker container inspect busybox1

[root@localhost ~]# docker container inspect busybox1
...

11.5    查看容器容器的日誌

執行docker container logs nginx1

[root@localhost ~]# docker container logs nginx1
/docker-entrypoint.sh: /docker-entrypoint.d/ is not empty, will attempt to perform configuration
/docker-entrypoint.sh: Looking for shell scripts in /docker-entrypoint.d/
/docker-entrypoint.sh: Launching /docker-entrypoint.d/10-listen-on-ipv6-by-default.sh
10-listen-on-ipv6-by-default.sh: Getting the checksum of /etc/nginx/conf.d/default.conf
10-listen-on-ipv6-by-default.sh: Enabled listen on IPv6 in /etc/nginx/conf.d/default.conf
/docker-entrypoint.sh: Launching /docker-entrypoint.d/20-envsubst-on-templates.sh
/docker-entrypoint.sh: Configuration complete; ready for start up
172.17.0.1 - - [21/Feb/2021:13:51:30 +0000] "GET / HTTP/1.1" 200 612 "-" "curl/7.29.0" "-"  #172.17.0.1有來訪問過

12. 在運行中的容器執行別的命令

執行docker container exec -it nginx1 /bin/sh

[root@localhost ~]# docker container ls
CONTAINER ID   IMAGE                 COMMAND                  CREATED       STATUS       PORTS     NAMES
d05bc46beada   nginx:stable-alpine   "/docker-entrypoint.…"   7 hours ago   Up 7 hours   80/tcp    nginx1  #當前有一個運行中nginx的容器
[root@localhost ~]# docker container exec -it nginx1 /bin/sh  #使用exec在運行中的容器執行別的命令
/ # ps
PID   USER     TIME  COMMAND
    1 root      0:00 nginx: master process nginx -g daemon off;  #PID號為1的進程,容器啟動時默認執行的程序
   30 nginx     0:00 nginx: worker process
   38 root      0:00 /bin/sh  #使用exec在運行中的容器執行別的命令
   44 root      0:00 ps
/ #

13. 連上運行中的容器

我們剝離了運行中的容器想再連上去,使用docker container attach。

執行docker container attach nginx1

[root@localhost ~]# docker container ls -a
CONTAINER ID   IMAGE                 COMMAND                  CREATED        STATUS                    PORTS     NAMES
e113315928d9   busybox               "sh"                     23 hours ago   Exited (0) 23 hours ago             busybox1
08ef90251924   nginx:stable-alpine   "/docker-entrypoint.…"   23 hours ago   Up 23 hours               80/tcp    nginx1  #運行在後台中的容器
[root@localhost ~]# docker container attach nginx1

14. 其它

14.1  docker info

顯示Docker系統的詳細資訊。