attachnode
將本地終端的輸入, 輸出 以及 錯誤流 鏈接到容器上, (就是登陸到容器中去操做)linux
// 使用 centos:7 鏡像, 運行容器 docker run --name C7 -itd centos:7 /bin/sh 29a79dff571b58ce84a90e8ea28935a6a3c35ab707c7e48eae14b49d8ce19347 // 查看運行容器 docker container ls CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 29a79dff571b centos:7 "/bin/sh" 2 minutes ago Up 2 minutes C7 // 使用 attach 登陸 C7 操做; 退出操做的話,容器會終止 docker attach C7 sh-4.2# ls anaconda-post.log bin dev etc home lib lib64 media mnt opt proc root run sbin srv sys tmp usr var sh-4.2# ps PID TTY TIME CMD 1 pts/0 00:00:00 sh 7 pts/0 00:00:00 ps
buildgit
// docker build --help 查看幫助 // 首先建立目錄, 使用 Dockerfile 建立一個鏡像 mkdir /image_f cd /image_f vim Dockerfile // Dockerfile簡單使用 FROM centos:7 LABEL maintainer="Tian tzhr1225@163.com" RUN mkdir -pv /data/tian && \ echo "This is a test" > /data/tian/test // 使用 docker build 經過 Dockerfile 文件構建新的鏡像 docker build -t test:1 ./ Sending build context to Docker daemon 2.048kB Step 1/3 : FROM centos:7 ---> 9f38484d220f Step 2/3 : LABEL maintainer="Tian tzhr1225@163.com" ---> Running in 0f0f45d5003c Removing intermediate container 0f0f45d5003c ---> 242e247091b1 Step 3/3 : RUN mkdir -pv /data/tian && echo "This is a test" > /data/tian/test ---> Running in 9643e013f948 mkdir: created directory '/data' mkdir: created directory '/data/tian' Removing intermediate container 9643e013f948 ---> af654e159686 Successfully built af654e159686 Successfully tagged test:1 // 查看構建的鏡像 docker image ls REPOSITORY TAG IMAGE ID CREATED SIZE test 1 af654e159686 31 seconds ago 202MB // 運行鏡像, 查看建立的目錄等 docker run --name test --rm -it test:1 [root@ed21b42319e7 ~]# cat /data/tian/test This is a test
commit -- 將一個容器打包成另外一個鏡像
github
適用與在一個鏡像容器中操做(安裝或卸載服務等), 將操做後的容器再構建成一個鏡像, 以便有需求直接使用redis
// 運行 test 容器中, 默認沒有 vim; 使用 yum 安裝 vim [root@localhost ~]# docker run --name test -it test:1 [root@3c43c355e9d7 /]# yum -y install vim // 複製上面的 ID; 退出 test 容器, 使用 docker commit 建立新的鏡像 [root@3c43c355e9d7 /]# exit exit [root@localhost ~]# docker commit 3c43c355e9d7 test:2.0 // 運行 test:2.0 , 直接使用存在 vim 命令 [root@localhost ~]# docker run --name test2 --rm -it test:2.0 [root@42303df50598 /]# vim ... ... ~ VIM - Vi IMproved ... ...
cpdocker
// 查看到沒有運行的容器 test, 啓動test [root@localhost ~]# docker container ls -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 3c43c355e9d7 test:1 "/bin/bash" 8 minutes ago Exited (0) 6 minutes ago test [root@localhost ~]# docker start test test // 將 容器中的 /data/tian 中的 test 文件 複製到本地 [root@localhost ~]# docker cp test:/data/tian/test ./ [root@localhost ~]# cat test This is a test // 將本地 Dockerfile 文件上傳到容器中 [root@localhost ~]# docker cp /image_f/Dockerfile test:/data/tian/ [root@localhost ~]# docker exec test cat /data/tian/Dockerfile FROM centos:7 LABEL maintainer="Tian tzhr1225@163.com" RUN mkdir -pv /data/tian && \ echo "This is a test" > /data/tian/test
createjson
// 建立一個容器, 可是不啓動它, 用法 docker create --help 查看, 參數用法同 docker run 差很少 // 建立 centos:6 [root@localhost ~]# docker create centos:6 // 查看容器建立, centos 6 有, 可是沒有運行; [root@localhost ~]# docker container ls -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 6243c7c92ac1 centos:6 "/bin/bash" 2 minutes ago Created gracious_lehmann
diffvim
// 使用 centos:6 鏡像啓動 c6 容器, 並使用 yum 安裝 vim; [root@localhost ~]# docker run --name c6 --rm -it centos:6 [root@d849adb301f4 /]# yum -y install vim // 另起一個終端 使用 docker diff 查看 c6 容器; C 表示建立, A 表示追加,修改; [root@localhost ~]# docker diff c6 ... ... A /var/lib/yum/yumdb/v/a8ff3e5026c514ae782a731398908b8322561b1c-vim-enhanced-7.4.629-5.el6_8.1-x86_64/from_repo_timestamp C /var/lib/rpm ... ...
eventscentos
// 直接使用 docker events 就能夠動態查看記錄容器的操做 [root@localhost ~]# docker events // 另起一個終端, exit 中止一個容器, 並刪除上面 建立 的容器, [root@d849adb301f4 /]# exit [root@localhost ~]# [root@localhost ~]# docker container ls -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 6243c7c92ac1 centos:6 "/bin/bash" 29 hours ago Exited (0) 10 minutes ago gracious_lehmann [root@localhost ~]# docker container rm 624 624 // 查看 docker events [root@localhost ~]# docker events 2019-06-18T04:46:37.623915823-04:00 container die d849adb301f45670a08ab9f90e20e36df4a9d8159bef3fd02af977d8dcc560c3 (exitCode=0, image=centos:6, name=c6, org.label-schema.build-date=20181006, org.label-schema.license=GPLv2, org.label-schema.name=CentOS Base Image, org.label-schema.schema-version=1.0, org.label-schema.vendor=CentOS) 2019-06-18T04:46:37.712754544-04:00 network disconnect dd54f129406eb1e595af66fc0abe6a6b1cba8d0c69bff35a4a1eba549e3fb820 (container=d849adb301f45670a08ab9f90e20e36df4a9d8159bef3fd02af977d8dcc560c3, name=bridge, type=bridge) 2019-06-18T04:47:09.282202462-04:00 container destroy 6243c7c92ac17aaa0dd026be829323dfb8521e8349d1e727184f4e91b5259cd8 (image=centos:6, name=gracious_lehmann, org.label-schema.build-date=20181006, org.label-schema.license=GPLv2, org.label-schema.name=CentOS Base Image, org.label-schema.schema-version=1.0, org.label-schema.vendor=CentOS)
execbash
// 使用 centos:7 鏡像起一個容器 [root@localhost ~]# docker run --name C7 --rm -itd centos:7 a3e536afc59255910c818ffdc4d81b6afa9cdde1b1a49803a3997e694b685d13 // 使用 exec 執行命令 [root@localhost ~]# docker exec C7 hostname a3e536afc592 // 使用 exec 交互式執行 bash; 與 attach 的區別是這個執行完退出後容器不終止; [root@localhost ~]# docker exec -it C7 /bin/bash [root@a3e536afc592 /]# hostname a3e536afc592 [root@a3e536afc592 /]# w 08:58:58 up 51 days, 8 min, 0 users, load average: 0.00, 0.01, 0.05 USER TTY FROM LOGIN@ IDLE JCPU PCPU WHAT // 使用exit 退出容器, 再查看容器還在運行 [root@a3e536afc592 /]# exit exit [root@localhost ~]# docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES a3e536afc592 centos:7 "/bin/bash" 4 minutes ago Up 4 minutes C7
export
// 兩種方式導出爲 tar 文件 [root@localhost ~]# docker export C7 -o centos7_1.tar [root@localhost ~]# docker export C7 > centos7_2.tar // 兩種方式權限不一樣, -o 600 ; > 644; [root@localhost ~]# ll centos7_* -rw------- 1 root root 209489920 Jun 18 05:05 centos7_1.tar -rw-r--r-- 1 root root 209489920 Jun 18 05:05 centos7_2.tar
import
// 經常使用於開發/測試對容器中進行修改後, 使用 export 生成 tar 包, 再恢復的使用使用 import [root@localhost ~]# docker import centos7_1.tar centos:7-t sha256:626dda4747388dc14eac5920afd23b3d2109a3c81a718e68ee96509bd1fe3e34 // 查看 鏡像 [root@localhost ~]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE centos 7-t 626dda474738 About a minute ago 202MB ... ... // 運行一個容器 [root@localhost ~]# docker run --name C_7-n -it --rm centos:7-t /bin/bash
history
// 查看 centos:6 的歷史記錄, 等同於 docker image history [root@localhost ~]# docker history centos:6 IMAGE CREATED CREATED BY SIZE COMMENT d0957ffdf8a2 3 months ago /bin/sh -c #(nop) CMD ["/bin/bash"] 0B <missing> 3 months ago /bin/sh -c #(nop) LABEL org.label-schema.sc… 0B <missing> 3 months ago /bin/sh -c #(nop) ADD file:0065316a41144e95b… 194MB <missing> 8 months ago /bin/sh -c #(nop) MAINTAINER https://github… 0B
images
// 查看系統中的鏡像, 至關於 docker image ls [root@localhost ~]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE busybox latest 64f5d945efcc 5 weeks ago 1.2MB tian/httpd v.1.0 4c1ef9008f84 7 weeks ago 1.2MB 192.168.9.30:5000/httpd v.2.0 4f14d7a10dc3 7 weeks ago 1.2MB tian/httpd v.2.0 4f14d7a10dc3 7 weeks ago 1.2MB centos 6 d0957ffdf8a2 3 months ago 194MB centos 7 9f38484d220f 3 months ago 202MB hello-world latest fce289e99eb9 5 months ago 1.84kB
info
// 查看 docker 系統中的全部容器信息 [root@localhost ~]# docker info Containers: 1 Running: 1 Paused: 0 Stopped: 0 Images: 7 Server Version: 18.09.6 Storage Driver: overlay2 Backing Filesystem: xfs Supports d_type: true Native Overlay Diff: true Logging Driver: json-file Cgroup Driver: cgroupfs Plugins: Volume: local Network: bridge host macvlan null overlay Log: awslogs fluentd gcplogs gelf journald json-file local logentries splunk syslog Swarm: inactive Runtimes: runc Default Runtime: runc Init Binary: docker-init containerd version: bb71b10fd8f58240ca47fbb579b9d1028eea7c84 runc version: 2b18fe1d885ee5083ef9f0838fee39b62d653e30 init version: fec3683 Security Options: seccomp Profile: default Kernel Version: 3.10.0-693.el7.x86_64 Operating System: CentOS Linux 7 (Core) OSType: linux Architecture: x86_64 CPUs: 2 Total Memory: 1.796GiB Name: localhost.localdomain ID: BJU6:VTUC:HA23:LARR:VULF:H2LR:4UNY:KGRP:2OL3:G5GS:77KA:ZGCZ Docker Root Dir: /var/lib/docker Debug Mode (client): false Debug Mode (server): false Registry: https://index.docker.io/v1/ Labels: Experimental: false Insecure Registries: 192.168.9.30:5000 127.0.0.0/8 Registry Mirrors: https://registry.docker-cn.com/ Live Restore Enabled: false Product License: Community Engine
inspect
// 查看鏡像的信息 *** 調試中使用的會比較多 *** [root@localhost ~]# docker inspect hello-world:latest [ { "Id": "sha256:fce289e99eb9bca977dae136fbe2a82b6b7d4c372474c9235adc1741675f587e", "RepoTags": [ "hello-world:latest" ], "RepoDigests": [ "hello-world@sha256:41a65640635299bab090f783209c1e3a3f11934cf7756b09cb2f1e02147c6ed8" ], "Parent": "", "Comment": "", "Created": "2019-01-01T01:29:27.650294696Z", "Container": "8e2caa5a514bb6d8b4f2a2553e9067498d261a0fd83a96aeaaf303943dff6ff9", "ContainerConfig": { "Hostname": "8e2caa5a514b", "Domainname": "", ... ... // 查看容器的詳細信息 [root@localhost ~]# docker inspect C7 [ { "Id": "a3e536afc59255910c818ffdc4d81b6afa9cdde1b1a49803a3997e694b685d13", "Created": "2019-06-18T08:56:49.557911196Z", "Path": "/bin/bash", "Args": [], "State": { "Status": "running", "Running": true, "Paused": false, "Restarting": false, "OOMKilled": false, "Dead": false, "Pid": 20501, "ExitCode": 0, "Error": "", "StartedAt": "2019-06-18T08:56:50.052990757Z", "FinishedAt": "0001-01-01T00:00:00Z" }, ... ... // 查看指定對應的值 [root@localhost ~]# docker inspect hello-world:latest -f '{{.ContainerConfig.Hostname}}' 8e2caa5a514b
kill
[root@localhost ~]# docker container ls CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES a3e536afc592 centos:7 "/bin/bash" 21 hours ago Up 21 hours C7 [root@localhost ~]# docker kill C7 C7 [root@localhost ~]# docker container ls CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
save
// 兩種方式, 生成的 tar 包 權限不一樣 [root@localhost ~]# docker image ls REPOSITORY TAG IMAGE ID CREATED SIZE centos 7-t 626dda474738 3 hours ago 202MB busybox latest 64f5d945efcc 5 weeks ago 1.2MB tian/httpd v.1.0 4c1ef9008f84 7 weeks ago 1.2MB 192.168.9.30:5000/httpd v.2.0 4f14d7a10dc3 7 weeks ago 1.2MB tian/httpd v.2.0 4f14d7a10dc3 7 weeks ago 1.2MB centos 6 d0957ffdf8a2 3 months ago 194MB centos 7 9f38484d220f 3 months ago 202MB hello-world latest fce289e99eb9 5 months ago 1.84kB [root@localhost ~]# docker save busybox:latest -o busybox.tar [root@localhost ~]# docker save hello-world:latest > hello.tar [root@localhost ~]# ll busybox.tar hello.tar -rw------- 1 root root 1424896 Jun 19 02:47 busybox.tar -rw-r--r-- 1 root root 12800 Jun 19 02:48 hello.tar
load
// 須要使用 -i 的參數指定 tar 文件, 不然將從標準輸入中尋找 [root@localhost ~]# docker load -i busybox.tar Loaded image: busybox:latest
login
// -u 用戶名 -p 密碼, 後面不跟網址默認是 docker hub 的鏡像倉庫; 要是別的須要跟上指定的網址 [root@localhost ~]# docker login -u tzhr -p xxxx WARNING! Using --password via the CLI is insecure. Use --password-stdin. WARNING! Your password will be stored unencrypted in /root/.docker/config.json. Configure a credential helper to remove this warning. See https://docs.docker.com/engine/reference/commandline/login/#credentials-store Login Succeeded
logout
// 若登陸的是 docker hub 鏡像倉庫, 直接使用 docker logout [root@localhost ~]# docker logout Removing login credentials for https://index.docker.io/v1/ // 如果登陸的是 私有的 鏡像倉庫, 在後面跟上網址
logs
// 查看容器的操做日誌 [root@localhost ~]# docker logs C7 // 能夠指定只顯示最後十行 [root@localhost ~]# docker logs --tail 10 C7 // 動態實時輸出 [root@localhost ~]# docker logs -f C7
pause unpause
// 暫停 C7 容器 應用場景: 有的容器啓動快了.. 須要暫停, 等候一下其餘的環境/容器的啓動加載; [root@localhost ~]# docker pause C7 C7 [root@localhost ~]# docker container ls -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 3f442c7c1e7e centos:7 "/bin/bash" 9 minutes ago Up 9 minutes (Paused) C7 // 取消暫停 [root@localhost ~]# docker unpause C7 C7 [root@localhost ~]# docker container ls -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 3f442c7c1e7e centos:7 "/bin/bash" 10 minutes ago Up 10 minutes
port
// 運行 C6 容器, 映射 80 端口; [root@localhost ~]# docker run --name C6 --rm -itd -p 80 centos:6 4ba4536302a7664eefecdce7660bb9aa2affad79fae7fc01782d5e8394a08ce8 [root@localhost ~]# docker port C6 80/tcp -> 0.0.0.0:32769
ps
// 默認查看運行的容器 [root@localhost ~]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 3f442c7c1e7e centos:7 "/bin/bash" 21 minutes ago Up 21 minutes C7 // 使用 ls -a 查看全部的 容器 [root@localhost ~]# docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES d7e6fc22cfa4 centos:6 "/bin/bash" 5 seconds ago Exited (0) 3 seconds ago C6 3f442c7c1e7e centos:7 "/bin/bash" 21 minutes ago Up 21 minutes C7 // 使用 docker ps -l 查看最近的一個容器, 不管運行與不運行 [root@localhost ~]# docker ps -l CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES d7e6fc22cfa4 centos:6 "/bin/bash" 5 minutes ago Exited (0) 5 minutes ago C6
pull push
// 下拉鏡像, 後面直接指定鏡像的名稱 ; 最後使用 -a 參數表示拉取全部的 tag; [root@localhost ~]# docker pull hello-world Using default tag: latest latest: Pulling from library/hello-world 1b930d010525: Pull complete Digest: sha256:41a65640635299bab090f783209c1e3a3f11934cf7756b09cb2f1e02147c6ed8 Status: Downloaded newer image for hello-world:latest // push 推送的話, 須要先登陸, 而後push docker push 地址/鏡像:tag
rename
[root@localhost ~]# docker container ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 3f442c7c1e7e centos:7 "/bin/bash" 4 days ago Up 4 days C7 // 將 C7 改名爲 Centos_7 [root@localhost ~]# docker rename C7 Centos_7 [root@localhost ~]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 3f442c7c1e7e centos:7 "/bin/bash" 4 days ago Up 4 days Centos_7
restart
[root@localhost ~]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 717aac1e0b79 centos:6 "/bin/bash" 3 seconds ago Up 2 seconds Centos_6 3f442c7c1e7e centos:7 "/bin/bash" 4 days ago Up 4 days Centos_7 // 重啓 Centos_6 和 Centos_7 容器; 可使用 -t 參數指定多長時間, 默認 10 秒; [root@localhost ~]# docker restart Centos_6 Centos_7 Centos_6 Centos_7
rm
// 查看全部的容器 [root@localhost ~]# docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 717aac1e0b79 centos:6 "/bin/bash" 10 minutes ago Up 10 minutes Centos_6 d7e6fc22cfa4 centos:6 "/bin/bash" 4 days ago Exited (0) 4 days ago C6 3f442c7c1e7e centos:7 "/bin/bash" 4 days ago Up 6 minutes Centos_7 // 刪除沒有運行的容器, 可使用容器名, 也可使用 ID , 這裏使用 ID 的前兩位(簡寫,匹配) [root@localhost ~]# docker rm d7 d7 // 刪除運行的容器報錯 [root@localhost ~]# docker rm 71 Error response from daemon: You cannot remove a running container 717aac1e0b7967120db07aa15d91a8be4c8efe127eb5206e4b667c9278a82f42. Stop the container before attempting removal or force remove // 使用 -f 參數刪除運行的容器 [root@localhost ~]# docker rm -f 71 71 [root@localhost ~]# docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 3f442c7c1e7e centos:7 "/bin/bash" 4 days ago Up 7 minutes Centos_7
rmi
// 查看鏡像, 容器使用的鏡像是刪不掉的 [root@localhost ~]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE centos 7-t 626dda474738 4 days ago 202MB busybox latest 64f5d945efcc 6 weeks ago 1.2MB tian/httpd v.1.0 4c1ef9008f84 7 weeks ago 1.2MB 192.168.9.30:5000/httpd v.2.0 4f14d7a10dc3 8 weeks ago 1.2MB tian/httpd v.2.0 4f14d7a10dc3 8 weeks ago 1.2MB centos 6 d0957ffdf8a2 3 months ago 194MB centos 7 9f38484d220f 3 months ago 202MB hello-world latest fce289e99eb9 5 months ago 1.84kB // 能夠跟 鏡像:標籤 也能夠跟 ID; 使用 -f 能夠強制刪除一個鏡像; [root@localhost ~]# docker rmi centos:7-t 4c Untagged: centos:7-t Deleted: sha256:626dda4747388dc14eac5920afd23b3d2109a3c81a718e68ee96509bd1fe3e34 Deleted: sha256:0b685c6e322d224524d9c7baf4dcb12991020faee6530cf6b1898ddb70c70b80 Untagged: tian/httpd:v.1.0 Deleted: sha256:4c1ef9008f84aae233d73222d38e71c3156dfce8a4515cb9e2c951d9706bcabc Deleted: sha256:b4f291dd94aa6ef25df0272b1c7bb5da99e675e4ef19fa94fe719d46491ed6e3
run
參數有不少, 能夠指定 主機名 內存 CPU 等; 使用 docker run --help 查看使用幫助;;
// --name 定義容器名, --rm 表示中止容器就刪除, -it 交互式, -d 後臺運行; [root@localhost ~]# docker run --name C6 --rm -itd centos:6 dde7d991f1990409e9247ef797267289d06190f138ee83e96a0298afb2d3281f [root@localhost ~]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES dde7d991f199 centos:6 "/bin/bash" 4 seconds ago Up 3 seconds C6 [root@localhost ~]# docker run --name centos_6 --rm -it centos:6 /bin/bash [root@8f2d2fab3d16 /]#
search
// 查找 redis 鏡像 [root@localhost ~]# docker search redis NAME DESCRIPTION STARS OFFICIAL AUTOMATED redis Redis is an open source key-value store that… 7034 [OK] bitnami/redis Bitnami Redis Docker Image 114 [OK] sameersbn/redis 75 ... ... // 過濾出 stars 大於 100 的 ; 過濾使用 -f; [root@localhost ~]# docker search redis -f stars=100 NAME DESCRIPTION STARS OFFICIAL AUTOMATED redis Redis is an open source key-value store that… 7034 [OK] bitnami/redis Bitnami Redis Docker Image 114 [OK] // 過濾出官方的 [root@localhost ~]# docker search redis -f is-official=true NAME DESCRIPTION STARS OFFICIAL AUTOMATED redis Redis is an open source key-value store that… 7034 [OK] // 多個因素過濾 --filter [root@localhost ~]# docker search redis --filter is-official=true --filter stars=100 NAME DESCRIPTION STARS OFFICIAL AUTOMATED redis Redis is an open source key-value store that… 7034 [OK]
stop / start
// 運行一個容器 [root@localhost ~]# docker run --name C6 -itd centos:6 ce5db9dc2f4407f40042fcb71ef1fab6ecc5de8b2f603918a4c7ae098dfd3a7c [root@localhost ~]# docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES ce5db9dc2f44 centos:6 "/bin/bash" 5 seconds ago Up 4 seconds C6 d518ba8150ef centos:7 "/bin/bash" 4 minutes ago Exited (0) About a minute ago friendly_davinci // 中止容器 [root@localhost ~]# docker stop C6 C6 // 查看狀態 [root@localhost ~]# docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES ce5db9dc2f44 centos:6 "/bin/bash" 36 seconds ago Exited (137) 13 seconds ago C6 d518ba8150ef centos:7 "/bin/bash" 5 minutes ago Exited (0) About a minute ago friendly_davinci // 啓動容器 [root@localhost ~]# docker start C6 C6 [root@localhost ~]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES ce5db9dc2f44 centos:6 "/bin/bash" 47 seconds ago Up 3 seconds C6
stats
// 輸出 容器 的資源使用狀況, --no-stream 只輸出一次, -a 參數 輸出全部容器的狀態;; [root@localhost ~]# docker stats C6 CONTAINER ID NAME CPU % MEM USAGE / LIMIT MEM % NET I/O BLOCK I/O PIDS ce5db9dc2f44 C6 0.00% 348KiB / 1.796GiB 0.02% 648B / 0B 0B / 0B 1 CONTAINER ID NAME CPU % MEM USAGE / LIMIT MEM % NET I/O BLOCK I/O PIDS ce5db9dc2f44 C6 0.00% 348KiB / 1.796GiB 0.02% 648B / 0B 0B / 0B 1
... ...
tag
// 經過一個指定的鏡像生成另外一個鏡像, 不指定標籤默認 latest [root@localhost ~]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE busybox latest 64f5d945efcc 6 weeks ago 1.2MB 192.168.9.30:5000/httpd v.2.0 4f14d7a10dc3 8 weeks ago 1.2MB tian/httpd v.2.0 4f14d7a10dc3 8 weeks ago 1.2MB centos 6 d0957ffdf8a2 3 months ago 194MB centos 7 9f38484d220f 3 months ago 202MB hello-world latest fce289e99eb9 5 months ago 1.84kB [root@localhost ~]# docker tag busybox busybox:new // 生成新的鏡像與原鏡像的 ID 號相同; [root@localhost ~]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE busybox latest 64f5d945efcc 6 weeks ago 1.2MB busybox new 64f5d945efcc 6 weeks ago 1.2MB 192.168.9.30:5000/httpd v.2.0 4f14d7a10dc3 8 weeks ago 1.2MB tian/httpd v.2.0 4f14d7a10dc3 8 weeks ago 1.2MB centos 6 d0957ffdf8a2 3 months ago 194MB centos 7 9f38484d220f 3 months ago 202MB hello-world latest fce289e99eb9 5 months ago 1.84kB
top
[root@localhost ~]# docker top C6 UID PID PPID C STIME TTY TIME CMD root 26247 26230 0 21:52 pts/0 00:00:00 /bin/bash
update
// 啓動的使用對容器進行了一些資源的限制, 運行過程當中發現資源不夠用, 可使用指定的參數更新容器的資源 [root@localhost ~]# docker update --help Usage: docker update [OPTIONS] CONTAINER [CONTAINER...] Update configuration of one or more containers 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 --restart string Restart policy to apply when a container exits
version
[root@localhost ~]# docker version Client: Version: 18.09.6 API version: 1.39 Go version: go1.10.8 Git commit: 481bc77156 Built: Sat May 4 02:34:58 2019 OS/Arch: linux/amd64 Experimental: false Server: Docker Engine - Community Engine: Version: 18.09.6 API version: 1.39 (minimum version 1.12) Go version: go1.10.8 Git commit: 481bc77 Built: Sat May 4 02:02:43 2019 OS/Arch: linux/amd64 Experimental: false
wait
[root@localhost ~]# docker run --name C7 -it -d centos:7 72035c965f748c800b432120bb9a87bdb03efe19160a6ccae469bc3cc97e2777 [root@localhost ~]# docker stop C7 C7 [root@localhost ~]# docker wait C7 137
service
// docker service 的用法 [root@localhost ~]# docker service --help Usage: docker service COMMAND Manage services Commands: create Create a new service inspect Display detailed information on one or more services logs Fetch the logs of a service or task ls List services ps List the tasks of one or more services rm Remove one or more services rollback Revert changes to a service's configuration scale Scale one or multiple replicated services // 擴展節點數量 update Update a service // 先查看沒有就service [root@localhost ~]# docker service ls ID NAME MODE REPLICAS IMAGE PORTS // 建立一個service [root@localhost ~]# docker service create --name redis redis:alpine o6paknwk9wcyqd6uvxaimd448 overall progress: 1 out of 1 tasks 1/1: running verify: Service converged [root@localhost ~]# docker service ls ID NAME MODE REPLICAS IMAGE PORTS o6paknwk9wcy redis replicated 1/1 redis:alpine // 擴展 redis , 將replicas 節點擴展爲 3 個; [root@localhost ~]# docker service scale redis=3 redis scaled to 3 overall progress: 3 out of 3 tasks 1/3: running 2/3: running 3/3: running verify: Service converged // 查看 [root@localhost ~]# docker service ls ID NAME MODE REPLICAS IMAGE PORTS o6paknwk9wcy redis replicated 3/3 redis:alpine [root@localhost ~]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES e0070f5403b3 redis:alpine "docker-entrypoint.s…" 58 seconds ago Up 57 seconds 6379/tcp redis.3.5p2550af9hvoft2qxs4qoe3g7 a62dfe79ee9a redis:alpine "docker-entrypoint.s…" 58 seconds ago Up 57 seconds 6379/tcp redis.2.lil9796511b7dsl8luupgc7uj dea48d1e4a5b redis:alpine "docker-entrypoint.s…" 2 minutes ago Up 2 minutes 6379/tcp redis.1.obwahecglli7g6i1wn4hjhh14 ce5db9dc2f44 centos:6 "/bin/bash" 2 hours ago Up 2 hours C6
config
// 建立文件, 用於容器中的一些配置文件, 公共的配置文件的使用; [root@localhost ~]# echo "This is configure test"|docker config create my-config - 1n32fabs69c73jixr6nsqg4wm // 查看建立的文件 [root@localhost ~]# docker config ls ID NAME CREATED UPDATED 1n32fabs69c73jixr6nsqg4wm my-config 8 seconds ago 8 seconds ago // 啓動容器, 掛載使用上面的配置文件 [root@localhost ~]# docker service create --name redis --config my-config redis:alpine zrhd42cysh0o3i557riaq00tl overall progress: 1 out of 1 tasks 1/1: running verify: Service converged // 查看 [root@localhost ~]# docker service ps redis ID NAME IMAGE NODE DESIRED STATE CURRENT STATE ERROR PORTS 7hm9sbsckh6x redis.1 redis:alpine localhost.localdomain Running Running 3 minutes ago // 進入容器, 查看上面的文件 [root@localhost ~]# docker ps |grep redis ea63f28bfc9d redis:alpine "docker-entrypoint.s…" 5 minutes ago Up 5 minutes 6379/tcp redis.1.7hm9sbsckh6x6kotnxh3eqdyd [root@localhost ~]# docker exec ea6 ls -l / total 8 drwxr-xr-x 1 root root 4096 May 16 00:52 bin drwxr-xr-x 2 redis redis 6 May 16 00:52 data drwxr-xr-x 5 root root 340 Jun 24 02:30 dev drwxr-xr-x 1 root root 66 Jun 24 02:30 etc drwxr-xr-x 1 root root 19 May 11 01:43 home drwxr-xr-x 1 root root 17 May 16 00:52 lib drwxr-xr-x 5 root root 44 May 9 20:49 media drwxr-xr-x 2 root root 6 May 9 20:49 mnt -r--r--r-- 1 root root 23 Jun 24 02:30 my-config drwxr-xr-x 2 root root 6 May 9 20:49 opt dr-xr-xr-x 137 root root 0 Jun 24 02:30 proc drwx------ 2 root root 6 May 9 20:49 root drwxr-xr-x 2 root root 6 May 9 20:49 run drwxr-xr-x 1 root root 21 May 11 01:43 sbin drwxr-xr-x 2 root root 6 May 9 20:49 srv dr-xr-xr-x 13 root root 0 Jun 24 02:30 sys drwxrwxrwt 1 root root 6 May 16 00:52 tmp drwxr-xr-x 1 root root 19 May 16 00:52 usr drwxr-xr-x 1 root root 19 May 9 20:49 var // 看到有 my-config 文件, 查看文件內容 [root@localhost ~]# docker exec ea6 cat /my-config This is configure test // 刪除容器, 及刪除文件 [root@localhost ~]# docker service ls ID NAME MODE REPLICAS IMAGE PORTS zrhd42cysh0o redis replicated 1/1 redis:alpine [root@localhost ~]# docker service rm zrh zrh [root@localhost ~]# docker config ls ID NAME CREATED UPDATED 1n32fabs69c73jixr6nsqg4wm my-config 17 minutes ago 17 minutes ago [root@localhost ~]# docker config rm 1n32 1n32
container
[root@localhost ~]# docker container --help Usage: docker container COMMAND Manage containers Commands: 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
image
[root@localhost ~]# docker image --help Usage: docker image COMMAND Manage images Commands: build Build an image from a Dockerfile history Show the history of an image import Import the contents from a tarball to create a filesystem image inspect Display detailed information on one or more images load Load an image from a tar archive or STDIN ls List images prune Remove unused images // 刪除未使用的鏡像 pull Pull an image or a repository from a registry push Push an image or a repository to a registry rm Remove one or more images save Save one or more images to a tar archive (streamed to STDOUT by default) tag Create a tag TARGET_IMAGE that refers to SOURCE_IMAGE
node
[root@localhost ~]# docker node --help Usage: docker node COMMAND Manage Swarm nodes Commands: demote Demote one or more nodes from manager in the swarm inspect Display detailed information on one or more nodes ls List nodes in the swarm promote Promote one or more nodes to manager in the swarm ps List tasks running on one or more nodes, defaults to current node rm Remove one or more nodes from the swarm update Update a node
plugin
[root@localhost ~]# docker plugin --help Usage: docker plugin COMMAND Manage plugins Commands: create Create a plugin from a rootfs and configuration. Plugin data directory must contain config.json and rootfs directory. disable Disable a plugin enable Enable a plugin inspect Display detailed information on one or more plugins install Install a plugin ls List plugins push Push a plugin to a registry rm Remove one or more plugins set Change settings for a plugin upgrade Upgrade an existing plugin
secret
// 用於給容器提供一些共享的數據; 如 證書 或者 配置等 [root@localhost ~]# docker secret --help Usage: docker secret COMMAND Manage Docker secrets Commands: create Create a secret from a file or STDIN as content inspect Display detailed information on one or more secrets ls List secrets rm Remove one or more secrets