1. 从镜像启动一个容器
1.1 前台启动容器
root@node16:~# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
root@node16:~# docker pull nginx
Using default tag: latest
latest: Pulling from library/nginx
a2abf6c4d29d: Already exists
a9edb18cadd1: Already exists
589b7251471a: Already exists
186b1aaa4aa6: Already exists
b4df32aa5a72: Already exists
a0bcbecc962e: Already exists
Digest: sha256:0d17b565c37bcbd895e9d92315a05c1c3c9a29f762b011a10c54a66cd53c9b31
Status: Downloaded newer image for nginx:latest
docker.io/library/nginx:latest
root@node16:~# docker container ls -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
root@node16:~#
root@node16:~# docker container ls
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
22bfc1251066 nginx "/docker-entrypoint.…" 33 seconds ago Up 30 seconds 80/tcp optimistic_brahmagupta
root@node16:~#
1.2 后台启动容器
### 后台启动容器,并生成随机的容器ID和名称
root@node16:~# docker container ls -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
22bfc1251066 nginx "/docker-entrypoint.…" 5 minutes ago Exited (0) About a minute ago optimistic_brahmagupta
root@node16:~# docker run -d nginx
e9251fe9a682fe2d5b2a6ca0c1984565d66af5ea0f9dcfa2124d854e3b75ac08
root@node16:~#
root@node16:~# docker container ls -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
e9251fe9a682 nginx "/docker-entrypoint.…" 12 seconds ago Up 10 seconds 80/tcp heuristic_newton
22bfc1251066 nginx "/docker-entrypoint.…" 6 minutes ago Exited (0) About a minute ago optimistic_brahmagupta
root@node16:~#
root@node16:~# docker container ls
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
e9251fe9a682 nginx "/docker-entrypoint.…" 29 seconds ago Up 28 seconds 80/tcp heuristic_newton
root@node16:~#
1.3 创建容器时并进入容器(依赖于"-i"(保持一个标准输入)和"-t"(分配一个tty终端)两个参数)
### 会打开一个bash并直接进入到容器,并随机生成容器ID和名称
root@node16:~# docker image ls
REPOSITORY TAG IMAGE ID CREATED SIZE
nginx latest 605c77e624dd 2 months ago 141MB
root@node16:~# docker run -it nginx bash
root@e3b823472f5e:/#
root@e3b823472f5e:/# cat /etc/issue
Debian GNU/Linux 11 \n \l
root@e3b823472f5e:/#
root@e3b823472f5e:/# uname -a
Linux e3b823472f5e 4.15.0-112-generic #113-Ubuntu SMP Thu Jul 9 23:41:39 UTC 2020 x86_64 GNU/Linux
root@e3b823472f5e:/# exit
exit
root@node16:~# uname -a
Linux node16 4.15.0-112-generic #113-Ubuntu SMP Thu Jul 9 23:41:39 UTC 2020 x86_64 x86_64 x86_64 GNU/Linux
root@node16:~#
1.4 自定义容器名称(一般在单机上会有点作用,在k8s集群中很少去自定义容器名称)
root@node16:~# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
nginx latest 605c77e624dd 2 months ago 141MB
root@node16:~# docker container ls -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
b4d22577de0f nginx "/docker-entrypoint.…" 20 seconds ago Up 19 seconds 80/tcp mynginx
e3b823472f5e nginx "/docker-entrypoint.…" 3 minutes ago Exited (0) 3 minutes ago strange_mcnulty
e9251fe9a682 nginx "/docker-entrypoint.…" 9 minutes ago Up 9 minutes 80/tcp heuristic_newton
22bfc1251066 nginx "/docker-entrypoint.…" 15 minutes ago Exited (0) 10 minutes ago optimistic_brahmagupta
root@node16:~# docker run --name my_nginx -d nginx
fe311fd2419c13222f416a86523ceaf3ea563aff71ff6ee9a25b2069a2222ce2
root@node16:~# docker container ls -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
fe311fd2419c nginx "/docker-entrypoint.…" 3 seconds ago Up 2 seconds 80/tcp my_nginx
b4d22577de0f nginx "/docker-entrypoint.…" 40 seconds ago Up 39 seconds 80/tcp mynginx
e3b823472f5e nginx "/docker-entrypoint.…" 4 minutes ago Exited (0) 3 minutes ago strange_mcnulty
e9251fe9a682 nginx "/docker-entrypoint.…" 9 minutes ago Up 9 minutes 80/tcp heuristic_newton
22bfc1251066 nginx "/docker-entrypoint.…" 15 minutes ago Exited (0) 10 minutes ago optimistic_brahmagupta
root@node16:~#
1.5 单次运行容器(容器退出后会自动删除,主要用于临时验证镜像内容是否符合标准,如配置文件目录组织结构等信息)
root@node16:~# docker run -it --rm --name V1nginx nginx bash
root@d46480b32cb4:/# exit
exit
root@node16:~# docker container ls -a |grep V1nginx #发现容器没有了
root@node16:~# docker container ls -a
1.6 批量强制关闭正在运行的容器
root@node16:~# docker container ls -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
fe311fd2419c nginx "/docker-entrypoint.…" 12 minutes ago Up 12 minutes 80/tcp my_nginx
b4d22577de0f nginx "/docker-entrypoint.…" 12 minutes ago Up 12 minutes 80/tcp mynginx
e3b823472f5e nginx "/docker-entrypoint.…" 16 minutes ago Exited (0) 15 minutes ago strange_mcnulty
e9251fe9a682 nginx "/docker-entrypoint.…" 21 minutes ago Up 21 minutes 80/tcp heuristic_newton
22bfc1251066 nginx "/docker-entrypoint.…" 27 minutes ago Exited (0) 22 minutes ago optimistic_brahmagupta
root@node16:~# docker container ps -a -q
fe311fd2419c
b4d22577de0f
e3b823472f5e
e9251fe9a682
22bfc1251066
root@node16:~#
root@node16:~# docker kill $(docker container ps -a -q)
fe311fd2419c
b4d22577de0f
e9251fe9a682
Error response from daemon: Cannot kill container: e3b823472f5e: Container e3b823472f5e4b0de10b98b9322800330eafb2cd8187031b4de1173687da7578 is not running
Error response from daemon: Cannot kill container: 22bfc1251066: Container 22bfc125106624d7540d23d9ee3a2d3fe2c35a1be1cf1745a4f95033b8706f9d is not running
root@node16:~# docker container ls -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
fe311fd2419c nginx "/docker-entrypoint.…" 13 minutes ago Exited (137) 16 seconds ago my_nginx
b4d22577de0f nginx "/docker-entrypoint.…" 13 minutes ago Exited (137) 16 seconds ago mynginx
e3b823472f5e nginx "/docker-entrypoint.…" 17 minutes ago Exited (0) 16 minutes ago strange_mcnulty
e9251fe9a682 nginx "/docker-entrypoint.…" 22 minutes ago Exited (137) 16 seconds ago heuristic_newton
22bfc1251066 nginx "/docker-entrypoint.…" 28 minutes ago Exited (0) 23 minutes ago optimistic_brahmagupta
root@node16:~#
1.7 批量删除所有容器
root@node16:~# docker container ls -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
fe311fd2419c nginx "/docker-entrypoint.…" 16 minutes ago Exited (137) 3 minutes ago my_nginx
b4d22577de0f nginx "/docker-entrypoint.…" 16 minutes ago Exited (137) 3 minutes ago mynginx
e3b823472f5e nginx "/docker-entrypoint.…" 20 minutes ago Exited (0) 19 minutes ago strange_mcnulty
e9251fe9a682 nginx "/docker-entrypoint.…" 25 minutes ago Exited (137) 3 minutes ago heuristic_newton
22bfc1251066 nginx "/docker-entrypoint.…" 31 minutes ago Exited (0) 26 minutes ago optimistic_brahmagupta
root@node16:~# docker ps -a -q
fe311fd2419c
b4d22577de0f
e3b823472f5e
e9251fe9a682
22bfc1251066
root@node16:~#
root@node16:~# docker container rm -f `docker container ps -a -q`
fe311fd2419c
b4d22577de0f
e3b823472f5e
e9251fe9a682
22bfc1251066
root@node16:~# docker container ls -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
root@node16:~#
2. 查看容器
2.1 查看正在运行的容器
root@node16:~# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
75effa3b5d32 nginx "/docker-entrypoint.…" 5 seconds ago Up 3 seconds 80/tcp brave_edison
22d3fe040ab5 nginx "/docker-entrypoint.…" 13 seconds ago Up 12 seconds 80/tcp serene_sinoussi
root@node16:~# docker container ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
75effa3b5d32 nginx "/docker-entrypoint.…" 18 seconds ago Up 16 seconds 80/tcp brave_edison
22d3fe040ab5 nginx "/docker-entrypoint.…" 26 seconds ago Up 24 seconds 80/tcp serene_sinoussi
root@node16:~#
2.2 查看所有容器(包含当前正在运行以及关闭的所有容器)
root@node16:~# docker container ps --help
Usage: docker container ls [OPTIONS]
List containers
Aliases:
ls, ps, list
Options:
-a, --all Show all containers (default shows just running)
-f, --filter filter Filter output based on conditions provided
--format string Pretty-print containers using a Go template
-n, --last int Show n last created containers (includes all states) (default -1)
-l, --latest Show the latest created container (includes all states)
--no-trunc Don't truncate output
-q, --quiet Only display container IDs
-s, --size Display total file sizes
root@node16:~#
root@node16:~# docker ps --help
Usage: docker ps [OPTIONS]
List containers
Options:
-a, --all Show all containers (default shows just running)
-f, --filter filter Filter output based on conditions provided
--format string Pretty-print containers using a Go template
-n, --last int Show n last created containers (includes all states) (default -1)
-l, --latest Show the latest created container (includes all states)
--no-trunc Don't truncate output
-q, --quiet Only display container IDs
-s, --size Display total file sizes
root@node16:~# docker container ls --help
Usage: docker container ls [OPTIONS]
List containers
Aliases:
ls, ps, list
Options:
-a, --all Show all containers (default shows just running)
-f, --filter filter Filter output based on conditions provided
--format string Pretty-print containers using a Go template
-n, --last int Show n last created containers (includes all states) (default -1)
-l, --latest Show the latest created container (includes all states)
--no-trunc Don't truncate output
-q, --quiet Only display container IDs
-s, --size Display total file sizes
root@node16:~# docker container ls
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
75effa3b5d32 nginx "/docker-entrypoint.…" 4 minutes ago Up 4 minutes 80/tcp brave_edison
22d3fe040ab5 nginx "/docker-entrypoint.…" 5 minutes ago Up 5 minutes 80/tcp serene_sinoussi
root@node16:~#
root@node16:~# docker container list --help
Usage: docker container ls [OPTIONS]
List containers
Aliases:
ls, ps, list
Options:
-a, --all Show all containers (default shows just running)
-f, --filter filter Filter output based on conditions provided
--format string Pretty-print containers using a Go template
-n, --last int Show n last created containers (includes all states) (default -1)
-l, --latest Show the latest created container (includes all states)
--no-trunc Don't truncate output
-q, --quiet Only display container IDs
-s, --size Display total file sizes
root@node16:~#
root@node16:~# docker container list
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
75effa3b5d32 nginx "/docker-entrypoint.…" 6 minutes ago Up 6 minutes 80/tcp brave_edison
22d3fe040ab5 nginx "/docker-entrypoint.…" 6 minutes ago Up 6 minutes 80/tcp serene_sinoussi
root@node16:~#
root@node16:~# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
cdd03684811a nginx "/docker-entrypoint.…" 16 seconds ago Exited (0) 12 seconds ago admiring_hawking
75effa3b5d32 nginx "/docker-entrypoint.…" 7 minutes ago Up 7 minutes 80/tcp brave_edison
22d3fe040ab5 nginx "/docker-entrypoint.…" 7 minutes ago Up 7 minutes 80/tcp serene_sinoussi
root@node16:~# docker container ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
cdd03684811a nginx "/docker-entrypoint.…" 26 seconds ago Exited (0) 22 seconds ago admiring_hawking
75effa3b5d32 nginx "/docker-entrypoint.…" 7 minutes ago Up 7 minutes 80/tcp brave_edison
22d3fe040ab5 nginx "/docker-entrypoint.…" 7 minutes ago Up 7 minutes 80/tcp serene_sinoussi
root@node16:~#
3. 删除运行中的容器
3.1 删除单个容器
root@Kubernetes:~# docker container rm --help
Usage: docker container rm [OPTIONS] CONTAINER [CONTAINER...]
Remove one or more containers
Options:
-f, --force Force the removal of a running container (uses SIGKILL)
-l, --link Remove the specified link
-v, --volumes Remove anonymous volumes associated with the container
root@Kubernetes:~# root@Kubernetes:~# docker container rm --help
Usage: docker container rm [OPTIONS] CONTAINER [CONTAINER...]
Remove one or more containers
Options:
-f, --force Force the removal of a running container (uses SIGKILL)
-l, --link Remove the specified link
-v, --volumes Remove anonymous volumes associated with the container
root@Kubernetes:~#
root@Kubernetes:~# docker rm --help
Usage: docker rm [OPTIONS] CONTAINER [CONTAINER...]
Remove one or more containers
Options:
-f, --force Force the removal of a running container (uses SIGKILL)
-l, --link Remove the specified link
-v, --volumes Remove anonymous volumes associated with the container
root@Kubernetes:~#
root@node16:~# docker container ls -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
b2be2056e8cc nginx "/docker-entrypoint.…" 12 seconds ago Up 10 seconds 80/tcp lucid_hawking
a92ef63990b3 nginx "/docker-entrypoint.…" 28 seconds ago Up 26 seconds 80/tcp strange_franklin
2a8c66062b02 alpine "/bin/sh" 2 minutes ago Exited (0) 2 minutes ago cool_chaplygin
cdd03684811a nginx "/docker-entrypoint.…" 22 hours ago Exited (0) 22 hours ago admiring_hawking
75effa3b5d32 nginx "/docker-entrypoint.…" 23 hours ago Exited (0) 22 hours ago brave_edison
22d3fe040ab5 nginx "/docker-entrypoint.…" 23 hours ago Exited (0) 22 hours ago serene_sinoussi
root@node16:~#
root@node16:~# docker container rm -f b2be2056e8cc #基于容器ID强制删除该容器
b2be2056e8cc
root@node16:~#
root@node16:~# docker rm -f 22d3fe040ab5
22d3fe040ab5
root@node16:~# docker container ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
a92ef63990b3 nginx "/docker-entrypoint.…" 2 minutes ago Up 2 minutes
3.2 删除多个容器
root@node16:~# docker container ls -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
a92ef63990b3 nginx "/docker-entrypoint.…" 7 minutes ago Up 7 minutes 80/tcp strange_franklin
cdd03684811a nginx "/docker-entrypoint.…" 23 hours ago Exited (0) 23 hours ago admiring_hawking
root@node16:~# docker container rm -f a92ef63990b3 cdd03684811a
a92ef63990b3
cdd03684811a
root@node16:~# docker container ls -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
3.3 批量删除状态为已退出的容器
root@node16:~# docker container ls -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
f953b556817b alpine "/bin/sh" 50 seconds ago Exited (0) 49 seconds ago agitated_panini
089167ee6bf2 alpine "/bin/sh" 51 seconds ago Exited (0) 50 seconds ago romantic_brattain
008b6448950e alpine "/bin/sh" 53 seconds ago Exited (0) 51 seconds ago romantic_cerf
714c14ea777e alpine "/bin/sh" 54 seconds ago Exited (0) 53 seconds ago intelligent_tharp
83a11c1e556c alpine "/bin/sh" 56 seconds ago Exited (0) 54 seconds ago brave_robinson
4c829a31213e nginx "/docker-entrypoint.…" About a minute ago Up About a minute 80/tcp funny_wing
83aabe7b8b22 nginx "/docker-entrypoint.…" About a minute ago Up About a minute 80/tcp awesome_mayer
7c2f4e32e545 nginx "/docker-entrypoint.…" About a minute ago Up About a minute 80/tcp adoring_noether
98e39c1a2abf nginx "/docker-entrypoint.…" About a minute ago Up About a minute
root@node16:~# docker container ps -aq -f status=exited
f953b556817b
089167ee6bf2
008b6448950e
714c14ea777e
83a11c1e556c
root@node16:~#
root@node16:~# docker container rm -f `docker container ps -qa -f status=exited`
f953b556817b
089167ee6bf2
008b6448950e
714c14ea777e
83a11c1e556c
root@node16:~# docker container ls -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
4c829a31213e nginx "/docker-entrypoint.…" 2 minutes ago Up 2 minutes 80/tcp funny_wing
83aabe7b8b22 nginx "/docker-entrypoint.…" 2 minutes ago Up 2 minutes 80/tcp awesome_mayer
7c2f4e32e545 nginx "/docker-entrypoint.…" 3 minutes ago Up 2 minutes 80/tcp adoring_noether
98e39c1a2abf nginx "/docker-entrypoint.…" 3 minutes ago Up 2 minutes 80/tcp exciting_visvesvaraya
f406b036985d nginx "/docker-entrypoint.…" 3 minutes ago Up 3 minutes 80/tcp clever_mahavira
7bcab6a5489f nginx "/docker-entrypoint.…" 3 minutes ago Up 3 minutes 80/tcp interesting_wu
159adb663185 nginx "/docker-entrypoint.…" 3 minutes ago Up 3 minutes 80/tcp kind_knuth
aeaa19272e8e nginx "/docker-entrypoint.…" 3 minutes ago Up 3 minutes 80/tcp hopeful_wilson
367a9a3e0f59 nginx "/docker-entrypoint.…" 3 minutes ago Up 3 minutes 80/tcp jolly_jennings
root@node16:~#
3.4 批量关闭正在运行的容器
root@node16:~# docker container ls -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
7bcab6a5489f nginx "/docker-entrypoint.…" 8 minutes ago Up 8 minutes 80/tcp interesting_wu
159adb663185 nginx "/docker-entrypoint.…" 8 minutes ago Up 8 minutes 80/tcp kind_knuth
aeaa19272e8e nginx "/docker-entrypoint.…" 8 minutes ago Up 8 minutes 80/tcp hopeful_wilson
367a9a3e0f59 nginx "/docker-entrypoint.…" 8 minutes ago Up 8 minutes 80/tcp jolly_jennings
root@node16:~# docker ps -a -q
7bcab6a5489f
159adb663185
aeaa19272e8e
367a9a3e0f59
root@node16:~# docker stop $(docker container ps -a -q)
7bcab6a5489f
159adb663185
aeaa19272e8e
367a9a3e0f59
root@node16:~# docker container ls -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
7bcab6a5489f nginx "/docker-entrypoint.…" 9 minutes ago Exited (0) 7 seconds ago interesting_wu
159adb663185 nginx "/docker-entrypoint.…" 9 minutes ago Exited (0) 7 seconds ago kind_knuth
aeaa19272e8e nginx "/docker-entrypoint.…" 9 minutes ago Exited (0) 7 seconds ago hopeful_wilson
367a9a3e0f59 nginx "/docker-entrypoint.…" 9 minutes ago Exited (0) 7 seconds ago jolly_jennings
root@node16:~#
3.5 批量开启关闭的容器
root@node16:~# docker container ls -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
7bcab6a5489f nginx "/docker-entrypoint.…" 13 minutes ago Exited (0) 4 minutes ago interesting_wu
159adb663185 nginx "/docker-entrypoint.…" 13 minutes ago Exited (0) 4 minutes ago kind_knuth
aeaa19272e8e nginx "/docker-entrypoint.…" 13 minutes ago Exited (0) 4 minutes ago hopeful_wilson
root@node16:~# docker container ps -a -q -f status=exited
7bcab6a5489f
159adb663185
aeaa19272e8e
root@node16:~# docker start `docker container ps -q -a -f status=exited`
7bcab6a5489f
159adb663185
aeaa19272e8e
root@node16:~# docker container ls -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
7bcab6a5489f nginx "/docker-entrypoint.…" 14 minutes ago Up 9 seconds 80/tcp interesting_wu
159adb663185 nginx "/docker-entrypoint.…" 14 minutes ago Up 9 seconds 80/tcp kind_knuth
aeaa19272e8e nginx "/docker-entrypoint.…" 14 minutes ago Up 8 seconds 80/tcp hopeful_wilson
root@node16:~#
3.6 批量强制关闭正在运行的容器
root@node16:~# docker container ls -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
7bcab6a5489f nginx "/docker-entrypoint.…" 16 minutes ago Up 2 minutes 80/tcp interesting_wu
159adb663185 nginx "/docker-entrypoint.…" 16 minutes ago Up 2 minutes 80/tcp kind_knuth
aeaa19272e8e nginx "/docker-entrypoint.…" 16 minutes ago Up 2 minutes 80/tcp hopeful_wilson
root@node16:~# docker container ps -a -q
7bcab6a5489f
159adb663185
aeaa19272e8e
root@node16:~# docker kill $(docker container ps -a -q)
7bcab6a5489f
159adb663185
aeaa19272e8e
root@node16:~# docker container ls -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
7bcab6a5489f nginx "/docker-entrypoint.…" 17 minutes ago Exited (137) 12 seconds ago interesting_wu
159adb663185 nginx "/docker-entrypoint.…" 17 minutes ago Exited (137) 12 seconds ago kind_knuth
aeaa19272e8e nginx "/docker-entrypoint.…" 17 minutes ago Exited (137) 12 seconds ago hopeful_wilson
root@node16:~#
3.7 批量删除所有容器
root@node16:~# docker container ls -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
7bcab6a5489f nginx "/docker-entrypoint.…" 19 minutes ago Exited (137) 2 minutes ago interesting_wu
159adb663185 nginx "/docker-entrypoint.…" 19 minutes ago Exited (137) 2 minutes ago kind_knuth
aeaa19272e8e nginx "/docker-entrypoint.…" 19 minutes ago Exited (137) 2 minutes ago hopeful_wilson
root@node16:~# docker container ps -a -q
7bcab6a5489f
159adb663185
aeaa19272e8e
root@node16:~# docker container rm -f $(docker container ps -a -q)
7bcab6a5489f
159adb663185
aeaa19272e8e
root@node16:~# docker container ls -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
root@node16:~#
4. 容器端口映射
4.1 随机端口映射
root@node16:~# docker container ls -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
c23a0456d6a6 nginx "/docker-entrypoint.…" 5 seconds ago Up 3 seconds 80/tcp happy_bassi
root@node16:~# docker container rm -f c23a0456d6a6
c23a0456d6a6
root@node16:~# docker image ls
REPOSITORY TAG IMAGE ID CREATED SIZE
nginx latest 605c77e624dd 2 months ago 141MB
alpine latest c059bfaa849c 3 months ago 5.59MB
root@node16:~# docker container ls -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
root@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.sh
10-listen-on-ipv6-by-default.sh: info: Getting the checksum of /etc/nginx/conf.d/default.conf
10-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 up
2022/03/01 13:28:31 [notice] 1#1: using the "epoll" event method
2022/03/01 13:28:31 [notice] 1#1: nginx/1.21.5
2022/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-generic
2022/03/01 13:28:31 [notice] 1#1: getrlimit(RLIMIT_NOFILE): 1048576:1048576
2022/03/01 13:28:31 [notice] 1#1: start worker processes
2022/03/01 13:28:31 [notice] 1#1: start worker process 32
2022/03/01 13:28:31 [notice] 1#1: start worker process 33
4.2 指定端口映射
root@node15:~# hostname -i
172.16.1.15
root@node15:~# docker run -p 10001:80 --name myNginx-test01 -it -d nginx #将本地所有地址的10001端口映射到容器的80端口
4c123f46e38f7246fc74725082737ce933f0faa352c015113833804272c87b23
root@node15:~# docker run -p 172.16.1.15:20002:80 --name myNginx-test02 -it -d nginx #将本地172.16.1.15对应的IP地址的20002端口映射到容器的80端口
e90410d961d576ce7fb6cf6c2dda8f97703d55c07dbc572c0578e5c8b3d64ba3
root@node15:~# docker run -p 172.16.1.15::80 --name myNginx-test03 -it -d nginx #将本地172.16.1.15对应的IP地址的随机端口映射到容器的80端口
3c6eae128f3455f2fca232900d7627ab4cd9a99ef4baaf21a26fa3a43e9b1352
root@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端口
43c85684930a13492c1665d99af2c61929f5f2b2e7aaf3091c0e9fb86be1aa75
root@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 #还可以一次性映射多个端口/协议
15acaf3a4cff4fa3ab73ca407a93cf040410a33919cdf2fbd0479f7eb6c86f98
root@node15:~# docker container ls -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
15acaf3a4cff 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-test05
43c85684930a nginx "/docker-entrypoint.…" About a minute ago Up About a minute 80/tcp, 172.16.1.15:40004->80/udp myNginx-test04
3c6eae128f34 nginx "/docker-entrypoint.…" 2 minutes ago Up 2 minutes 172.16.1.15:49153->80/tcp myNginx-test03
e90410d961d5 nginx "/docker-entrypoint.…" 2 minutes ago Up 2 minutes 172.16.1.15:20002->80/tcp myNginx-test02
4c123f46e38f nginx "/docker-entrypoint.…" 3 minutes ago Up 3 minutes 0.0.0.0:10001->80/tcp, :::10001->80/tcp myNginx-test01
root@node15:~#
4.3 查看容器已经映射的端口
root@node15:~# docker container ls -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
15acaf3a4cff 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-test05
43c85684930a nginx "/docker-entrypoint.…" 10 minutes ago Up 10 minutes 80/tcp, 172.16.1.15:40004->80/udp myNginx-test04
3c6eae128f34 nginx "/docker-entrypoint.…" 10 minutes ago Up 10 minutes 172.16.1.15:49153->80/tcp myNginx-test03
e90410d961d5 nginx "/docker-entrypoint.…" 11 minutes ago Up 11 minutes 172.16.1.15:20002->80/tcp myNginx-test02
4c123f46e38f nginx "/docker-entrypoint.…" 12 minutes ago Up 12 minutes 0.0.0.0:10001->80/tcp, :::10001->80/tcp myNginx-test01
root@node15:~# docker container port myNginx-test05
80/udp -> 172.16.1.15:50001
443/tcp -> 0.0.0.0:50002
443/tcp -> :::50002
53/udp -> 0.0.0.0:50003
53/udp -> :::50003
root@node15:~# docker container port myNginx-test04
80/udp -> 172.16.1.15:40004
root@node15:~# docker container port myNginx-test03
80/tcp -> 172.16.1.15:49153
root@node15:~# docker container port myNginx-test02
80/tcp -> 172.16.1.15:20002
root@node15:~# docker container port myNginx-test01
80/tcp -> 0.0.0.0:10001
80/tcp -> :::10001
root@node15:~#
5. 使用inspect命令获取容器的信息
5.1 获取容器的详细信息
### 查看容器名称为"myNginx-test05"的容器详细信息
root@node15:~# docker container ls -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
15acaf3a4cff 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-test05
root@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 <[email protected]>"
},
"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 -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
15acaf3a4cff 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-test05
root@node15:~# docker container inspect 15acaf3a4cff
5.2 获取容器的IP地址
root@node15:~# docker container ls -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
15acaf3a4cff 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-test05
root@node15:~# docker container inspect -f "{{.NetworkSettings.IPAddress}}" myNginx-test05
172.17.0.6
root@node15:~# docker container inspect -f "{{.NetworkSettings.IPAddress}}" 15acaf3a4cff
172.17.0.6
root@node15:~#
5.3 获取容器的PID信息
root@node15:~# docker container ls -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
15acaf3a4cff 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-test05
root@node15:~# docker container inspect -f "{{.State.Pid}}" 15acaf3a4cff
2855
root@node15:~# docker container inspect -f "{{.State.Pid}}" myNginx-test05
2855
root@node15:~#
6. 进入到正在运行的容器
6.1 使用attach命令连接正在运行的容器(生产环境不推荐使用,因为使用exit退出容器后容器会跟着自动关闭)
root@node15:~# docker container ls -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
39cb32823eb0 nginx "/docker-entrypoint.…" About a minute ago Up About a minute 80/tcp myNginx-test01
root@node15:~# docker container attach 39cb32823eb0
root@39cb32823eb0:/# ls /
bin dev docker-entrypoint.sh home lib64 mnt proc run srv tmp var
boot docker-entrypoint.d etc lib media opt root sbin sys usr
root@39cb32823eb0:/# date
Wed Mar 2 01:51:16 UTC 2022
root@39cb32823eb0:/# date +%Y-%m-%d-%H:%M:%S
2022-03-02-01:51:28
root@39cb32823eb0:/# exit
exit
root@node15:~# docker container ls -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
39cb32823eb0 nginx "/docker-entrypoint.…" 3 minutes ago Exited (0) About a minute ago myNginx-test01
root@node15:~#
6.2 使用exec命令(生产环境推荐使用这种方式)
root@node15:~# docker container ls -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
39cb32823eb0 nginx "/docker-entrypoint.…" 7 minutes ago Up 3 seconds 80/tcp myNginx-test01
root@node15:~# docker container exec -it myNginx-test01 bash
root@39cb32823eb0:/# cat /etc/hosts
127.0.0.1 localhost
::1 localhost ip6-localhost ip6-loopback
fe00::0 ip6-localnet
ff00::0 ip6-mcastprefix
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters
172.17.0.2 39cb32823eb0
root@39cb32823eb0:/# exit
exit
root@node15:~# docker container ls -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
39cb32823eb0 nginx "/docker-entrypoint.…" 8 minutes ago Up 49 seconds 80/tcp myNginx-test01
root@node15:~#
6.3 使用nsenter命令(生产环境也推荐使用这种方式,但需要单独安装nsenter命令)
root@node15:~# apt-get install util-linux #安装nsenter命令
root@node15:~# docker container ls -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
39cb32823eb0 nginx "/docker-entrypoint.…" 18 minutes ago Up 10 minutes 80/tcp myNginx-test01
root@node15:~# docker container inspect -f "{{.State.Pid}}" myNginx-test01 #获取容器PID
2559
root@node15:~# nsenter -t 2559 -m -u -i -n -p #通过容器的PID连接运行中的容器
root@39cb32823eb0:/# cat /etc/issue
Debian GNU/Linux 11 \n \l
root@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.114
root@39cb32823eb0:/# exit
logout
root@node15:~# docker container ls -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
39cb32823eb0 nginx "/docker-entrypoint.…" 19 minutes ago Up 12 minutes 80/tcp myNginx-test01
root@node15:~#
6.4 脚本方式连接容器
root@node15:/data/scripts# pwd
/data/scripts
root@node15:/data/scripts# cat docker_connect.sh
#!/bin/bash
function 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 $1
root@node15:/data/scripts#
root@node15:/data/scripts# ll docker_connect.sh
-rw-r--r-- 1 root root 582 Mar 2 10:19 docker_connect.sh
root@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 -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
39cb32823eb0 nginx "/docker-entrypoint.…" 33 minutes ago Up 25 minutes 80/tcp myNginx-test01
root@node15:/data/scripts# ll
total 12
drwxr-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-test01
root@39cb32823eb0:/# cat /etc/issue
Debian GNU/Linux 11 \n \l
root@39cb32823eb0:/# date +%F
2022-03-02
root@39cb32823eb0:/# exit
logout
root@node15:/data/scripts# docker container ls -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
39cb32823eb0 nginx "/docker-entrypoint.…" 34 minutes ago Up 26 minutes 80/tcp myNginx-test01
root@node15:/data/scripts#