Docker-容器使用

懵懂小虎發表於2021-08-02

容器container作為Docker三大基本概念之一,相當於一個系統中執行的軟體。一個映象下可以啟動多個容器。通過Docker命令可以檢視容器container支援的命令。

docker container --help
命令 等價命令 說明
container attach attach 將本地標準輸入、輸出和錯誤流連線到執行中的容器
container commit commit 根據當前容器建立一個新的映象
container cp cp 將宿主機與容器之間複製檔案/資料夾
container create create 建立一個新的容器
container diff diff 檢查容器檔案系統上檔案或目錄的更改
container exec exec 進入一個正在執行的容器
container export export 將容器匯出為本地檔案
container inspect 檢視一個或多個容器詳細資訊
container kill kill 殺死一個或多個正在執行的容器
container logs logs 獲取容器日誌
container ls ps 獲取容器列表
container pause pause 暫停一個或多個容器
container unpause unpause 取消一個或多個暫停的容器
container port port 列出容器的埠對映或特定對映
container prune 刪除全部停止的容器
container rename rename 重新命名一個容器
container restart restart 重啟一個或多個容器
container rm rm 刪除一個或多個容器
container run run 執行一個新的容器
container start start 啟動一個或多個停止的容器
container stats stats 顯示容器資源使用統計資訊的實時流
container stop stop 停止一個或多個正在執行的容器
container top top 顯示容器執行的程式
container update update 更新一個或多個容器配置
container wait wait 等待到一個或多個容器停止,然後列印容器的退出程式碼

接下來就詳細檢視每個命令的使用方式。

attach

將本地標準輸入、輸出和錯誤流連線到執行中的容器

docker container attach [OPTIONS] CONTAINER 
or
docker attach [OPTIONS] CONTAINER

Options:
      --detach-keys string   Override the key sequence for detaching a container
      --no-stdin             Do not attach STDIN
      --sig-proxy            Proxy all received signals to the process (default true)

使用這個命令需要注意的時,當退出容器時,容器也會跟著停止。為了退出時容器不停止,可以使用引數--no-stdin

commit

根據當前容器建立一個新的映象

docker container commit [OPTIONS] CONTAINER [REPOSITORY[:TAG]]
or
docker commit [OPTIONS] CONTAINER [REPOSITORY[:TAG]]

Options:
  -a, --author string    Author (e.g., "John Hannibal Smith <hannibal@a-team.com>")
  -c, --change list      Apply Dockerfile instruction to the created image
  -m, --message string   Commit message
  -p, --pause            Pause container during commit (default true)

這裡啟動的是一個tomcat容器

CONTAINER ID   IMAGE     COMMAND             CREATED          STATUS          PORTS                                       NAMES
ec4b4f0fd45c   4b        "catalina.sh run"   18 minutes ago   Up 18 minutes   0.0.0.0:8088->8080/tcp, :::8088->8080/tcp   fervent_goldberg

將這個容器製作一個新的容器

docker commit -a "arvin88" -m "測試commit命令" ec4b4f0fd45c arvin88/tomcat_user:v1
sha256:77fc61f55a0ca2b504db9265c950168bca7045a7a925abf649c1abaa9a311566

檢視映象

REPOSITORY            TAG       IMAGE ID       CREATED          SIZE
arvin88/tomcat_user   v1        77fc61f55a0c   32 seconds ago   590MB

可以看到已經建立成功

注意:如果不指定倉庫源:版本,預設建立出來的就是,可以通過tag命令來重新指定倉庫源:版本

cp

將宿主機與容器之間複製檔案/資料夾

docker container cp [OPTIONS] CONTAINER:SRC_PATH DEST_PATH|-
docker cp [OPTIONS] SRC_PATH|- CONTAINER:DEST_PATH
or
docker cp [OPTIONS] CONTAINER:SRC_PATH DEST_PATH|-
docker cp [OPTIONS] SRC_PATH|- CONTAINER:DEST_PAT
Options:
  -a, --archive       Archive mode (copy all uid/gid information)
  -L, --follow-link   Always follow symbol link in SRC_PATH

