1. 从镜像启动一个容器 1.1 前台启动容器 root@node16:~# docker imagesREPOSITORY TAG IMAGE ID CREATED SIZEroot@node16:~# docker pull nginxUsing default tag: latestlatest: Pulling from library/nginxa2abf6c4d29d: Already exists a9
1. 从镜像启动一个容器
1.1 前台启动容器
root@node16:~# docker imagesREPOSITORY TAG IMAGE ID CREATED SIZEroot@node16:~# docker pull nginxUsing default tag: latestlatest: Pulling from library/nginxa2abf6c4d29d: Already exists a9edb18cadd1: Already exists 589b7251471a: Already exists 186b1aaa4aa6: Already exists b4df32aa5a72: Already exists a0bcbecc962e: Already exists Digest: sha256:0d17b565c37bcbd895e9d92315a05c1c3c9a29f762b011a10c54a66cd53c9b31Status: Downloaded newer image for nginx:latestdocker.io/library/nginx:latestroot@node16:~# docker container ls -aCONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMESroot@node16:~# root@node16:~# docker container lsCONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES22bfc1251066 nginx "/docker-entrypoint.…" 33 seconds ago Up 30 seconds 80/tcp optimistic_brahmaguptaroot@node16:~#
1.2 后台启动容器
### 后台启动容器,并生成随机的容器ID和名称root@node16:~# docker container ls -aCONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES22bfc1251066 nginx "/docker-entrypoint.…" 5 minutes ago Exited (0) About a minute ago optimistic_brahmaguptaroot@node16:~# docker run -d nginxe9251fe9a682fe2d5b2a6ca0c1984565d66af5ea0f9dcfa2124d854e3b75ac08root@node16:~#root@node16:~# docker container ls -aCONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMESe9251fe9a682 nginx "/docker-entrypoint.…" 12 seconds ago Up 10 seconds 80/tcp heuristic_newton22bfc1251066 nginx "/docker-entrypoint.…" 6 minutes ago Exited (0) About a minute ago optimistic_brahmaguptaroot@node16:~# root@node16:~# docker container ls CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMESe9251fe9a682 nginx "/docker-entrypoint.…" 29 seconds ago Up 28 seconds 80/tcp heuristic_newtonroot@node16:~#
1.3 创建容器时并进入容器(依赖于"-i"(保持一个标准输入)和"-t"(分配一个tty终端)两个参数)
### 会打开一个bash并直接进入到容器,并随机生成容器ID和名称root@node16:~# docker image lsREPOSITORY TAG IMAGE ID CREATED SIZEnginx latest 605c77e624dd 2 months ago 141MBroot@node16:~# docker run -it nginx bashroot@e3b823472f5e:/# root@e3b823472f5e:/# cat /etc/issueDebian GNU/Linux 11 \n \lroot@e3b823472f5e:/# root@e3b823472f5e:/# uname -aLinux e3b823472f5e 4.15.0-112-generic #113-Ubuntu SMP Thu Jul 9 23:41:39 UTC 2020 x86_64 GNU/Linuxroot@e3b823472f5e:/# exitexitroot@node16:~# uname -aLinux node16 4.15.0-112-generic #113-Ubuntu SMP Thu Jul 9 23:41:39 UTC 2020 x86_64 x86_64 x86_64 GNU/Linuxroot@node16:~#
1.4 自定义容器名称(一般在单机上会有点作用,在k8s集群中很少去自定义容器名称)
root@node16:~# docker imagesREPOSITORY TAG IMAGE ID CREATED SIZEnginx latest 605c77e624dd 2 months ago 141MBroot@node16:~# docker container ls -aCONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMESb4d22577de0f nginx "/docker-entrypoint.…" 20 seconds ago Up 19 seconds 80/tcp mynginxe3b823472f5e nginx "/docker-entrypoint.…" 3 minutes ago Exited (0) 3 minutes ago strange_mcnultye9251fe9a682 nginx "/docker-entrypoint.…" 9 minutes ago Up 9 minutes 80/tcp heuristic_newton22bfc1251066 nginx "/docker-entrypoint.…" 15 minutes ago Exited (0) 10 minutes ago optimistic_brahmaguptaroot@node16:~# docker run --name my_nginx -d nginxfe311fd2419c13222f416a86523ceaf3ea563aff71ff6ee9a25b2069a2222ce2root@node16:~# docker container ls -aCONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMESfe311fd2419c nginx "/docker-entrypoint.…" 3 seconds ago Up 2 seconds 80/tcp my_nginxb4d22577de0f nginx "/docker-entrypoint.…" 40 seconds ago Up 39 seconds 80/tcp mynginxe3b823472f5e nginx "/docker-entrypoint.…" 4 minutes ago Exited (0) 3 minutes ago strange_mcnultye9251fe9a682 nginx "/docker-entrypoint.…" 9 minutes ago Up 9 minutes 80/tcp heuristic_newton22bfc1251066 nginx "/docker-entrypoint.…" 15 minutes ago Exited (0) 10 minutes ago optimistic_brahmaguptaroot@node16:~#
1.5 单次运行容器(容器退出后会自动删除,主要用于临时验证镜像内容是否符合标准,如配置文件目录组织结构等信息)
root@node16:~# docker run -it --rm --name V1nginx nginx bashroot@d46480b32cb4:/# exitexitroot@node16:~# docker container ls -a |grep V1nginx #发现容器没有了root@node16:~# docker container ls -a
1.6 批量强制关闭正在运行的容器
root@node16:~# docker container ls -aCONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMESfe311fd2419c nginx "/docker-entrypoint.…" 12 minutes ago Up 12 minutes 80/tcp my_nginxb4d22577de0f nginx "/docker-entrypoint.…" 12 minutes ago Up 12 minutes 80/tcp mynginxe3b823472f5e nginx "/docker-entrypoint.…" 16 minutes ago Exited (0) 15 minutes ago strange_mcnultye9251fe9a682 nginx "/docker-entrypoint.…" 21 minutes ago Up 21 minutes 80/tcp heuristic_newton22bfc1251066 nginx "/docker-entrypoint.…" 27 minutes ago Exited (0) 22 minutes ago optimistic_brahmaguptaroot@node16:~# docker container ps -a -qfe311fd2419cb4d22577de0fe3b823472f5ee9251fe9a68222bfc1251066root@node16:~# root@node16:~# docker kill $(docker container ps -a -q)fe311fd2419cb4d22577de0fe9251fe9a682Error response from daemon: Cannot kill container: e3b823472f5e: Container e3b823472f5e4b0de10b98b9322800330eafb2cd8187031b4de1173687da7578 is not runningError response from daemon: Cannot kill container: 22bfc1251066: Container 22bfc125106624d7540d23d9ee3a2d3fe2c35a1be1cf1745a4f95033b8706f9d is not runningroot@node16:~# docker container ls -aCONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMESfe311fd2419c nginx "/docker-entrypoint.…" 13 minutes ago Exited (137) 16 seconds ago my_nginxb4d22577de0f nginx "/docker-entrypoint.…" 13 minutes ago Exited (137) 16 seconds ago mynginxe3b823472f5e nginx "/docker-entrypoint.…" 17 minutes ago Exited (0) 16 minutes ago strange_mcnultye9251fe9a682 nginx "/docker-entrypoint.…" 22 minutes ago Exited (137) 16 seconds ago heuristic_newton22bfc1251066 nginx "/docker-entrypoint.…" 28 minutes ago Exited (0) 23 minutes ago optimistic_brahmaguptaroot@node16:~#
1.7 批量删除所有容器
root@node16:~# docker container ls -aCONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMESfe311fd2419c nginx "/docker-entrypoint.…" 16 minutes ago Exited (137) 3 minutes ago my_nginxb4d22577de0f nginx "/docker-entrypoint.…" 16 minutes ago Exited (137) 3 minutes ago mynginxe3b823472f5e nginx "/docker-entrypoint.…" 20 minutes ago Exited (0) 19 minutes ago strange_mcnultye9251fe9a682 nginx "/docker-entrypoint.…" 25 minutes ago Exited (137) 3 minutes ago heuristic_newton22bfc1251066 nginx "/docker-entrypoint.…" 31 minutes ago Exited (0) 26 minutes ago optimistic_brahmaguptaroot@node16:~# docker ps -a -qfe311fd2419cb4d22577de0fe3b823472f5ee9251fe9a68222bfc1251066root@node16:~# root@node16:~# docker container rm -f `docker container ps -a -q`fe311fd2419cb4d22577de0fe3b823472f5ee9251fe9a68222bfc1251066root@node16:~# docker container ls -aCONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMESroot@node16:~#
2. 查看容器
2.1 查看正在运行的容器
root@node16:~# docker psCONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES75effa3b5d32 nginx "/docker-entrypoint.…" 5 seconds ago Up 3 seconds 80/tcp brave_edison22d3fe040ab5 nginx "/docker-entrypoint.…" 13 seconds ago Up 12 seconds 80/tcp serene_sinoussiroot@node16:~# docker container psCONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES75effa3b5d32 nginx "/docker-entrypoint.…" 18 seconds ago Up 16 seconds 80/tcp brave_edison22d3fe040ab5 nginx "/docker-entrypoint.…" 26 seconds ago Up 24 seconds 80/tcp serene_sinoussiroot@node16:~#
2.2 查看所有容器(包含当前正在运行以及关闭的所有容器)
root@node16:~# docker container ps --helpUsage: docker container ls [OPTIONS]List containersAliases: ls, ps, listOptions: -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 sizesroot@node16:~#root@node16:~# docker ps --helpUsage: docker ps [OPTIONS]List containersOptions: -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 sizesroot@node16:~# docker container ls --helpUsage: docker container ls [OPTIONS]List containersAliases: ls, ps, listOptions: -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 sizesroot@node16:~# docker container lsCONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES75effa3b5d32 nginx "/docker-entrypoint.…" 4 minutes ago Up 4 minutes 80/tcp brave_edison22d3fe040ab5 nginx "/docker-entrypoint.…" 5 minutes ago Up 5 minutes 80/tcp serene_sinoussiroot@node16:~#root@node16:~# docker container list --helpUsage: docker container ls [OPTIONS]List containersAliases: ls, ps, listOptions: -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 sizesroot@node16:~# root@node16:~# docker container listCONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES75effa3b5d32 nginx "/docker-entrypoint.…" 6 minutes ago Up 6 minutes 80/tcp brave_edison22d3fe040ab5 nginx "/docker-entrypoint.…" 6 minutes ago Up 6 minutes 80/tcp serene_sinoussiroot@node16:~#root@node16:~# docker ps -aCONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMEScdd03684811a nginx "/docker-entrypoint.…" 16 seconds ago Exited (0) 12 seconds ago admiring_hawking75effa3b5d32 nginx "/docker-entrypoint.…" 7 minutes ago Up 7 minutes 80/tcp brave_edison22d3fe040ab5 nginx "/docker-entrypoint.…" 7 minutes ago Up 7 minutes 80/tcp serene_sinoussiroot@node16:~# docker container ps -aCONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMEScdd03684811a nginx "/docker-entrypoint.…" 26 seconds ago Exited (0) 22 seconds ago admiring_hawking75effa3b5d32 nginx "/docker-entrypoint.…" 7 minutes ago Up 7 minutes 80/tcp brave_edison22d3fe040ab5 nginx "/docker-entrypoint.…" 7 minutes ago Up 7 minutes 80/tcp serene_sinoussiroot@node16:~#
3. 删除运行中的容器
3.1 删除单个容器
root@Kubernetes:~# docker container rm --help Usage: docker container rm [OPTIONS] CONTAINER [CONTAINER...]Remove one or more containersOptions: -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 containerroot@Kubernetes:~# root@Kubernetes:~# docker container rm --help Usage: docker container rm [OPTIONS] CONTAINER [CONTAINER...]Remove one or more containersOptions: -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 containerroot@Kubernetes:~#root@Kubernetes:~# docker rm --helpUsage: docker rm [OPTIONS] CONTAINER [CONTAINER...]Remove one or more containersOptions: -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 containerroot@Kubernetes:~#root@node16:~# docker container ls -aCONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMESb2be2056e8cc nginx "/docker-entrypoint.…" 12 seconds ago Up 10 seconds 80/tcp lucid_hawkinga92ef63990b3 nginx "/docker-entrypoint.…" 28 seconds ago Up 26 seconds 80/tcp strange_franklin2a8c66062b02 alpine "/bin/sh" 2 minutes ago Exited (0) 2 minutes ago cool_chaplygincdd03684811a nginx "/docker-entrypoint.…" 22 hours ago Exited (0) 22 hours ago admiring_hawking75effa3b5d32 nginx "/docker-entrypoint.…" 23 hours ago Exited (0) 22 hours ago brave_edison22d3fe040ab5 nginx "/docker-entrypoint.…" 23 hours ago Exited (0) 22 hours ago serene_sinoussiroot@node16:~# root@node16:~# docker container rm -f b2be2056e8cc #基于容器ID强制删除该容器b2be2056e8ccroot@node16:~#root@node16:~# docker rm -f 22d3fe040ab522d3fe040ab5root@node16:~# docker container ps -aCONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMESa92ef63990b3 nginx "/docker-entrypoint.…" 2 minutes ago Up 2 minutes
3.2 删除多个容器
root@node16:~# docker container ls -aCONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMESa92ef63990b3 nginx "/docker-entrypoint.…" 7 minutes ago Up 7 minutes 80/tcp strange_franklincdd03684811a nginx "/docker-entrypoint.…" 23 hours ago Exited (0) 23 hours ago admiring_hawkingroot@node16:~# docker container rm -f a92ef63990b3 cdd03684811aa92ef63990b3cdd03684811aroot@node16:~# docker container ls -aCONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
3.3 批量删除状态为已退出的容器
root@node16:~# docker container ls -aCONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMESf953b556817b alpine "/bin/sh" 50 seconds ago Exited (0) 49 seconds ago agitated_panini089167ee6bf2 alpine "/bin/sh" 51 seconds ago Exited (0) 50 seconds ago romantic_brattain008b6448950e alpine "/bin/sh" 53 seconds ago Exited (0) 51 seconds ago romantic_cerf714c14ea777e alpine "/bin/sh" 54 seconds ago Exited (0) 53 seconds ago intelligent_tharp83a11c1e556c alpine "/bin/sh" 56 seconds ago Exited (0) 54 seconds ago brave_robinson4c829a31213e nginx "/docker-entrypoint.…" About a minute ago Up About a minute 80/tcp funny_wing83aabe7b8b22 nginx "/docker-entrypoint.…" About a minute ago Up About a minute 80/tcp awesome_mayer7c2f4e32e545 nginx "/docker-entrypoint.…" About a minute ago Up About a minute 80/tcp adoring_noether98e39c1a2abf nginx "/docker-entrypoint.…" About a minute ago Up About a minuteroot@node16:~# docker container ps -aq -f status=exitedf953b556817b089167ee6bf2008b6448950e714c14ea777e83a11c1e556croot@node16:~#root@node16:~# docker container rm -f `docker container ps -qa -f status=exited`f953b556817b089167ee6bf2008b6448950e714c14ea777e83a11c1e556croot@node16:~# docker container ls -aCONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES4c829a31213e nginx "/docker-entrypoint.…" 2 minutes ago Up 2 minutes 80/tcp funny_wing83aabe7b8b22 nginx "/docker-entrypoint.…" 2 minutes ago Up 2 minutes 80/tcp awesome_mayer7c2f4e32e545 nginx "/docker-entrypoint.…" 3 minutes ago Up 2 minutes 80/tcp adoring_noether98e39c1a2abf nginx "/docker-entrypoint.…" 3 minutes ago Up 2 minutes 80/tcp exciting_visvesvarayaf406b036985d nginx "/docker-entrypoint.…" 3 minutes ago Up 3 minutes 80/tcp clever_mahavira7bcab6a5489f nginx "/docker-entrypoint.…" 3 minutes ago Up 3 minutes 80/tcp interesting_wu159adb663185 nginx "/docker-entrypoint.…" 3 minutes ago Up 3 minutes 80/tcp kind_knuthaeaa19272e8e nginx "/docker-entrypoint.…" 3 minutes ago Up 3 minutes 80/tcp hopeful_wilson367a9a3e0f59 nginx "/docker-entrypoint.…" 3 minutes ago Up 3 minutes 80/tcp jolly_jenningsroot@node16:~#
3.4 批量关闭正在运行的容器
root@node16:~# docker container ls -aCONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES7bcab6a5489f nginx "/docker-entrypoint.…" 8 minutes ago Up 8 minutes 80/tcp interesting_wu159adb663185 nginx "/docker-entrypoint.…" 8 minutes ago Up 8 minutes 80/tcp kind_knuthaeaa19272e8e nginx "/docker-entrypoint.…" 8 minutes ago Up 8 minutes 80/tcp hopeful_wilson367a9a3e0f59 nginx "/docker-entrypoint.…" 8 minutes ago Up 8 minutes 80/tcp jolly_jenningsroot@node16:~# docker ps -a -q7bcab6a5489f159adb663185aeaa19272e8e367a9a3e0f59root@node16:~# docker stop $(docker container ps -a -q)7bcab6a5489f159adb663185aeaa19272e8e367a9a3e0f59root@node16:~# docker container ls -aCONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES7bcab6a5489f nginx "/docker-entrypoint.…" 9 minutes ago Exited (0) 7 seconds ago interesting_wu159adb663185 nginx "/docker-entrypoint.…" 9 minutes ago Exited (0) 7 seconds ago kind_knuthaeaa19272e8e nginx "/docker-entrypoint.…" 9 minutes ago Exited (0) 7 seconds ago hopeful_wilson367a9a3e0f59 nginx "/docker-entrypoint.…" 9 minutes ago Exited (0) 7 seconds ago jolly_jenningsroot@node16:~#
3.5 批量开启关闭的容器
root@node16:~# docker container ls -aCONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES7bcab6a5489f nginx "/docker-entrypoint.…" 13 minutes ago Exited (0) 4 minutes ago interesting_wu159adb663185 nginx "/docker-entrypoint.…" 13 minutes ago Exited (0) 4 minutes ago kind_knuthaeaa19272e8e nginx "/docker-entrypoint.…" 13 minutes ago Exited (0) 4 minutes ago hopeful_wilsonroot@node16:~# docker container ps -a -q -f status=exited7bcab6a5489f159adb663185aeaa19272e8eroot@node16:~# docker start `docker container ps -q -a -f status=exited`7bcab6a5489f159adb663185aeaa19272e8eroot@node16:~# docker container ls -aCONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES7bcab6a5489f nginx "/docker-entrypoint.…" 14 minutes ago Up 9 seconds 80/tcp interesting_wu159adb663185 nginx "/docker-entrypoint.…" 14 minutes ago Up 9 seconds 80/tcp kind_knuthaeaa19272e8e nginx "/docker-entrypoint.…" 14 minutes ago Up 8 seconds 80/tcp hopeful_wilsonroot@node16:~#
3.6 批量强制关闭正在运行的容器
root@node16:~# docker container ls -aCONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES7bcab6a5489f nginx "/docker-entrypoint.…" 16 minutes ago Up 2 minutes 80/tcp interesting_wu159adb663185 nginx "/docker-entrypoint.…" 16 minutes ago Up 2 minutes 80/tcp kind_knuthaeaa19272e8e nginx "/docker-entrypoint.…" 16 minutes ago Up 2 minutes 80/tcp hopeful_wilsonroot@node16:~# docker container ps -a -q7bcab6a5489f159adb663185aeaa19272e8eroot@node16:~# docker kill $(docker container ps -a -q)7bcab6a5489f159adb663185aeaa19272e8eroot@node16:~# docker container ls -aCONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES7bcab6a5489f nginx "/docker-entrypoint.…" 17 minutes ago Exited (137) 12 seconds ago interesting_wu159adb663185 nginx "/docker-entrypoint.…" 17 minutes ago Exited (137) 12 seconds ago kind_knuthaeaa19272e8e nginx "/docker-entrypoint.…" 17 minutes ago Exited (137) 12 seconds ago hopeful_wilsonroot@node16:~#
3.7 批量删除所有容器
root@node16:~# docker container ls -aCONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES7bcab6a5489f nginx "/docker-entrypoint.…" 19 minutes ago Exited (137) 2 minutes ago interesting_wu159adb663185 nginx "/docker-entrypoint.…" 19 minutes ago Exited (137) 2 minutes ago kind_knuthaeaa19272e8e nginx "/docker-entrypoint.…" 19 minutes ago Exited (137) 2 minutes ago hopeful_wilsonroot@node16:~# docker container ps -a -q7bcab6a5489f159adb663185aeaa19272e8eroot@node16:~# docker container rm -f $(docker container ps -a -q)7bcab6a5489f159adb663185aeaa19272e8eroot@node16:~# docker container ls -aCONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMESroot@node16:~#
4. 容器端口映射
4.1 随机端口映射
root@node16:~# docker container ls -aCONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMESc23a0456d6a6 nginx "/docker-entrypoint.…" 5 seconds ago Up 3 seconds 80/tcp happy_bassiroot@node16:~# docker container rm -f c23a0456d6a6c23a0456d6a6root@node16:~# docker image lsREPOSITORY TAG IMAGE ID CREATED SIZEnginx latest 605c77e624dd 2 months ago 141MBalpine latest c059bfaa849c 3 months ago 5.59MBroot@node16:~# docker container ls -aCONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMESroot@node16:~#root@node16:~# docker run -P nginx #前台启动并随机映射本地端口到容器的80端口/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.sh10-listen-on-ipv6-by-default.sh: info: Getting the checksum of /etc/nginx/conf.d/default.conf10-listen-on-ipv6-by-default.sh: info: 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: Launching /docker-entrypoint.d/30-tune-worker-processes.sh/docker-entrypoint.sh: Configuration complete; ready for start up2022/03/01 13:28:31 [notice] 1#1: using the "epoll" event method2022/03/01 13:28:31 [notice] 1#1: nginx/1.21.52022/03/01 13:28:31 [notice] 1#1: built by gcc 10.2.1 20210110 (Debian 10.2.1-6) 2022/03/01 13:28:31 [notice] 1#1: OS: Linux 4.15.0-112-generic2022/03/01 13:28:31 [notice] 1#1: getrlimit(RLIMIT_NOFILE): 1048576:10485762022/03/01 13:28:31 [notice] 1#1: start worker processes2022/03/01 13:28:31 [notice] 1#1: start worker process 322022/03/01 13:28:31 [notice] 1#1: start worker process 33
4.2 指定端口映射
root@node15:~# hostname -i172.16.1.15root@node15:~# docker run -p 10001:80 --name myNginx-test01 -it -d nginx #将本地所有地址的10001端口映射到容器的80端口4c123f46e38f7246fc74725082737ce933f0faa352c015113833804272c87b23root@node15:~# docker run -p 172.16.1.15:20002:80 --name myNginx-test02 -it -d nginx #将本地172.16.1.15对应的IP地址的20002端口映射到容器的80端口e90410d961d576ce7fb6cf6c2dda8f97703d55c07dbc572c0578e5c8b3d64ba3root@node15:~# docker run -p 172.16.1.15::80 --name myNginx-test03 -it -d nginx #将本地172.16.1.15对应的IP地址的随机端口映射到容器的80端口3c6eae128f3455f2fca232900d7627ab4cd9a99ef4baaf21a26fa3a43e9b1352root@node15:~# docker run -p 172.16.1.15:40004:80/udp --name myNginx-test04 -it -d nginx #将本地172.16.1.15对应的IP地址的40004端口映射到80UDP端口,如果不指定协议默认为TCP端口43c85684930a13492c1665d99af2c61929f5f2b2e7aaf3091c0e9fb86be1aa75root@node15:~# docker run -p 172.16.1.15:50001:80/udp -p 50002:443/tcp -p 50003:53/udp --name myNginx-test05 -it -d nginx #还可以一次性映射多个端口/协议15acaf3a4cff4fa3ab73ca407a93cf040410a33919cdf2fbd0479f7eb6c86f98root@node15:~# docker container ls -aCONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES15acaf3a4cff nginx "/docker-entrypoint.…" 32 seconds ago Up 31 seconds 80/tcp, 0.0.0.0:50003->53/udp, :::50003->53/udp, 172.16.1.15:50001->80/udp, 0.0.0.0:50002->443/tcp, :::50002->443/tcp myNginx-test0543c85684930a nginx "/docker-entrypoint.…" About a minute ago Up About a minute 80/tcp, 172.16.1.15:40004->80/udp myNginx-test043c6eae128f34 nginx "/docker-entrypoint.…" 2 minutes ago Up 2 minutes 172.16.1.15:49153->80/tcp myNginx-test03e90410d961d5 nginx "/docker-entrypoint.…" 2 minutes ago Up 2 minutes 172.16.1.15:20002->80/tcp myNginx-test024c123f46e38f nginx "/docker-entrypoint.…" 3 minutes ago Up 3 minutes 0.0.0.0:10001->80/tcp, :::10001->80/tcp myNginx-test01root@node15:~#
4.3 查看容器已经映射的端口
root@node15:~# docker container ls -aCONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES15acaf3a4cff nginx "/docker-entrypoint.…" 9 minutes ago Up 9 minutes 80/tcp, 0.0.0.0:50003->53/udp, :::50003->53/udp, 172.16.1.15:50001->80/udp, 0.0.0.0:50002->443/tcp, :::50002->443/tcp myNginx-test0543c85684930a nginx "/docker-entrypoint.…" 10 minutes ago Up 10 minutes 80/tcp, 172.16.1.15:40004->80/udp myNginx-test043c6eae128f34 nginx "/docker-entrypoint.…" 10 minutes ago Up 10 minutes 172.16.1.15:49153->80/tcp myNginx-test03e90410d961d5 nginx "/docker-entrypoint.…" 11 minutes ago Up 11 minutes 172.16.1.15:20002->80/tcp myNginx-test024c123f46e38f nginx "/docker-entrypoint.…" 12 minutes ago Up 12 minutes 0.0.0.0:10001->80/tcp, :::10001->80/tcp myNginx-test01root@node15:~# docker container port myNginx-test0580/udp -> 172.16.1.15:50001443/tcp -> 0.0.0.0:50002443/tcp -> :::5000253/udp -> 0.0.0.0:5000353/udp -> :::50003root@node15:~# docker container port myNginx-test0480/udp -> 172.16.1.15:40004root@node15:~# docker container port myNginx-test0380/tcp -> 172.16.1.15:49153root@node15:~# docker container port myNginx-test0280/tcp -> 172.16.1.15:20002root@node15:~# docker container port myNginx-test0180/tcp -> 0.0.0.0:1000180/tcp -> :::10001root@node15:~#
5. 使用inspect命令获取容器的信息
5.1 获取容器的详细信息
### 查看容器名称为"myNginx-test05"的容器详细信息root@node15:~# docker container ls -aCONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES15acaf3a4cff nginx "/docker-entrypoint.…" 12 minutes ago Up 12 minutes 80/tcp, 0.0.0.0:50003->53/udp, :::50003->53/udp, 172.16.1.15:50001->80/udp, 0.0.0.0:50002->443/tcp, :::50002->443/tcp myNginx-test05root@node15:~# docker container inspect myNginx-test05[ { "Id": "15acaf3a4cff4fa3ab73ca407a93cf040410a33919cdf2fbd0479f7eb6c86f98", "Created": "2022-03-01T14:05:29.698813256Z", "Path": "/docker-entrypoint.sh", "Args": [ "nginx", "-g", "daemon off;" ], "State": { "Status": "running", "Running": true, "Paused": false, "Restarting": false, "OOMKilled": false, "Dead": false, "Pid": 2855, "ExitCode": 0, "Error": "", "StartedAt": "2022-03-01T14:05:30.823644089Z", "FinishedAt": "0001-01-01T00:00:00Z" }, "Image": "sha256:605c77e624ddb75e6110f997c58876baa13f8754486b461117934b24a9dc3a85", "ResolvConfPath": "/var/lib/docker/containers/15acaf3a4cff4fa3ab73ca407a93cf040410a33919cdf2fbd0479f7eb6c86f98/resolv.conf", "HostnamePath": "/var/lib/docker/containers/15acaf3a4cff4fa3ab73ca407a93cf040410a33919cdf2fbd0479f7eb6c86f98/hostname", "HostsPath": "/var/lib/docker/containers/15acaf3a4cff4fa3ab73ca407a93cf040410a33919cdf2fbd0479f7eb6c86f98/hosts", "LogPath": "/var/lib/docker/containers/15acaf3a4cff4fa3ab73ca407a93cf040410a33919cdf2fbd0479f7eb6c86f98/15acaf3a4cff4fa3ab73ca407a93cf040410a33919cdf2fbd0479f7eb6c86f98-json.log", "Name": "/myNginx-test05", "RestartCount": 0, "Driver": "overlay2", "Platform": "linux", "MountLabel": "", "ProcessLabel": "", "AppArmorProfile": "docker-default", "ExecIDs": null, "HostConfig": { "Binds": null, "ContainerIDFile": "", "LogConfig": { "Type": "json-file", "Config": {} }, "NetworkMode": "default", "PortBindings": { "443/tcp": [ { "HostIp": "", "HostPort": "50002" } ], "53/udp": [ { "HostIp": "", "HostPort": "50003" } ], "80/udp": [ { "HostIp": "172.16.1.15", "HostPort": "50001" } ] }, "RestartPolicy": { "Name": "no", "MaximumRetryCount": 0 }, "AutoRemove": false, "VolumeDriver": "", "VolumesFrom": null, "CapAdd": null, "CapDrop": null, "CgroupnsMode": "host", "Dns": [], "DnsOptions": [], "DnsSearch": [], "ExtraHosts": null, "GroupAdd": null, "IpcMode": "private", "Cgroup": "", "Links": null, "OomScoreAdj": 0, "PidMode": "", "Privileged": false, "PublishAllPorts": false, "ReadonlyRootfs": false, "SecurityOpt": null, "UTSMode": "", "UsernsMode": "", "ShmSize": 67108864, "Runtime": "runc", "ConsoleSize": [ 0, 0 ], "Isolation": "", "CpuShares": 0, "Memory": 0, "NanoCpus": 0, "CgroupParent": "", "BlkioWeight": 0, "BlkioWeightDevice": [], "BlkioDeviceReadBps": null, "BlkioDeviceWriteBps": null, "BlkioDeviceReadIOps": null, "BlkioDeviceWriteIOps": null, "CpuPeriod": 0, "CpuQuota": 0, "CpuRealtimePeriod": 0, "CpuRealtimeRuntime": 0, "CpusetCpus": "", "CpusetMems": "", "Devices": [], "DeviceCgroupRules": null, "DeviceRequests": null, "KernelMemory": 0, "KernelMemoryTCP": 0, "MemoryReservation": 0, "MemorySwap": 0, "MemorySwappiness": null, "OomKillDisable": false, "PidsLimit": null, "Ulimits": null, "CpuCount": 0, "CpuPercent": 0, "IOMaximumIOps": 0, "IOMaximumBandwidth": 0, "MaskedPaths": [ "/proc/asound", "/proc/acpi", "/proc/kcore", "/proc/keys", "/proc/latency_stats", "/proc/timer_list", "/proc/timer_stats", "/proc/sched_debug", "/proc/scsi", "/sys/firmware" ], "ReadonlyPaths": [ "/proc/bus", "/proc/fs", "/proc/irq", "/proc/sys", "/proc/sysrq-trigger" ] }, "GraphDriver": { "Data": { "LowerDir": "/var/lib/docker/overlay2/26c80ddc859369aa91dc6b6851dbba06ee5c279d943ae35a9887ce136b215f21-init/diff:/var/lib/docker/overlay2/5a7618ea5a0fa112e17a4bb1c9dee7ca22cec7d74c1b678dbc23ac4930b3a180/diff:/var/lib/docker/overlay2/3be83da644a2a178cfe60f1988b081822660589a317a3e651e5103c63e84d161/diff:/var/lib/docker/overlay2/eabfcaff677298c8de8e747449a2e6b2a2e880d1b209df79471b25ac8319226d/diff:/var/lib/docker/overlay2/16040bf8bb5a6bfe2725e4ee9937301ed432cf2e54be2f65915779326de181c2/diff:/var/lib/docker/overlay2/1e03d3aff8d8f00cfe8807317dac5ff3a85df649c719c4298c3dd59c4e6a48f6/diff:/var/lib/docker/overlay2/87f90de43c71bef203cd6759ac817746e575575171cb644b6038a3ced9f544aa/diff", "MergedDir": "/var/lib/docker/overlay2/26c80ddc859369aa91dc6b6851dbba06ee5c279d943ae35a9887ce136b215f21/merged", "UpperDir": "/var/lib/docker/overlay2/26c80ddc859369aa91dc6b6851dbba06ee5c279d943ae35a9887ce136b215f21/diff", "WorkDir": "/var/lib/docker/overlay2/26c80ddc859369aa91dc6b6851dbba06ee5c279d943ae35a9887ce136b215f21/work" }, "Name": "overlay2" }, "Mounts": [], "Config": { "Hostname": "15acaf3a4cff", "Domainname": "", "User": "", "AttachStdin": false, "AttachStdout": false, "AttachStderr": false, "ExposedPorts": { "443/tcp": {}, "53/udp": {}, "80/tcp": {}, "80/udp": {} }, "Tty": true, "OpenStdin": true, "StdinOnce": false, "Env": [ "PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin", "NGINX_VERSION=1.21.5", "NJS_VERSION=0.7.1", "PKG_RELEASE=1~bullseye" ], "Cmd": [ "nginx", "-g", "daemon off;" ], "Image": "nginx", "Volumes": null, "WorkingDir": "", "Entrypoint": [ "/docker-entrypoint.sh" ], "OnBuild": null, "Labels": { "maintainer": "NGINX Docker Maintainers <docker-maint@nginx.com>" }, "StopSignal": "SIGQUIT" }, "NetworkSettings": { "Bridge": "", "SandboxID": "c3a326db64307dd66a8c559856c3e81b1314ae9ca27deb30e88fd71a5fddf1ca", "HairpinMode": false, "LinkLocalIPv6Address": "", "LinkLocalIPv6PrefixLen": 0, "Ports": { "443/tcp": [ { "HostIp": "0.0.0.0", "HostPort": "50002" }, { "HostIp": "::", "HostPort": "50002" } ], "53/udp": [ { "HostIp": "0.0.0.0", "HostPort": "50003" }, { "HostIp": "::", "HostPort": "50003" } ], "80/tcp": null, "80/udp": [ { "HostIp": "172.16.1.15", "HostPort": "50001" } ] }, "SandboxKey": "/var/run/docker/netns/c3a326db6430", "SecondaryIPAddresses": null, "SecondaryIPv6Addresses": null, "EndpointID": "ca79902b0c3e8e90c2d4c3300cf1546dbeefb6d601b8e31c10aa423d4768e317", "Gateway": "172.17.0.1", "GlobalIPv6Address": "", "GlobalIPv6PrefixLen": 0, "IPAddress": "172.17.0.6", "IPPrefixLen": 16, "IPv6Gateway": "", "MacAddress": "02:42:ac:11:00:06", "Networks": { "bridge": { "IPAMConfig": null, "Links": null, "Aliases": null, "NetworkID": "5317e48bd29cc927dccc4efcfab6eeb695a7b932311f55c13a891028c7b8fb9c", "EndpointID": "ca79902b0c3e8e90c2d4c3300cf1546dbeefb6d601b8e31c10aa423d4768e317", "Gateway": "172.17.0.1", "IPAddress": "172.17.0.6", "IPPrefixLen": 16, "IPv6Gateway": "", "GlobalIPv6Address": "", "GlobalIPv6PrefixLen": 0, "MacAddress": "02:42:ac:11:00:06", "DriverOpts": null } } } }]root@node15:~#### 也可以基于容器ID查看容器的详细信息root@node15:~# docker container ls -aCONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES15acaf3a4cff nginx "/docker-entrypoint.…" 15 minutes ago Up 15 minutes 80/tcp, 0.0.0.0:50003->53/udp, :::50003->53/udp, 172.16.1.15:50001->80/udp, 0.0.0.0:50002->443/tcp, :::50002->443/tcp myNginx-test05root@node15:~# docker container inspect 15acaf3a4cff
5.2 获取容器的IP地址
root@node15:~# docker container ls -aCONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES15acaf3a4cff nginx "/docker-entrypoint.…" 16 minutes ago Up 16 minutes 80/tcp, 0.0.0.0:50003->53/udp, :::50003->53/udp, 172.16.1.15:50001->80/udp, 0.0.0.0:50002->443/tcp, :::50002->443/tcp myNginx-test05root@node15:~# docker container inspect -f "{{.NetworkSettings.IPAddress}}" myNginx-test05172.17.0.6root@node15:~# docker container inspect -f "{{.NetworkSettings.IPAddress}}" 15acaf3a4cff172.17.0.6root@node15:~#
5.3 获取容器的PID信息
root@node15:~# docker container ls -aCONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES15acaf3a4cff nginx "/docker-entrypoint.…" 18 minutes ago Up 18 minutes 80/tcp, 0.0.0.0:50003->53/udp, :::50003->53/udp, 172.16.1.15:50001->80/udp, 0.0.0.0:50002->443/tcp, :::50002->443/tcp myNginx-test05root@node15:~# docker container inspect -f "{{.State.Pid}}" 15acaf3a4cff2855root@node15:~# docker container inspect -f "{{.State.Pid}}" myNginx-test052855root@node15:~#
6. 进入到正在运行的容器
6.1 使用attach命令连接正在运行的容器(生产环境不推荐使用,因为使用exit退出容器后容器会跟着自动关闭)
root@node15:~# docker container ls -aCONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES39cb32823eb0 nginx "/docker-entrypoint.…" About a minute ago Up About a minute 80/tcp myNginx-test01root@node15:~# docker container attach 39cb32823eb0root@39cb32823eb0:/# ls /bin dev docker-entrypoint.sh home lib64 mnt proc run srv tmp varboot docker-entrypoint.d etc lib media opt root sbin sys usrroot@39cb32823eb0:/# dateWed Mar 2 01:51:16 UTC 2022root@39cb32823eb0:/# date +%Y-%m-%d-%H:%M:%S2022-03-02-01:51:28root@39cb32823eb0:/# exitexitroot@node15:~# docker container ls -aCONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES39cb32823eb0 nginx "/docker-entrypoint.…" 3 minutes ago Exited (0) About a minute ago myNginx-test01root@node15:~#
6.2 使用exec命令(生产环境推荐使用这种方式)
root@node15:~# docker container ls -aCONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES39cb32823eb0 nginx "/docker-entrypoint.…" 7 minutes ago Up 3 seconds 80/tcp myNginx-test01root@node15:~# docker container exec -it myNginx-test01 bashroot@39cb32823eb0:/# cat /etc/hosts 127.0.0.1 localhost::1 localhost ip6-localhost ip6-loopbackfe00::0 ip6-localnetff00::0 ip6-mcastprefixff02::1 ip6-allnodesff02::2 ip6-allrouters172.17.0.2 39cb32823eb0root@39cb32823eb0:/# exitexitroot@node15:~# docker container ls -aCONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES39cb32823eb0 nginx "/docker-entrypoint.…" 8 minutes ago Up 49 seconds 80/tcp myNginx-test01root@node15:~#
6.3 使用nsenter命令(生产环境也推荐使用这种方式,但需要单独安装nsenter命令)
root@node15:~# apt-get install util-linux #安装nsenter命令root@node15:~# docker container ls -aCONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES39cb32823eb0 nginx "/docker-entrypoint.…" 18 minutes ago Up 10 minutes 80/tcp myNginx-test01root@node15:~# docker container inspect -f "{{.State.Pid}}" myNginx-test01 #获取容器PID2559root@node15:~# nsenter -t 2559 -m -u -i -n -p #通过容器的PID连接运行中的容器root@39cb32823eb0:/# cat /etc/issueDebian GNU/Linux 11 \n \lroot@39cb32823eb0:/# cat /etc/resolv.conf # This file is managed by man:systemd-resolved(8). Do not edit.## This is a dynamic resolv.conf file for connecting local clients directly to# all known uplink DNS servers. This file lists all configured search domains.## Third party programs must not access this file directly, but only through the# symlink at /etc/resolv.conf. To manage man:resolv.conf(5) in a different way,# replace this symlink by a static file or a different symlink.## See man:systemd-resolved.service(8) for details about the supported modes of# operation for /etc/resolv.conf.nameserver 114.114.114.114root@39cb32823eb0:/# exitlogoutroot@node15:~# docker container ls -aCONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES39cb32823eb0 nginx "/docker-entrypoint.…" 19 minutes ago Up 12 minutes 80/tcp myNginx-test01root@node15:~#
6.4 脚本方式连接容器
root@node15:/data/scripts# pwd /data/scriptsroot@node15:/data/scripts# cat docker_connect.sh #!/bin/bashfunction docker_connect(){ CONTAINER_NAME=$1 CONTAINER_PID=$(docker container inspect -f "{{.State.Pid}}" ${CONTAINER_NAME}) nsenter -t ${CONTAINER_PID} -m -u -i -n -p}docker_connect $1root@node15:/data/scripts#root@node15:/data/scripts# ll docker_connect.sh -rw-r--r-- 1 root root 582 Mar 2 10:19 docker_connect.shroot@node15:/data/scripts# chmod +x docker_connect.sh root@node15:/data/scripts# ll docker_connect.sh -rwxr-xr-x 1 root root 582 Mar 2 10:19 docker_connect.sh*root@node15:/data/scripts# docker container ls -aCONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES39cb32823eb0 nginx "/docker-entrypoint.…" 33 minutes ago Up 25 minutes 80/tcp myNginx-test01root@node15:/data/scripts# lltotal 12drwxr-xr-x 2 root root 4096 Mar 2 10:22 ./drwxr-xr-x 3 root root 4096 Mar 2 10:16 ../-rwxr-xr-x 1 root root 582 Mar 2 10:19 docker_connect.sh*root@node15:/data/scripts# ./docker_connect.sh myNginx-test01root@39cb32823eb0:/# cat /etc/issueDebian GNU/Linux 11 \n \lroot@39cb32823eb0:/# date +%F2022-03-02root@39cb32823eb0:/# exitlogoutroot@node15:/data/scripts# docker container ls -aCONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES39cb32823eb0 nginx "/docker-entrypoint.…" 34 minutes ago Up 26 minutes 80/tcp myNginx-test01root@node15:/data/scripts#