docker容器操作 === ## docker容器指令 ### `docker`和容器相關的指令 ```shell attach Attach local standard input, output, and error streams to a running container cp Copy files/folders between a container and the local filesystem create Create a new container diff Inspect changes to files or directories on a container's filesystem exec Run a command in a running container export Export a container's filesystem as a tar archive inspect Return low-level information on Docker objects kill Kill one or more running containers logs Fetch the logs of a container pause Pause all processes within one or more containers port List port mappings or a specific mapping for the container ps List containers rename Rename a container restart Restart one or more containers rm Remove one or more containers run Run a command in a new container start Start one or more stopped containers stats Display a live stream of container(s) resource usage statistics stop Stop one or more running containers top Display the running processes of a container unpause Unpause all processes within one or more containers update Update configuration of one or more containers wait Block until one or more containers stop, then print their exit codes ``` ### `docker container`指令 ```shell= attach Attach local standard input, output, and error streams to a running container commit Create a new image from a container's changes cp Copy files/folders between a container and the local filesystem create Create a new container diff Inspect changes to files or directories on a container's filesystem exec Run a command in a running container export Export a container's filesystem as a tar archive inspect Display detailed information on one or more containers kill Kill one or more running containers logs Fetch the logs of a container ls List containers pause Pause all processes within one or more containers port List port mappings or a specific mapping for the container prune Remove all stopped containers rename Rename a container restart Restart one or more containers rm Remove one or more containers run Run a command in a new container start Start one or more stopped containers stats Display a live stream of container(s) resource usage statistics stop Stop one or more running containers top Display the running processes of a container unpause Unpause all processes within one or more containers update Update configuration of one or more containers wait Block until one or more containers stop, then print their exit codes ``` ### `docker`總覽 * 檢查環境:version, info * 建立:run(create) * 檢查:logs, top, inspect, port * 啟動:start, restart * 列出:ps * 執行:attach, exec, unpause * 停止:stop, kill,pause,wait * 存入存出:export, import * 刪除:rm * 交換資料:cp * 列出和主機資料的差異:diff,events ### `docker container`總覽 * 建立刪除:create, run, kill, rm, prune * 連接:attach, exec * 監視:inspect, logs, stats, top,port * 列出:container ls, ps * 檔案:commit, cp, diff, export * 執行時:kill, pause, rename, restart, start, stop unpause, wait, update ## 容器指令範例 **範例1:`docker run <OPTIONS> <image>`** 啟動一個container,參數另說明,參考 https://hackmd.io/@joshhu/H1467RoUt 。 ``` # 執行完一個容器後就離開 (joshhuAI)joshhu:~/ $ docker run ubuntu uname -a [1:41:28] Linux 93263a70182b 4.15.0-142-generic #146~16.04.1-Ubuntu SMP Tue Apr 13 09:27:15 UTC 2021 x86_64 x86_64 x86_64 GNU/Linux (joshhuAI)joshhu:~/ $ docker ps [1:41:38] CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES (joshhuAI)joshhu:~/ $ ``` ``` # 還是看得到結束執行的container (joshhuAI)joshhu:~/ $ docker ps -a [1:41:46] CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 93263a70182b ubuntu "uname -a" 37 seconds ago Exited (0) 36 seconds ago amazing_kepler (joshhuAI)joshhu:~/ $ ``` ``` # 背景執行一個vnc圖型介面的Linux (joshhuAI)joshhu:~/ $ docker run -d -p:5910:5900 dorowu/ubuntu-desktop-lxde-vnc [1:44:51] Unable to find image 'dorowu/ubuntu-desktop-lxde-vnc:latest' locally latest: Pulling from dorowu/ubuntu-desktop-lxde-vnc a70d879fa598: Pull complete c4394a92d1f8: Pull complete 10e6159c56c0: Pull complete 6d516dea5dcb: Pull complete 2c326a79b2c1: Pull complete 77c398b95e39: Pull complete 1a5adfd9ff08: Pull complete d8327acdb588: Pull complete b4d11d536f7c: Pull complete 0403b18ddc74: Pull complete c163d8624a71: Pull complete 1dc3570b5fa6: Pull complete 6f9c71cf3486: Pull complete e02f721e36c7: Pull complete ba596a49f65e: Pull complete 31c6cc0bdd8d: Pull complete Digest: sha256:775184eaa5bbc01bc4261a6c9596a642b3b43a433c1508637567bc724575964b Status: Downloaded newer image for dorowu/ubuntu-desktop-lxde-vnc:latest f52a502a310000f1925c85fb5eb407a2deef7156ac1b5feb56ce10580fcc327b ``` ``` (joshhuAI)joshhu:~/ $ docker ps [1:49:00] CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES f52a502a3100 dorowu/ubuntu-desktop-lxde-vnc "/startup.sh" 3 minutes ago Up 3 minutes (healthy) 80/tcp, 0.0.0.0:5910->5900/tcp, :::5910->5900/tcp strange_nightingale (joshhuAI)joshhu:~/ $ ``` ![](https://i.imgur.com/vWR5IRh.png) ![](https://i.imgur.com/axVVe0q.png) **範例2:`docker create <OPTIONS> <image>`** 啟動但不執行。 ``` (joshhuAI)joshhu:~/ $ docker create test-php-site [1:55:12] 5a56773b0b89afbd4562abad537f539b66a38f6f44ccac178a5535605745693f (joshhuAI)joshhu:~/ $ docker ps [1:55:20] CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES (joshhuAI)joshhu:~/ $ docker ps -a [1:55:22] CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 5a56773b0b89 test-php-site "docker-php-entrypoi…" 4 seconds ago Created exciting_neumann (joshhuAI)joshhu:~/ $ ``` **範例3:`docker ps <OPTIONS>`** 列出執行中的容器。 ``` (joshhuAI)joshhu:~/ $ docker run ubuntu ls -l [2:11:00] total 48 lrwxrwxrwx 1 root root 7 Oct 6 16:47 bin -> usr/bin drwxr-xr-x 2 root root 4096 Apr 15 2020 boot drwxr-xr-x 5 root root 340 Oct 30 18:11 dev drwxr-xr-x 1 root root 4096 Oct 30 18:11 etc drwxr-xr-x 2 root root 4096 Apr 15 2020 home lrwxrwxrwx 1 root root 7 Oct 6 16:47 lib -> usr/lib lrwxrwxrwx 1 root root 9 Oct 6 16:47 lib32 -> usr/lib32 lrwxrwxrwx 1 root root 9 Oct 6 16:47 lib64 -> usr/lib64 lrwxrwxrwx 1 root root 10 Oct 6 16:47 libx32 -> usr/libx32 drwxr-xr-x 2 root root 4096 Oct 6 16:47 media drwxr-xr-x 2 root root 4096 Oct 6 16:47 mnt drwxr-xr-x 2 root root 4096 Oct 6 16:47 opt dr-xr-xr-x 486 root root 0 Oct 30 18:11 proc drwx------ 2 root root 4096 Oct 6 16:58 root drwxr-xr-x 5 root root 4096 Oct 6 16:58 run lrwxrwxrwx 1 root root 8 Oct 6 16:47 sbin -> usr/sbin drwxr-xr-x 2 root root 4096 Oct 6 16:47 srv dr-xr-xr-x 13 root root 0 Oct 30 15:38 sys drwxrwxrwt 2 root root 4096 Oct 6 16:58 tmp drwxr-xr-x 13 root root 4096 Oct 6 16:47 usr drwxr-xr-x 11 root root 4096 Oct 6 16:58 var (joshhuAI)joshhu:~/ $ docker ps [2:11:21] CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES (joshhuAI)joshhu:~/ $ docker ps -a [2:11:25] CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES b5b03846b1ab ubuntu "ls -l" 11 seconds ago Exited (0) 10 seconds ago peaceful_williams ``` ```shell (joshhuAI)joshhu:~/ $ docker run -d test-php-site [2:11:40] 69b50714fdc8b62f05b08f883e148ac6ee21d0ede2e6c88e875f91f9007184de (joshhuAI)joshhu:~/ $ docker ps [2:12:36] CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 69b50714fdc8 test-php-site "docker-php-entrypoi…" 3 seconds ago Up 2 seconds 80/tcp laughing_rosalind (joshhuAI)joshhu:~/ $ ``` ``` (joshhuAI)joshhu:~/ $ docker ps -a [2:34:08] CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 69b50714fdc8 test-php-site "docker-php-entrypoi…" 23 minutes ago Up 23 minutes 80/tcp laughing_rosalind b5b03846b1ab ubuntu "ls -l" 25 minutes ago Exited (0) 25 minutes ago peaceful_williams ``` **範例4:`docker kill <OPTIONS> <容器>`** 刪除容器,但並沒有真正從記憶體中移除。 ``` (joshhuAI)joshhu:~/ $ docker run -d tutum/apache-php [16:46:20] 73768685be31d1865b5acf2f50a2b57708dd91f0b3df5efae1c1ce2251dc2fbe (joshhuAI)joshhu:~/ $ docker ps [16:46:26] CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 73768685be31 tutum/apache-php "/run.sh" 3 seconds ago Up 2 seconds 80/tcp interesting_sanderson (joshhuAI)joshhu:~/ $ docker kill 7 [16:46:28] 7 (joshhuAI)joshhu:~/ $ docker ps -a [16:46:32] CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 73768685be31 tutum/apache-php "/run.sh" 13 seconds ago Exited (137) 6 seconds ago interesting_sanderson b03283087cb9 tutum/apache-php "/run.sh" 2 hours ago Exited (0) 9 minutes ago funny_beaver a9371a753f86 php "docker-php-entrypoi…" 2 hours ago Exited (0) 2 hours ago distracted_jackson ``` **範例5:`docker rm <OPTIONS> <容器>`** 刪除容器,並且真正從記憶體中移除,刪除執行中容器需加參數`-f`。 ``` (joshhuAI)joshhu:~/ $ docker ps -a [16:48:50] CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES (joshhuAI)joshhu:~/ $ docker run -d tutum/apache-php [16:48:52] fa7bd73251d786c7218c83c42bc3b90e26e701bc801cc97bf0b476e4279964fc (joshhuAI)joshhu:~/ $ docker ps [16:48:56] CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES fa7bd73251d7 tutum/apache-php "/run.sh" 8 seconds ago Up 7 seconds 80/tcp ecstatic_almeida (joshhuAI)joshhu:~/ $ docker rm -f fa7bd73251d7 [16:49:03] fa7bd73251d7 (joshhuAI)joshhu:~/ $ docker ps -a [16:49:10] CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES (joshhuAI)joshhu:~/ $ ``` **範例6:`docker stop/start <OPTIONS> <容器>`** 停止或啟動容器。 ``` (joshhuAI)joshhu:~/ $ docker run -d tutum/apache-php [16:54:27] 24c962f563088488ec97b9bd72115a4badeb1f23a301e48d6d22556a8694d9c0 (joshhuAI)joshhu:~/ $ docker ps [16:54:31] CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 24c962f56308 tutum/apache-php "/run.sh" 2 seconds ago Up 1 second 80/tcp nervous_ride (joshhuAI)joshhu:~/ $ docker stop 24c962f56308 [16:54:32] 24c962f56308 (joshhuAI)joshhu:~/ $ docker ps [16:54:40] CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES (joshhuAI)joshhu:~/ $ docker ps -a [16:54:42] CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 24c962f56308 tutum/apache-php "/run.sh" 13 seconds ago Exited (0) 3 seconds ago nervous_ride (joshhuAI)joshhu:~/ $ docker start 24c962f56308 [16:54:43] 24c962f56308 (joshhuAI)joshhu:~/ $ docker ps [16:54:48] CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 24c962f56308 tutum/apache-php "/run.sh" 20 seconds ago Up 1 second 80/tcp nervous_ride (joshhuAI)joshhu:~/ $ ``` **範例7:`docker pause/unpause <容器>`** 暫停/啟動容器。 ``` (joshhuAI)joshhu:~/ $ docker run -d tutum/apache-php [16:55:27] 9f21bea1089fdd5d92ce22a240a3cf995db7f13622f1e8ccd372a6e6b89c71f7 (joshhuAI)joshhu:~/ $ docker pause 9f [16:55:31] 9f (joshhuAI)joshhu:~/ $ docker ps [16:55:36] CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 9f21bea1089f tutum/apache-php "/run.sh" 8 seconds ago Up 7 seconds (Paused) 80/tcp zealous_allen (joshhuAI)joshhu:~/ $ docker unpause 9f [16:55:38] 9f (joshhuAI)joshhu:~/ $ docker ps [16:55:48] CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 9f21bea1089f tutum/apache-php "/run.sh" 20 seconds ago Up 19 seconds 80/tcp zealous_allen (joshhuAI)joshhu:~/ $ ``` **範例8:`docker attach <容器>`** 將螢幕鍵盤連接到一個執行中的容器。 ``` (joshhuAI)joshhu:~/ $ docker run -d -p 80:80 tutum/apache-php [0:02:44] 6ffad8c2b687b8cdf905c9794697586d59adefe47f4aaad96d2c7c4e8c756805 (joshhuAI)joshhu:~/ $ docker attach 6ffad8c2b687 [0:02:56] ==> /var/log/apache2/access.log <== ==> /var/log/apache2/error.log <== ==> /var/log/apache2/access.log <== ==> /var/log/apache2/error.log <== ``` **範例9:`docker cp <file> <容器>`** 在主系統和容器間複製資料,雙向。 ``` (joshhuAI)joshhu:~/ $ docker run -d -p 80:80 tutum/apache-php [0:04:01] 3ed316b16a9899c8d9143b16376bc9d2ff2f7178cfe294c0281728d5fc13b21e (joshhuAI)joshhu:~/ $ ll [0:04:05] total 163M drwxrwxr-x 26 joshhu joshhu 4.0K 1月 4 2020 anaconda3 lrwxrwxrwx 1 joshhu joshhu 10 7月 29 2020 Backup -> /mnt/disk2 -rw------- 1 joshhu joshhu 1.5M 10月 30 15:13 busybox.tar drwxrwxr-x 4 joshhu joshhu 4.0K 11月 16 2020 cuda drwxrwxr-x 2 joshhu joshhu 4.0K 1月 25 2021 deepmind drwxr-xr-x 2 joshhu joshhu 4.0K 1月 26 2021 Desktop drwxr-xr-x 5 joshhu joshhu 4.0K 9月 22 14:40 Downloads -rw-r--r-- 1 joshhu joshhu 8.8K 11月 20 2019 examples.desktop drwxrwxr-x 4 joshhu joshhu 4.0K 9月 26 02:20 mnt drwxrwxr-x 3 joshhu joshhu 4.0K 9月 21 16:55 PhoenixMiner_5.6d_Linux drwxr-xr-x 2 joshhu joshhu 4.0K 10月 26 16:49 Pictures drwx------ 4 joshhu joshhu 4.0K 2月 6 2020 snap drwxr-xr-x 3 joshhu joshhu 4.0K 2月 18 2021 Videos drwx------ 11 joshhu joshhu 4.0K 8月 16 21:34 VirtualBox VMs drwxr-xr-x 9 joshhu joshhu 4.0K 9月 22 19:46 VMs -rw-rw-r-- 1 joshhu joshhu 161M 10月 11 18:40 wikititle.txt drwxrwxr-x 30 joshhu joshhu 4.0K 10月 19 15:12 workspace drwxr-xr-x 4 joshhu joshhu 4.0K 8月 20 00:36 xiaomi -rw-r--r-- 1 joshhu joshhu 3.7K 10月 7 2020 zshrc.general (joshhuAI)joshhu:~/ $ docker cp zshrc.general 3e:/ [0:04:09] (joshhuAI)joshhu:~/ $ docker exec -it 3e ls / [0:05:10] app boot etc lib media opt root run.sh srv tmp var bin dev home lib64 mnt proc run sbin sys usr zshrc.general (joshhuAI)joshhu:~/ $ ``` **範例10:`docker exec <容器> <指令>`** 在容器中執行某個指令。 ``` # 進入執行中容器 (joshhuAI)joshhu:~/ $ docker ps [0:08:27] CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 3ed316b16a98 tutum/apache-php "/run.sh" 4 minutes ago Up 4 minutes 0.0.0.0:80->80/tcp, :::80->80/tcp frosty_goldstine (joshhuAI)joshhu:~/ $ docker exec -it 3e bash [0:08:29] root@3ed316b16a98:/app# ``` ``` # 在主機上執行容器中的指令,不進入容器 (joshhuAI)joshhu:~/ $ docker exec -it 3e df -h [0:09:04] Filesystem Size Used Avail Use% Mounted on overlay 938G 596G 342G 64% / tmpfs 64M 0 64M 0% /dev tmpfs 32G 0 32G 0% /sys/fs/cgroup shm 64M 0 64M 0% /dev/shm /dev/nvme0n1p2 938G 596G 342G 64% /etc/hosts tmpfs 32G 0 32G 0% /proc/asound tmpfs 32G 0 32G 0% /proc/acpi tmpfs 64M 0 64M 0% /proc/kcore tmpfs 64M 0 64M 0% /proc/keys tmpfs 64M 0 64M 0% /proc/timer_list tmpfs 64M 0 64M 0% /proc/sched_debug tmpfs 32G 0 32G 0% /proc/scsi tmpfs 32G 0 32G 0% /sys/firmware (joshhuAI)joshhu:~/ $ ``` **範例11:`docker export <容器> <指令>`** 將執行中容器檔案系統輸出到`tar`檔案。 ``` (joshhuAI)joshhu:~/ $ docker export -o test.tar 3ed316b16a98 [0:11:48] (joshhuAI)joshhu:~/ $ ls -l [0:11:51] total 391548 drwxrwxr-x 26 joshhu joshhu 4096 1月 4 2020 anaconda3 lrwxrwxrwx 1 joshhu joshhu 10 7月 29 2020 Backup -> /mnt/disk2 -rw------- 1 joshhu joshhu 1468416 10月 30 15:13 busybox.tar drwxrwxr-x 4 joshhu joshhu 4096 11月 16 2020 cuda drwxrwxr-x 2 joshhu joshhu 4096 1月 25 2021 deepmind drwxr-xr-x 2 joshhu joshhu 4096 1月 26 2021 Desktop drwxr-xr-x 5 joshhu joshhu 4096 9月 22 14:40 Downloads -rw-r--r-- 1 joshhu joshhu 8980 11月 20 2019 examples.desktop drwxrwxr-x 4 joshhu joshhu 4096 9月 26 02:20 mnt drwxrwxr-x 3 joshhu joshhu 4096 9月 21 16:55 PhoenixMiner_5.6d_Linux drwxr-xr-x 2 joshhu joshhu 4096 10月 26 16:49 Pictures drwx------ 4 joshhu joshhu 4096 2月 6 2020 snap -rw------- 1 joshhu joshhu 230716416 11月 1 00:11 test.tar drwxr-xr-x 3 joshhu joshhu 4096 2月 18 2021 Videos drwx------ 11 joshhu joshhu 4096 8月 16 21:34 VirtualBox VMs drwxr-xr-x 9 joshhu joshhu 4096 9月 22 19:46 VMs -rw-rw-r-- 1 joshhu joshhu 168680603 10月 11 18:40 wikititle.txt drwxrwxr-x 30 joshhu joshhu 4096 10月 19 15:12 workspace drwxr-xr-x 4 joshhu joshhu 4096 8月 20 00:36 xiaomi -rw-r--r-- 1 joshhu joshhu 3748 10月 7 2020 zshrc.general (joshhuAI)joshhu:~/ $ ``` **範例12:`docker logs <容器>`** 記錄執行中容器的記錄檔。 ``` (joshhuAI)joshhu:~/ $ docker ps [0:12:30] CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 3ed316b16a98 tutum/apache-php "/run.sh" 8 minutes ago Up 8 minutes 0.0.0.0:80->80/tcp, :::80->80/tcp frosty_goldstine (joshhuAI)joshhu:~/ $ docker logs 3e [0:12:45] ==> /var/log/apache2/access.log <== ==> /var/log/apache2/error.log <== ==> /var/log/apache2/other_vhosts_access.log <== tail: unrecognized file system type 0x794c7630 for '/var/log/apache2/access.log'. please report this to bug-coreutils@gnu.org. reverting to polling tail: unrecognized file system type 0x794c7630 for '/var/log/apache2/error.log'. please report this to bug-coreutils@gnu.org. reverting to polling tail: unrecognized file system type 0x794c7630 for '/var/log/apache2/other_vhosts_access.log'. please report this to bug-coreutils@gnu.org. reverting to polling ==> /var/log/apache2/error.log <== (joshhuAI)joshhu:~/ $ ``` **範例13:`docker port <容器>`** 列出執行中容器對應到主機的通訊埠。 ``` (joshhuAI)joshhu:~/ $ docker run -d -p 8080:80 tutum/apache-php [0:13:58] 02e73923f36b76b92194a3b62f59018d2695e5195a8f8b2160ac7b2d7dc03a7b d% (joshhuAI)joshhu:~/ $ docker port 02e73923f36b [0:14:05] 80/tcp -> 0.0.0.0:8080 80/tcp -> :::8080 (joshhuAI)joshhu:~/ $ ``` **範例14:`docker stats <容器>`** 顯示容器的資源使用資料。 ``` docker stats 02 CONTAINER ID NAME CPU % MEM USAGE / LIMIT MEM % NET I/O BLOCK I/O PIDS 02e73923f36b practical_keldysh 0.01% 19.64MiB / 62.83GiB 0.03% 3.92kB / 0B 0B / 20.5kB 7 ``` **範例15:`docker top <容器>`** 顯示執行中容器的`top`資訊。 ``` (joshhuAI)joshhu:~/ $ docker top 02e73923f36b [0:16:46] UID PID PPID C STIME TTY TIME CMD root 100445 100407 0 00:14 ? 00:00:00 apache2 -D FOREGROUND root 100516 100445 0 00:14 ? 00:00:00 tail -F /var/log/apache2/access.log /var/log/apache2/error.log /var/log/apache2/other_vhosts_access.log www-data 100520 100445 0 00:14 ? 00:00:00 apache2 -D FOREGROUND www-data 100521 100445 0 00:14 ? 00:00:00 apache2 -D FOREGROUND www-data 100522 100445 0 00:14 ? 00:00:00 apache2 -D FOREGROUND www-data 100524 100445 0 00:14 ? 00:00:00 apache2 -D FOREGROUND www-data 100525 100445 0 00:14 ? 00:00:00 apache2 -D FOREGROUND (joshhuAI)joshhu:~/ $ ``` ###### tags: `docker`, `容器`, `container`