將宿主機的test.py檔案拷貝到tomcat容器的webapps目錄下

docker cp test.py ec4b4f0fd45c:/usr/local/tomcat/webapps

create

建立一個新的容器

docker container create [OPTIONS] IMAGE [COMMAND] [ARG...]
or 
docker create [OPTIONS] IMAGE [COMMAND] [ARG...]

引數有點多,這裡就不列出來了,有興趣可以使用--help檢視。

docker create -it -p 8089:8080 77fc61f55a0c

建立的新容器,預設是未啟動的,可以使用start命令啟動。

docker ps -a
CONTAINER ID   IMAGE          COMMAND             CREATED          STATUS          PORTS                                       NAMES
e5e2f9915c42   77fc61f55a0c   "catalina.sh run"   7 seconds ago    Created                                                     frosty_ellis
ec4b4f0fd45c   4b             "catalina.sh run"   37 minutes ago   Up 37 minutes   0.0.0.0:8088->8080/tcp, :::8088->8080/tcp   fervent_goldberg
docker start ec
docker ps
CONTAINER ID   IMAGE          COMMAND             CREATED          STATUS          PORTS                                       NAMES
e5e2f9915c42   77fc61f55a0c   "catalina.sh run"   42 seconds ago   Up 2 seconds    0.0.0.0:8089->8080/tcp, :::8089->8080/tcp   frosty_ellis
ec4b4f0fd45c   4b             "catalina.sh run"   38 minutes ago   Up 38 minutes   0.0.0.0:8088->8080/tcp, :::8088->8080/tcp   fervent_goldberg

diff

檢查容器檔案系統上檔案或目錄的更改

docker container diff CONTAINER
docker diff CONTAINER

這個命令比較簡單,沒有其他引數。

docker diff e5e2f9915c42

C /usr
C /usr/local
C /usr/local/tomcat
C /usr/local/tomcat/logs
C /usr/local/tomcat/logs/catalina.2021-08-01.log
C /usr/local/tomcat/logs/localhost.2021-08-01.log
C /tmp
C /tmp/hsperfdata_root
C /tmp/hsperfdata_root/1

exec

進入一個正在執行的容器

docker container exec [OPTIONS] CONTAINER COMMAND [ARG...]
or
docker exec [OPTIONS] CONTAINER COMMAND [ARG...]

Options:
  -d, --detach               Detached mode: run command in the background
      --detach-keys string   Override the key sequence for detaching a container
  -e, --env list             Set environment variables
      --env-file list        Read in a file of environment variables
  -i, --interactive          Keep STDIN open even if not attached
      --privileged           Give extended privileges to the command
  -t, --tty                  Allocate a pseudo-TTY
  -u, --user string          Username or UID (format: <name|uid>[:<group|gid>])
  -w, --workdir string       Working directory inside the container

通常使用-i -t引數,進入容器

docker exec -it e5 /bin/bas
root@e5e2f9915c42:/usr/local/tomcat# ls

看到root後面跟著容器ID,則表示進入到容器了,如果想退出容器,使用exit命令,就可以退出容器。

export

將容器匯出為本地檔案

 docker container export [OPTIONS] CONTAINER
 or
 docker export [OPTIONS] CONTAINER
Options:
  -o, --output string   Write to a file, instead of STDOUT 

這個必須要帶引數

docker export -o "tomcat_user_v1.gz.tar" e5

輸出的檔案路徑支援絕對路徑,如果不填絕對路徑,則預設就是在當前執行匯出命令的目錄下,使用llls檢視檔案。

-rw-------. 1 root root 588863488 Aug  1 11:51 tomcat_user_v1.gz.tar

從結果上看出已經匯出成功。

inspect

檢視一個或多個容器詳細資訊

docker container inspect [OPTIONS] CONTAINER [CONTAINER...]
Options:
  -f, --format string   Format the output using the given Go template
  -s, --size            Display total file sizes

命令:docker container inspect e5e2f9915c42,輸出的內容太多了,就不貼出來了,可以自行驗證檢視。

