容器管理,容器常用選項
選項 |
描述 |
-i, –interactive |
交互式 |
-t, –tty |
分配一個偽終端 |
-d, –detach |
運行容器到后台 |
-e, –env |
設置環境變量 |
-p, –publish list |
發布容器端口到主機 |
–name string |
指定容器名稱 |
-h, –hostname |
設置容器主機名 |
–restart string |
容器退出時重啟策略 默認no,可選值:[always|on-failure] |
[BEGIN] 2019/3/12 12:37:50 #查看docker container 的幫助 [root@192 ~]# 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 Run 'docker container COMMAND --help' for more information on a command. #查看docker的幫助 [root@192 ~]# docker --help Usage: docker [OPTIONS] COMMAND A self-sufficient runtime for containers Options: --config string Location of client config files (default "/root/.docker") -D, --debug Enable debug mode -H, --host list Daemon socket(s) to connect to -l, --log-level string Set the logging level ("debug"|"info"|"warn"|"error"|"fatal") (default "info") --tls Use TLS; implied by --tlsverify --tlscacert string Trust certs signed only by this CA (default "/root/.docker/ca.pem") --tlscert string Path to TLS certificate file (default "/root/.docker/cert.pem") --tlskey string Path to TLS key file (default "/root/.docker/key.pem") --tlsverify Use TLS and verify the remote -v, --version Print version information and quit Management Commands: builder Manage builds config Manage Docker configs container Manage containers engine Manage the docker engine image Manage images network Manage networks node Manage Swarm nodes plugin Manage plugins secret Manage Docker secrets service Manage services stack Manage Docker stacks swarm Manage Swarm system Manage Docker trust Manage trust on Docker images volume Manage volumes Commands: attach Attach local standard input, output, and error streams to a running container build Build an image from a Dockerfile 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 events Get real time events from the server exec Run a command in a running container export Export a container's filesystem as a tar archive history Show the history of an image images List images import Import the contents from a tarball to create a filesystem image info Display system-wide information inspect Return low-level information on Docker objects kill Kill one or more running containers load Load an image from a tar archive or STDIN login Log in to a Docker registry logout Log out from a Docker registry 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 pull Pull an image or a repository from a registry push Push an image or a repository to a registry rename Rename a container restart Restart one or more containers rm Remove one or more containers rmi Remove one or more images run Run a command in a new container save Save one or more images to a tar archive (streamed to STDOUT by default) search Search the Docker Hub for images 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 tag Create a tag TARGET_IMAGE that refers to SOURCE_IMAGE 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 version Show the Docker version information wait Block until one or more containers stop, then print their exit codes Run 'docker COMMAND --help' for more information on a command. #查看docker container run的幫助 [root@192 ~]# docker container run --help Usage: docker container run [OPTIONS] IMAGE [COMMAND] [ARG...] Run a command in a new container Options: --add-host list Add a custom host-to-IP mapping (host:ip) -a, --attach list Attach to STDIN, STDOUT or STDERR --blkio-weight uint16 Block IO (relative weight), between 10 and 1000, or 0 to disable (default 0) --blkio-weight-device list Block IO weight (relative device weight) (default []) --cap-add list Add Linux capabilities --cap-drop list Drop Linux capabilities --cgroup-parent string Optional parent cgroup for the container --cidfile string Write the container ID to the file --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 CPU real-time period in microseconds --cpu-rt-runtime int Limit 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) -d, --detach Run container in background and print container ID --detach-keys string Override the key sequence for detaching a container --device list Add a host device to the container --device-cgroup-rule list Add a rule to the cgroup allowed devices list --device-read-bps list Limit read rate (bytes per second) from a device (default []) --device-read-iops list Limit read rate (IO per second) from a device (default []) --device-write-bps list Limit write rate (bytes per second) to a device (default []) --device-write-iops list Limit write rate (IO per second) to a device (default []) --disable-content-trust Skip image verification (default true) --dns list Set custom DNS servers --dns-option list Set DNS options --dns-search list Set custom DNS search domains --entrypoint string Overwrite the default ENTRYPOINT of the image -e, --env list Set environment variables --env-file list Read in a file of environment variables --expose list Expose a port or a range of ports --group-add list Add additional groups to join --health-cmd string Command to run to check health --health-interval duration Time between running the check (ms|s|m|h) (default 0s) --health-retries int Consecutive failures needed to report unhealthy --health-start-period duration Start period for the container to initialize before starting health-retries countdown (ms|s|m|h) (default 0s) --health-timeout duration Maximum time to allow one check to run (ms|s|m|h) (default 0s) --help Print usage -h, --hostname string Container host name --init Run an init inside the container that forwards signals and reaps processes -i, --interactive Keep STDIN open even if not attached --ip string IPv4 address (e.g., 172.30.100.104) --ip6 string IPv6 address (e.g., 2001:db8::33) --ipc string IPC mode to use --isolation string Container isolation technology --kernel-memory bytes Kernel memory limit -l, --label list Set meta data on a container --label-file list Read in a line delimited file of labels --link list Add link to another container --link-local-ip list Container IPv4/IPv6 link-local addresses --log-driver string Logging driver for the container --log-opt list Log driver options --mac-address string Container MAC address (e.g., 92:d0:c6:0a:29:33) -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 --memory-swappiness int Tune container memory swappiness (0 to 100) (default -1) --mount mount Attach a filesystem mount to the container --name string Assign a name to the container --network string Connect a container to a network (default "default") --network-alias list Add network-scoped alias for the container --no-healthcheck Disable any container-specified HEALTHCHECK --oom-kill-disable Disable OOM Killer --oom-score-adj int Tune host's OOM preferences (-1000 to 1000) --pid string PID namespace to use --pids-limit int Tune container pids limit (set -1 for unlimited) --privileged Give extended privileges to this container -p, --publish list Publish a container's port(s) to the host -P, --publish-all Publish all exposed ports to random ports --read-only Mount the container's root filesystem as read only --restart string Restart policy to apply when a container exits (default "no") --rm Automatically remove the container when it exits --runtime string Runtime to use for this container --security-opt list Security Options --shm-size bytes Size of /dev/shm --sig-proxy Proxy received signals to the process (default true) --stop-signal string Signal to stop a container (default "SIGTERM") --stop-timeout int Timeout (in seconds) to stop a container --storage-opt list Storage driver options for the container --sysctl map Sysctl options (default map[]) --tmpfs list Mount a tmpfs directory -t, --tty Allocate a pseudo-TTY --ulimit ulimit Ulimit options (default []) -u, --user string Username or UID (format: <name|uid>[:<group|gid>]) --userns string User namespace to use --uts string UTS namespace to use -v, --volume list Bind mount a volume --volume-driver string Optional volume driver for the container --volumes-from list Mount volumes from the specified container(s) -w, --workdir string Working directory inside the container #列出images [root@192 ~]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE nginx latest 881bd08c0b08 6 days ago 109MB busybox latest d8233ab899d4 3 weeks ago 1.2MB #從鏡像倉庫拉取name=centos:latest鏡像 [root@192 ~]# docker image pull centos:latest latest: Pulling from library/centos Digest: sha256:38777a4106f0072649128ea4236241345a3ed21c55abbbd53bad5342549f6126 Status: Downloaded newer image for centos:latest #列出鏡像,確認centos:latest鏡像拉取成功 [root@192 ~]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE nginx latest 881bd08c0b08 6 days ago 109MB busybox latest d8233ab899d4 3 weeks ago 1.2MB centos latest 1e1148e4cc2c 3 months ago 202MB #從鏡像倉庫拉取name=centos:7鏡像 [root@192 ~]# docker container run centos:7 Unable to find image 'centos:7' locally 7: Pulling from library/centos Digest: sha256:184e5f35598e333bfa7de10d8fb1cebb5ee4df5bc0f970bf2b1e7c7345136426 Status: Downloaded newer image for centos:7 #運行一個容器,來自名為centos:7的鏡像 [root@192 ~]# docker container run centos:7 #列出最近啟動的容器, 這里為空,是因為centos:7是一個基礎鏡像,沒有加-itd 參數,啟動后,默認馬上退出 [root@192 ~]# docker container ls CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES #列出所有狀態的容器,可以看到centos:7 status=Exited,說明啟動后,退出了 [root@192 ~]# docker container ls -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES d9101c06b8ec centos:7 "/bin/bash" 2 minutes ago Exited (0) 2 minutes ago practical_hertz 63b0e38408f8 centos:7 "/bin/bash" 2 minutes ago Exited (0) 2 minutes ago goofy_elbakyan 0be8e5611dcc nginx "nginx -g 'daemon of…" 9 minutes ago Exited (0) 4 minutes ago elastic_sammet #刪除容器名=d9101c06b8ec的容器 [root@192 ~]# docker container rm d9101c06b8ec d9101c06b8ec #刪除容器名=63b0e38408f8的容器 [root@192 ~]# docker container rm 63b0e38408f8 63b0e38408f8 [root@192 ~]# docker container ls -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 0be8e5611dcc nginx "nginx -g 'daemon of…" 10 minutes ago Exited (0) 5 minutes ago elastic_sammet #一個鏡像可以啟動多個容器,就類似windows的快捷鍵,鏡像是分層存儲的,容器也是分層存儲的,容器的運行不互相影響,因為不同的容器,有自己的分層存儲文件。 [root@192 ~]# docker container run centos:7 [root@192 ~]# docker container run -d centos:7 a11b2d9ebb84ab7abc6aa0c51ae41d303d5f33d7f6a72d14988f0d3ae7458077 [root@192 ~]# docker container run -itd centos:7 9a9c6d523f30cc9bda9eab1f3cbd2a8ea0991bacaea1c744ce34e2c3b5bee0a1 [root@192 ~]# docker container run -itd centos:7 bash 7894ef6ccb2f7521f77968c5ad7dde18e868ccb128a2cccf1974965fc1ace1a6 [root@192 ~]# docker container ls CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 7894ef6ccb2f centos:7 "bash" 46 seconds ago Up 45 seconds condescending_torvalds 9a9c6d523f30 centos:7 "/bin/bash" 56 seconds ago Up 55 seconds focused_einstein #進入容器名=7894ef6ccb2f,並打開一個終端 [root@192 ~]# docker container exec -it 7894ef6ccb2f bash #進入終端后,可運行linux的基礎命令 [root@7894ef6ccb2f /]# ls anaconda-post.log bin dev etc home lib lib64 media mnt opt proc root run sbin srv sys tmp usr var [root@7894ef6ccb2f /]# ps -ef UID PID PPID C STIME TTY TIME CMD root 1 0 0 11:53 pts/0 00:00:00 bash root 14 0 0 11:54 pts/1 00:00:00 bash root 28 14 0 11:55 pts/1 00:00:00 ps -ef #退出容器 [root@7894ef6ccb2f /]# exit exit #可以通過帶-e 參數:對容器內進行環境變量設置, -name 給當前的容器一個名字, -h 設置容器的主機名 這里本地沒有java:8這個鏡像,所以默認從鏡像倉庫拉取,拉取成功后,自動運行 [root@192 ~]# docker run -e JAVA_HOME=/usr/local/jdk --name firstjava -h java8 java:8 Unable to find image 'java:8' locally 8: Pulling from library/java Digest: sha256:34959eed5df6bfe74a17fbf459ed29505d51a48d5cb4a39f215b613c65b8023b Status: Downloaded newer image for java:8 #列出所有狀態的容器 [root@192 ~]# docker container ls -l CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES e1ebf7553d34 java:8 "/bin/bash" 56 seconds ago Exited (0) 53 seconds ago firstjava #刪除容器 [root@192 ~]# docker container rm e1ebf7553d34 e1ebf7553d34 #加上 -d參數,運行容器 [root@192 ~]# docker run -d -e JAVA_HOME=/usr/local/jdk --name firstjava -h java8 java:8 dd74f7cf5d0fe0d42b4ab678e2ac93343c5dd21f88c08b78eff9636087d88d12 #加上 -d參數,運行容器,java:8 啟動后還是自動退出了 [root@192 ~]# docker container ls -l CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES dd74f7cf5d0f java:8 "/bin/bash" 21 seconds ago Exited (0) 20 seconds ago firstjava #刪除容器 [root@192 ~]# docker container rm dd74f7cf5d0f dd74f7cf5d0f #加上 -itd參數,運行容器 [root@192 ~]# docker run -itd -e JAVA_HOME=/usr/local/jdk --name firstjava -h java8 java:8 375d4cc961386a1fdeddd4277d92b466ae477e71743ee004d60965d5591ac684 #加上 -d參數,運行容器,java:8 啟動后容器狀態為啟動着。這里要說明一下,因為java:8也是一個基礎鏡像,前面2次啟動容器后,由於容器內沒有一直運行的進行,所以容器自動退出,加了-itd的后,容器在后台運行,並且啟動着一個tty終端,所以不會自動退出了。 [root@192 ~]# docker container ls -l CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 375d4cc96138 java:8 "/bin/bash" 6 seconds ago Up 4 seconds firstjava #進入上面創建的java:8容器,確認-e JAVA_HOME=/usr/local/jdk 變量是否成功 [root@192 ~]# docker container exec -it firstjava bash #給容器設置JAVA_HOME變量,設置成功 root@java8:/# echo $JAVA_HOME /usr/local/jdk #給容器設置hostname設置成功 root@java8:/# hostname java8 #退出容器 root@java8:/# exit exit #給容器配置 容器退出時重啟策略(測試通過:先將nginx容器stop. reboot宿主機,宿主機啟動后查看nginx默認啟動狀態) [root@192 ~]# docker run -itd -e JAVA_HOME=/usr/local/jdk --name test01 -h java8 --restart=always java:8 d19228751012d93568bba75827660c5cb848423416ec57a65c125cf8e75ebee7 [root@192 ~]# docker run -itd --restart=always java:8 e1b00707869712515b968ef4a98569a31cf4c0e5bccb5f4fcac57b978311279b #列出容器資源使用狀態,可以看到容器總內容是972.6MiB,已使用492KiB,MEM %=0.05%內存使用百分比,NET I/O是網絡IO [root@192 ~]# docker container stats test01 CONTAINER ID NAME CPU % MEM USAGE / LIMIT MEM % NET I/O BLOCK I/O PIDS d19228751012 test01 0.00% 492KiB / 972.6MiB 0.05% 656B / 0B 0B / 0B 1 [root@192 ~]# docker container ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES d64127b97ed8 java:8 "/bin/bash" About an hour ago Up About an hour pensive_franklin e1b007078697 java:8 "/bin/bash" 2 hours ago Up 2 hours musing_elion d19228751012 java:8 "/bin/bash" 2 hours ago Up 2 hours test01 375d4cc96138 java:8 "/bin/bash" 2 hours ago Up 2 hours firstjava 7894ef6ccb2f centos:7 "bash" 2 hours ago Up 2 hours condescending_torvalds 9a9c6d523f30 centos:7 "/bin/bash" 2 hours ago Up 2 hours focused_einstein a11b2d9ebb84 centos:7 "/bin/bash" 2 hours ago Exited (0) 2 hours ago clever_brown e6946111e2e9 centos:7 "/bin/bash" 2 hours ago Exited (0) 2 hours ago trusting_bardeen 0be8e5611dcc nginx "nginx -g 'daemon of…" 3 hours ago Exited (0) 3 hours ago elastic_sammet #停止上面所有容器 也可使用 docker container stop $(docker ps -q -a) [root@192 ~]# docker container stop $(docker ps -a|awk '{print $1}') d64127b97ed8 e1b007078697 d19228751012 375d4cc96138 7894ef6ccb2f 9a9c6d523f30 a11b2d9ebb84 e6946111e2e9 0be8e5611dcc Error response from daemon: No such container: CONTAINER #確認容器都已停止 [root@192 ~]# docker container ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES d64127b97ed8 java:8 "/bin/bash" 2 hours ago Exited (0) 20 seconds ago pensive_franklin e1b007078697 java:8 "/bin/bash" 2 hours ago Exited (0) 20 seconds ago musing_elion d19228751012 java:8 "/bin/bash" 2 hours ago Exited (0) 20 seconds ago test01 375d4cc96138 java:8 "/bin/bash" 2 hours ago Exited (0) 20 seconds ago firstjava 7894ef6ccb2f centos:7 "bash" 2 hours ago Exited (137) 10 seconds ago condescending_torvalds 9a9c6d523f30 centos:7 "/bin/bash" 2 hours ago Exited (137) 10 seconds ago focused_einstein a11b2d9ebb84 centos:7 "/bin/bash" 3 hours ago Exited (0) 3 hours ago clever_brown e6946111e2e9 centos:7 "/bin/bash" 3 hours ago Exited (0) 3 hours ago trusting_bardeen 0be8e5611dcc nginx "nginx -g 'daemon of…" 3 hours ago Exited (0) 3 hours ago elastic_sammet #刪除上面所有容器 也可使用 docker container rm $(docker ps -q -a), 其中 -f刪除可以強制刪除運行中的容器 [root@192 ~]# docker container rm -f $(docker ps -a|awk '{print $1}') d64127b97ed8 e1b007078697 d19228751012 375d4cc96138 7894ef6ccb2f 9a9c6d523f30 a11b2d9ebb84 e6946111e2e9 0be8e5611dcc Error: No such container: CONTAINER #確認容器都已被刪除 [root@192 ~]# docker container ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES #查看鏡像 [root@192 ~]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE nginx latest 881bd08c0b08 6 days ago 109MB busybox latest d8233ab899d4 3 weeks ago 1.2MB centos 7 1e1148e4cc2c 3 months ago 202MB centos latest 1e1148e4cc2c 3 months ago 202MB java 8 d23bdf5b1b1b 2 years ago 643MB #啟動一個nginx容器 [root@192 ~]# docker container run -d -p 8800:80 nginx a03e32f2e657358c75003e0f2dc8659d534bb92c2a5cd6f0d6134a760cc42384 #列出運行中的容器 [root@192 ~]# docker container ps -qa a03e32f2e657 #查看容器端口 [root@192 ~]# docker container port a03e32f2e657 80/tcp -> 0.0.0.0:8800 #查看容器日志 [root@192 ~]# docker container logs a03e32f2e657 192.168.1.5 - - [11/Mar/2019:14:29:06 +0000] "GET / HTTP/1.1" 304 0 "-" "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.109 Safari/537.36" "-" #查看容器日志,帶-f ,實時打印新增的日志 [root@192 ~]# docker container logs a03e32f2e657 -f 192.168.1.5 - - [11/Mar/2019:14:29:06 +0000] "GET / HTTP/1.1" 304 0 "-" "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.109 Safari/537.36" "-" 192.168.1.5 - - [11/Mar/2019:14:31:37 +0000] "GET / HTTP/1.1" 304 0 "-" "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.109 Safari/537.36" "-" 192.168.1.5 - - [11/Mar/2019:14:31:43 +0000] "GET / HTTP/1.1" 304 0 "-" "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.109 Safari/537.36" "-" 192.168.1.5 - - [11/Mar/2019:14:31:49 +0000] "GET / HTTP/1.1" 304 0 "-" "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.109 Safari/537.36" "-" ^C #列出容器里正在運行的進程 [root@192 ~]# docker container top a03e32f2e657 UID PID PPID C STIME TTY TIME CMD root 20099 20080 0 22:26 ? 00:00:00 nginx: master process nginx -g daemon off; 101 20136 20099 0 22:26 ? 00:00:00 nginx: worker process #以下做容器內外文件的copy [root@192 ~]# ls anaconda-ks.cfg nginx.tar [root@192 ~]# touch a.out [root@192 ~]# ls anaconda-ks.cfg a.out nginx.tar #把宿主機中的文件a.out copy 到容器a03e32f2e657的 /目錄 [root@192 ~]# docker container cp a.out a03e32f2e657:/ #進入容器查看a.out是否被copy進了宿主機,在容器中a.out存在,測試通過 [root@192 ~]# docker container exec -it a03e32f2e657 bash root@a03e32f2e657:/# ls a.out bin boot dev etc home lib lib64 media mnt opt proc root run sbin srv sys tmp usr var #在容器中創建 b.out, root@a03e32f2e657:/# touch b.out root@a03e32f2e657:/# ls a.out bin dev home lib64 mnt proc run srv tmp var b.out boot etc lib media opt root sbin sys usr root@a03e32f2e657:/# exit exit #將容器中的/b.out文件copy到宿主機用戶的當前目錄 [root@192 ~]# docker container cp a03e32f2e657:/b.out . #查看宿主機中b.out存在,測試通過 [root@192 ~]# ls anaconda-ks.cfg a.out b.out nginx.tar
作者: 梅梅~
出處: https://www.cnblogs.com/keeptesting
關於作者:專注軟件測試,測試運維相關工作,請多多賜教!
本文版權歸作者和博客園共有,歡迎轉載,但未經作者同意必須保留此段聲明,且在文章頁面明顯位置給出, 原文鏈接 如有問題,可加微信聯系。 微信:yangguangkg20140901 暗號:博客園.