kill

殺死一個或多個正在執行的容器

docker container kill [OPTIONS] CONTAINER [CONTAINER...]
or
docker kill [OPTIONS] CONTAINER [CONTAINER...]
Options:
  -s, --signal string   Signal to send to the container (default "KILL")

命令操作:

[root@localhost ~]# docker ps
CONTAINER ID   IMAGE          COMMAND             CREATED          STATUS          PORTS                                       NAMES
e5e2f9915c42   77fc61f55a0c   "catalina.sh run"   21 minutes ago   Up 20 minutes   0.0.0.0:8089->8080/tcp, :::8089->8080/tcp   frosty_ellis
ec4b4f0fd45c   4b             "catalina.sh run"   59 minutes ago   Up 59 minutes   0.0.0.0:8088->8080/tcp, :::8088->8080/tcp   fervent_goldberg
[root@localhost ~]# docker kill e5
e5
[root@localhost ~]# docker ps
CONTAINER ID   IMAGE     COMMAND             CREATED          STATUS          PORTS                                       NAMES
ec4b4f0fd45c   4b        "catalina.sh run"   59 minutes ago   Up 59 minutes   0.0.0.0:8088->8080/tcp, :::8088->8080/tcp   fervent_goldberg

logs

獲取容器日誌

docker container logs [OPTIONS] CONTAINER
or 
docker logs [OPTIONS] CONTAINER
Options:
      --details        Show extra details provided to logs
  -f, --follow         Follow log output
      --since string   Show logs since timestamp (e.g. 2013-01-02T13:23:37Z) or relative (e.g. 42m for 42 minutes)
  -n, --tail string    Number of lines to show from the end of the logs (default "all")
  -t, --timestamps     Show timestamps
      --until string   Show logs before a timestamp (e.g. 2013-01-02T13:23:37Z) or relative (e.g. 42m for 42 minutes)

啟動一個tomcat容器,然後使用命令檢視tomcat啟動日誌。

docker logs 8efed1c23ff5
02-Aug-2021 07:19:06.894 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Server version name:   Apache Tomcat/8.5.69
02-Aug-2021 07:19:06.895 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Server built:          Jun 30 2021 18:00:00 UTC
02-Aug-2021 07:19:06.895 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Server version number: 8.5.69.0
02-Aug-2021 07:19:06.895 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log OS Name:               Linux
02-Aug-2021 07:19:06.895 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log OS Version:            4.18.0-147.el8.x86_64
02-Aug-2021 07:19:06.896 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Architecture:          amd64
02-Aug-2021 07:19:06.896 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Java Home:             /usr/local/openjdk-8/jre
02-Aug-2021 07:19:06.896 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log JVM Version:           1.8.0_302-b08
02-Aug-2021 07:19:06.896 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log JVM Vendor:            Oracle Corporation
02-Aug-2021 07:19:06.896 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log CATALINA_BASE:         /usr/local/tomcat
02-Aug-2021 07:19:06.896 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log CATALINA_HOME:         /usr/local/tomcat
02-Aug-2021 07:19:06.896 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Djava.util.logging.config.file=/usr/local/tomcat/conf/logging.properties
02-Aug-2021 07:19:06.896 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager
02-Aug-2021 07:19:06.896 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Djdk.tls.ephemeralDHKeySize=2048
02-Aug-2021 07:19:06.896 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Djava.protocol.handler.pkgs=org.apache.catalina.webresources
02-Aug-2021 07:19:06.896 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Dorg.apache.catalina.security.SecurityListener.UMASK=0027
02-Aug-2021 07:19:06.896 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Dignore.endorsed.dirs=
02-Aug-2021 07:19:06.896 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Dcatalina.base=/usr/local/tomcat
02-Aug-2021 07:19:06.897 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Dcatalina.home=/usr/local/tomcat
02-Aug-2021 07:19:06.897 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Djava.io.tmpdir=/usr/local/tomcat/temp
02-Aug-2021 07:19:06.897 INFO [main] org.apache.catalina.core.AprLifecycleListener.lifecycleEvent Loaded Apache Tomcat Native library [1.2.30] using APR version [1.6.5].
02-Aug-2021 07:19:06.897 INFO [main] org.apache.catalina.core.AprLifecycleListener.lifecycleEvent APR capabilities: IPv6 [true], sendfile [true], accept filters [false], random [true].
02-Aug-2021 07:19:06.897 INFO [main] org.apache.catalina.core.AprLifecycleListener.lifecycleEvent APR/OpenSSL configuration: useAprConnector [false], useOpenSSL [true]
02-Aug-2021 07:19:06.900 INFO [main] org.apache.catalina.core.AprLifecycleListener.initializeSSL OpenSSL successfully initialized [OpenSSL 1.1.1d  10 Sep 2019]
02-Aug-2021 07:19:06.976 INFO [main] org.apache.coyote.AbstractProtocol.init Initializing ProtocolHandler ["http-nio-8080"]
02-Aug-2021 07:19:06.990 INFO [main] org.apache.tomcat.util.net.NioSelectorPool.getSharedSelector Using a shared selector for servlet write/read
02-Aug-2021 07:19:07.001 INFO [main] org.apache.catalina.startup.Catalina.load Initialization processed in 582 ms
02-Aug-2021 07:19:07.030 INFO [main] org.apache.tomcat.util.digester.FactoryCreateRule.begin [FactoryCreateRule] Create exception ignored: java.lang.NullPointerException
02-Aug-2021 07:19:07.034 INFO [main] org.apache.catalina.core.StandardService.startInternal Starting service [Catalina]
02-Aug-2021 07:19:07.035 INFO [main] org.apache.catalina.core.StandardEngine.startInternal Starting Servlet engine: [Apache Tomcat/8.5.69]
02-Aug-2021 07:19:07.045 INFO [localhost-startStop-1] org.apache.catalina.startup.HostConfig.deployDirectory Deploying web application directory [/usr/local/tomcat/webapps/ROOT]
02-Aug-2021 07:19:07.267 INFO [localhost-startStop-1] org.apache.catalina.startup.HostConfig.deployDirectory Deployment of web application directory [/usr/local/tomcat/webapps/ROOT] has finished in [221] ms
02-Aug-2021 07:19:07.267 INFO [localhost-startStop-1] org.apache.catalina.startup.HostConfig.deployDirectory Deploying web application directory [/usr/local/tomcat/webapps/docs]
02-Aug-2021 07:19:07.281 INFO [localhost-startStop-1] org.apache.catalina.startup.HostConfig.deployDirectory Deployment of web application directory [/usr/local/tomcat/webapps/docs] has finished in [14] ms
02-Aug-2021 07:19:07.281 INFO [localhost-startStop-1] org.apache.catalina.startup.HostConfig.deployDirectory Deploying web application directory [/usr/local/tomcat/webapps/examples]
02-Aug-2021 07:19:07.504 INFO [localhost-startStop-1] org.apache.catalina.startup.HostConfig.deployDirectory Deployment of web application directory [/usr/local/tomcat/webapps/examples] has finished in [223] ms
02-Aug-2021 07:19:07.504 INFO [localhost-startStop-1] org.apache.catalina.startup.HostConfig.deployDirectory Deploying web application directory [/usr/local/tomcat/webapps/host-manager]
02-Aug-2021 07:19:07.524 INFO [localhost-startStop-1] org.apache.catalina.startup.HostConfig.deployDirectory Deployment of web application directory [/usr/local/tomcat/webapps/host-manager] has finished in [19] ms
02-Aug-2021 07:19:07.524 INFO [localhost-startStop-1] org.apache.catalina.startup.HostConfig.deployDirectory Deploying web application directory [/usr/local/tomcat/webapps/manager]
02-Aug-2021 07:19:07.539 INFO [localhost-startStop-1] org.apache.catalina.startup.HostConfig.deployDirectory Deployment of web application directory [/usr/local/tomcat/webapps/manager] has finished in [15] ms
02-Aug-2021 07:19:07.544 INFO [main] org.apache.coyote.AbstractProtocol.start Starting ProtocolHandler ["http-nio-8080"]
02-Aug-2021 07:19:07.573 INFO [main] org.apache.catalina.startup.Catalina.start Server startup in 571 ms

從結果可以看出日誌輸出了tomcat的啟動日誌,如果需要時時檢視日誌,可以增加引數-f,就可以時時跟蹤日誌。

ls

獲取容器列表

docker container ls [OPTIONS]
or 
docker ps [OPTIONS]

Options:
  -a, --all             Show all containers (default shows just running)
  -f, --filter filter   Filter output based on conditions provided
      --format string   Pretty-print containers using a Go template
  -n, --last int        Show n last created containers (includes all states) (default -1)
  -l, --latest          Show the latest created container (includes all states)
      --no-trunc        Don't truncate output
  -q, --quiet           Only display container IDs
  -s, --size            Display total file sizes

不加任何引數情況下,列出正在執行的容器:

docker container ls
or 
docker ps
兩種命令效果一樣
CONTAINER ID   IMAGE     COMMAND             CREATED         STATUS         PORTS                                       NAMES
8efed1c23ff5   4b        "catalina.sh run"   6 minutes ago   Up 6 minutes   0.0.0.0:8088->8080/tcp, :::8088->8080/tcp   charming_pike

增加-a引數,可以檢視所有的容器。

dokcer ps -a
CONTAINER ID   IMAGE     COMMAND             CREATED          STATUS                       PORTS                                       NAMES
30c33d778412   4b        "catalina.sh run"   20 seconds ago   Up 17 seconds                0.0.0.0:8089->8080/tcp, :::8089->8080/tcp   elated_northcutt
8efed1c23ff5   4b        "catalina.sh run"   7 minutes ago    Exited (143) 3 seconds ago                                               charming_pike

pause

暫停一個或多個容器

docker container pause CONTAINER [CONTAINER...]
or
docker pause CONTAINER [CONTAINER...]

檢視容器執行情況

docker ps
CONTAINER ID   IMAGE     COMMAND             CREATED          STATUS         PORTS                                       NAMES
30c33d778412   4b        "catalina.sh run"   5 minutes ago    Up 5 minutes   0.0.0.0:8089->8080/tcp, :::8089->8080/tcp   elated_northcutt
8efed1c23ff5   4b        "catalina.sh run"   12 minutes ago   Up 6 seconds   0.0.0.0:8088->8080/tcp, :::8088->8080/tcp   charming_pike

暫停上面列出來的容器

docker pause 30c33d778412 8efed1c23ff5
暫停成功輸出容器ID
30c33d778412
8efed1c23ff5

檢視所有容器

 docker ps -a
CONTAINER ID   IMAGE     COMMAND             CREATED          STATUS                       PORTS                                       NAMES
30c33d778412   4b        "catalina.sh run"   6 minutes ago    Up 6 minutes (Paused)        0.0.0.0:8089->8080/tcp, :::8089->8080/tcp   elated_northcutt
8efed1c23ff5   4b        "catalina.sh run"   13 minutes ago   Up About a minute (Paused)   0.0.0.0:8088->8080/tcp, :::8088->8080/tcp   charming_pike

從結果的STATUS列可以看到,後面多了(Paused),表示容器已經暫停。

unpause

取消一個或多個暫停的容器

docker container unpause CONTAINER [CONTAINER...]
or
docker unpause CONTAINER [CONTAINER...]

上面我們已經暫停了兩個容器,現在使用unpause取消暫停

docker unpause 30c33d778412 8efed1c23ff5
30c33d778412
8efed1c23ff5

使用ps -a命令檢視

CONTAINER ID   IMAGE     COMMAND             CREATED          STATUS         PORTS                                       NAMES
30c33d778412   4b        "catalina.sh run"   9 minutes ago    Up 9 minutes   0.0.0.0:8089->8080/tcp, :::8089->8080/tcp   elated_northcutt
8efed1c23ff5   4b        "catalina.sh run"   17 minutes ago   Up 4 minutes   0.0.0.0:8088->8080/tcp, :::8088->8080/tcp   charming_pike

port

列出容器的埠對映或特定對映

docker container port CONTAINER [PRIVATE_PORT[/PROTO]]
or
docker port CONTAINER [PRIVATE_PORT[/PROTO]]

檢視容器埠對映

docker container port 30c33d778412
8080/tcp -> 0.0.0.0:8089
8080/tcp -> :::8089

prune

刪除全部停止的容器

docker container prune [OPTIONS]
Options:
      --filter filter   Provide filter values (e.g. 'until=<timestamp>')
  -f, --force           Do not prompt for confirmation
docker container prune
WARNING! This will remove all stopped containers.
Are you sure you want to continue? [y/N] y
Total reclaimed space: 0B

刪除的時候會提示是否繼續,如果不想提示可以加-f引數,最終會輸出刪除結果。

rename

重新命名一個容器

docker container rename CONTAINER NEW_NAME
or 
docker rename CONTAINER NEW_NAME

檢視容器

CONTAINER ID   IMAGE     COMMAND             CREATED          STATUS          PORTS                                       NAMES
30c33d778412   4b        "catalina.sh run"   23 minutes ago   Up 23 minutes   0.0.0.0:8089->8080/tcp, :::8089->8080/tcp   elated_northcutt
8efed1c23ff5   4b        "catalina.sh run"   30 minutes ago   Up 18 minutes   0.0.0.0:8088->8080/tcp, :::8088->8080/tcp   charming_pike

將容器30c33d778412重新命名

docker rename 30c33d778412 test_name

CONTAINER ID   IMAGE     COMMAND             CREATED          STATUS          PORTS                                       NAMES
30c33d778412   4b        "catalina.sh run"   24 minutes ago   Up 24 minutes   0.0.0.0:8089->8080/tcp, :::8089->8080/tcp   test_name
8efed1c23ff5   4b        "catalina.sh run"   31 minutes ago   Up 18 minutes   0.0.0.0:8088->8080/tcp, :::8088->8080/tcp   charming_pike

從結果的NAMES看到已經修改成test_name的容器名了。

restart

重啟一個或多個容器

docker container restart [OPTIONS] CONTAINER [CONTAINER...]
or
docker restart [OPTIONS] CONTAINER [CONTAINER...]
Options:
  -t, --time int   Seconds to wait for stop before killing the container (default 10)

重啟上面列出的兩個容器30c33d778412,8efed1c23ff5

docker restart 30c33d778412 8efed1c23ff5
30c33d778412
8efed1c23ff5

如果重啟容器想隔一會再重啟,可以增加引數-t,單位:秒。

rm

刪除一個或多個容器

docker container rm [OPTIONS] CONTAINER [CONTAINER...]
or
docker rm [OPTIONS] CONTAINER [CONTAINER...]
Options:
  -f, --force     Force the removal of a running container (uses SIGKILL)
  -l, --link      Remove the specified link
  -v, --volumes   Remove anonymous volumes associated with the container

刪除容器時,容器必須是未執行的。

docker rm 30c33d778412
Error response from daemon: You cannot remove a running container 30c33d7784121fc46beba6a3097ac9dda9a47e37c5bc6ccc4f2c2b7eb9b5b0cf. Stop the container before attempting removal or force remove

如果實在是想刪除,可以新增-f引數,強制刪除執行的容器。

run

執行一個新的容器,使用方式與建立容器差不多,引數也差不多。

docker container run [OPTIONS] IMAGE [COMMAND] [ARG...]
or
docker run [OPTIONS] IMAGE [COMMAND] [ARG...]

執行tomcat容器,後臺執行並將埠對映為8087

docker run -d -p 8087:8080 4b6a6e0cf6c1

start

啟動一個或多個停止的容器

docker container start [OPTIONS] CONTAINER [CONTAINER...]
or 
docker start [OPTIONS] CONTAINER [CONTAINER...]
Options:
  -a, --attach               Attach STDOUT/STDERR and forward signals
      --detach-keys string   Override the key sequence for detaching a container
  -i, --interactive          Attach container's STDIN

啟動容器30c33d778412

docker start 30c33d778412
30c33d778412

如果想看到啟動的過程,可以增加-a引數,不過退出啟動過程,容器也會跟著停止。

stats

顯示容器資源使用統計資訊的實時流

docker container stats [OPTIONS] [CONTAINER...]
or
ocker stats [OPTIONS] [CONTAINER...]
Options:
  -a, --all             Show all containers (default shows just running)
      --format string   Pretty-print images using a Go template
      --no-stream       Disable streaming stats and only pull the first result
      --no-trunc        Do not truncate output

如果不跟任何容器,則輸出所有容器的實時資訊

docker stats
CONTAINER ID   NAME            CPU %     MEM USAGE / LIMIT    MEM %     NET I/O       BLOCK I/O   PIDS
30c33d778412   test_name       0.10%     102.7MiB / 3.67GiB   2.73%     866B / 0B     0B / 0B     33
8efed1c23ff5   charming_pike   0.07%     97.89MiB / 3.67GiB   2.61%     1.01kB / 0B   0B / 0B     33

stop

停止一個或多個正在執行的容器

docker container stop [OPTIONS] CONTAINER [CONTAINER...]
docker stop [OPTIONS] CONTAINER [CONTAINER...]
Options:
  -t, --time int   Seconds to wait for stop before killing it (default 10)

從引數上看,停止命令與重啟命令一樣,可以設定時間。

docker stop 30c33d778412

docker ps -a
CONTAINER ID   IMAGE     COMMAND             CREATED          STATUS                        PORTS                                       NAMES
30c33d778412   4b        "catalina.sh run"   46 minutes ago   Exited (143) 13 seconds ago                                               test_name
8efed1c23ff5   4b        "catalina.sh run"   53 minutes ago   Up 18 minutes                 0.0.0.0:8088->8080/tcp, :::8088->8080/tcp   charming_pike

從結果看出PORTS列,停止掉的容器已經是空的,表示容器已經停止成功。

update

更新一個或多個容器配置

docker container update [OPTIONS] CONTAINER [CONTAINER...]
or
docker update [OPTIONS] CONTAINER [CONTAINER...]
Options:
      --blkio-weight uint16        Block IO (relative weight), between 10 and 1000, or 0 to disable (default 0)
      --cpu-period int             Limit CPU CFS (Completely Fair Scheduler) period
      --cpu-quota int              Limit CPU CFS (Completely Fair Scheduler) quota
      --cpu-rt-period int          Limit the CPU real-time period in microseconds
      --cpu-rt-runtime int         Limit the CPU real-time runtime in microseconds
  -c, --cpu-shares int             CPU shares (relative weight)
      --cpus decimal               Number of CPUs
      --cpuset-cpus string         CPUs in which to allow execution (0-3, 0,1)
      --cpuset-mems string         MEMs in which to allow execution (0-3, 0,1)
      --kernel-memory bytes        Kernel memory limit
  -m, --memory bytes               Memory limit
      --memory-reservation bytes   Memory soft limit
      --memory-swap bytes          Swap limit equal to memory plus swap: '-1' to enable unlimited swap
      --pids-limit int             Tune container pids limit (set -1 for unlimited)
      --restart string             Restart policy to apply when a container exits

從引數上看,可以更新容器的cpu記憶體配置,需要注意的是,--kernel-memoryDocker 20.10起,該選項已被棄用。

wait

等待到一個或多個容器停止,然後列印容器的退出程式碼

docker container wait CONTAINER [CONTAINER...]
or
docker wait CONTAINER [CONTAINER...]

如果是一個已經停止的容器,則直接輸出退出的程式碼

docker wait 30c33d778412
143

指定一個正在執行的容器,會一直等待容器停止,然後輸出退出程式碼。

docker wait 8efed1c23ff5

啟動另外一個視窗,停止8efed1c23ff5容器,這個時候之前wait命令的視窗就輸出143的容器退出程式碼。

相關文章