[root@Tang ~]# 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
[root@Tang ~]# docker container run --help
[root@Tang ~]# docker container ls --help
... ...
[root@Tang ~]# 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
... ...
# -t # 打开一个伪终端
# -i # 交互式访问
# --name # 容器名字
# --network # 指定网络
# --rm # 容器一停,自动删除(做测试时常用此选项)
# -d # 剥离与当前终端的关系;否则会一直占据着终端
# 端口映射,将容器内服务的端口映射在宿主机的指定端口
## -p <container port>
## -p <hostport>:<container port>
## -p <hostip>:<hostport>:<container port>
[root@Tang ~]# docker search nginx # 搜索 Nginx 相关镜像
NAME DESCRIPTION STARS OFFICIAL AUTOMATED
nginx Official build of Nginx. 12257 [OK]
... ...
[root@Tang ~]# docker image pull nginx:latest # 下载相关镜像
latest: Pulling from library/nginx
000eee12ec04: Pull complete
eb22865337de: Pull complete
bee5d581ef8b: Pull complete
Digest: sha256:50cf965a6e08ec5784009d0fccb380fc479826b6e0e65684d9879170a9df8566
Status: Downloaded newer image for nginx:latest
docker.io/library/nginx:latest
[root@Tang ~]# docker image ls # 查看镜像列表,下载成功
REPOSITORY TAG IMAGE ID CREATED SIZE
myap v2 c0986260dda2 27 hours ago 347MB
myap v1 2a5bcc7a5916 27 hours ago 347MB
nginx latest 231d40e811cd 4 days ago 126MB
centos 7 5e35e350aded 2 weeks ago 203MB
busybox latest 020584afccce 3 weeks ago 1.22MB
### 创建并运行一个容器,名称为 myweb1 ,镜像是 nginx:latest ###
### 暴露容器端口 80 ,宿主机端口为 8000 ,剥离当前运行终端 ###
[root@Tang ~]# docker container run -p 8000:80 -d --name myweb1 nginx:latest
87d042f5e6d87f9ab74b6fb4535b85e3d3e72584ecadc081a1128f0e9a87e23d
[root@Tang ~]# docker container ps -a # 查看 docker container 的运行状态
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
87d042f5e6d8 nginx:latest "nginx -g 'daemon of…" 28 seconds ago Up 26 seconds 0.0.0.0:8000->80/tcp myweb1
[root@Tang ~]# docker container port myweb1 # 查询 docker 端口映射
80/tcp -> 0.0.0.0:8000
### 宿主机地址查看 ###
[root@Tang ~]# ipinfo
enp1s0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 172.16.141.252 netmask 255.255.255.0 broadcast 172.16.141.255
### 宿主机端口映射查看 ###
[root@Tang ~]# iptables -t nat -nvL
Chain PREROUTING (policy ACCEPT 1317 packets, 91798 bytes)
pkts bytes target prot opt in out source destination
309 29887 DOCKER all -- * * 0.0.0.0/0 0.0.0.0/0 ADDRTYPE match dst-type LOCAL
Chain INPUT (policy ACCEPT 208 packets, 22398 bytes)
pkts bytes target prot opt in out source destination
Chain OUTPUT (policy ACCEPT 2 packets, 120 bytes)
pkts bytes target prot opt in out source destination
0 0 DOCKER all -- * * 0.0.0.0/0 !127.0.0.0/8 ADDRTYPE match dst-type LOCAL
Chain POSTROUTING (policy ACCEPT 3 packets, 180 bytes)
pkts bytes target prot opt in out source destination
218 13733 MASQUERADE all -- * !docker0 172.17.0.0/16 0.0.0.0/0
308 20521 MASQUERADE all -- * enp1s0 0.0.0.0/0 0.0.0.0/0
0 0 MASQUERADE all -- * tang 0.0.0.0/0 0.0.0.0/0
0 0 MASQUERADE tcp -- * * 172.17.0.2 172.17.0.2 tcp dpt:80
Chain DOCKER (2 references)
pkts bytes target prot opt in out source destination
4 336 RETURN all -- docker0 * 0.0.0.0/0 0.0.0.0/0
1 60 DNAT tcp -- !docker0 * 0.0.0.0/0 0.0.0.0/0 tcp dpt:8000 to:172.17.0.2:80
### 查看容器的地址 ###
[root@Tang ~]# docker container inspect myweb1 | grep "IPAddress"
"SecondaryIPAddresses": null,
"IPAddress": "172.17.0.2",
"IPAddress": "172.17.0.2",
### 宿主机进行容器 myweb1 的 web 访问 ###
[root@Tang ~]# curl http://172.17.0.2
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
body {
width: 35em;
margin: 0 auto;
font-family: Tahoma, Verdana, Arial, sans-serif;
}
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>
<p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p>
<p><em>Thank you for using nginx.</em></p>
</body>
</html>
### 其它主机进行对该容易进行访问,能够访问成功 ###
[root@Tang-1 ~]# ipinfo
enp1s0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 172.16.141.209 netmask 255.255.255.0 broadcast 172.16.141.255
[root@Tang-1 ~]# curl http://172.16.141.252:8000
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
body {
width: 35em;
margin: 0 auto;
font-family: Tahoma, Verdana, Arial, sans-serif;
}
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>
<p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p>
<p><em>Thank you for using nginx.</em></p>
</body>
</html>
[root@Tang ~]# docker container exec -it myweb1 /bin/bash
root@87d042f5e6d8:/# cat /etc/nginx/conf.d/default.conf
server {
listen 80;
server_name localhost;
location / {
root /usr/share/nginx/html;
index index.html index.htm;
}
}
[root@Tang ~]# 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 numeric IDs
-s, --size Display total file sizes
[root@Tang ~]# docker container ps -a # 显示所有容器
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
03bd73cb4dc8 nginx:latest "nginx -g 'daemon of…" 25 seconds ago Exited (0) 10 seconds ago myweb3
8659f2ee9dcf nginx:latest "nginx -g 'daemon of…" 33 seconds ago Up 31 seconds 80/tcp myweb2
87d042f5e6d8 nginx:latest "nginx -g 'daemon of…" 34 minutes ago Up 34 minutes 0.0.0.0:8000->80/tcp myweb1
[root@Tang ~]# docker container ps # 只显示正在运行的容器
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
8659f2ee9dcf nginx:latest "nginx -g 'daemon of…" 40 seconds ago Up 38 seconds 80/tcp myweb2
87d042f5e6d8 nginx:latest "nginx -g 'daemon of…" 34 minutes ago Up 34 minutes 0.0.0.0:8000->80/tcp myweb1
[root@Tang ~]# docker container kill --help
Usage: docker container kill [OPTIONS] CONTAINER [CONTAINER...]
Kill one or more running containers
Options:
-s, --signal string Signal to send to the container (default "KILL")
[root@Tang ~]# docker container stop --help
Usage: docker container stop [OPTIONS] CONTAINER [CONTAINER...]
Stop one or more running containers
Options:
-t, --time int Seconds to wait for stop before killing it (default 10)
[root@Tang ~]# docker container ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
8659f2ee9dcf nginx:latest "nginx -g 'daemon of…" 4 minutes ago Up 4 minutes 80/tcp myweb2
87d042f5e6d8 nginx:latest "nginx -g 'daemon of…" 38 minutes ago Up 38 minutes 0.0.0.0:8000->80/tcp myweb1
[root@Tang ~]# docker container kill myweb2
myweb2
[root@Tang ~]# docker container ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
87d042f5e6d8 nginx:latest "nginx -g 'daemon of…" 38 minutes ago Up 38 minutes 0.0.0.0:8000->80/tcp myweb1
[root@Tang ~]# docker container ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
03bd73cb4dc8 nginx:latest "nginx -g 'daemon of…" 5 minutes ago Exited (0) 4 minutes ago myweb3
8659f2ee9dcf nginx:latest "nginx -g 'daemon of…" 5 minutes ago Exited (137) 6 seconds ago myweb2
87d042f5e6d8 nginx:latest "nginx -g 'daemon of…" 38 minutes ago Up 38 minutes 0.0.0.0:8000->80/tcp myweb1
[root@Tang ~]# docker container start --help
Usage: docker container start [OPTIONS] CONTAINER [CONTAINER...]
Start one or more stopped containers
Options:
-a, --attach Attach STDOUT/STDERR and forward signals
--detach-keys string Override the key sequence for detaching a container
-i, --interactive Attach container's STDIN
# -a # 附加到当前终端
# -i # 交互式
### 查看所有容器状态 ###
[root@Tang ~]# docker container ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
03bd73cb4dc8 nginx:latest "nginx -g 'daemon of…" 5 minutes ago Exited (0) 4 minutes ago myweb3
8659f2ee9dcf nginx:latest "nginx -g 'daemon of…" 5 minutes ago Exited (137) 6 seconds ago myweb2
87d042f5e6d8 nginx:latest "nginx -g 'daemon of…" 38 minutes ago Up 38 minutes 0.0.0.0:8000->80/tcp myweb1
### 启动容器 myweb2 ###
[root@Tang ~]# docker container start myweb2
myweb2
### 容器已处于运行状态 ###
[root@Tang ~]# docker container ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
8659f2ee9dcf nginx:latest "nginx -g 'daemon of…" 11 minutes ago Up 15 seconds 80/tcp myweb2
87d042f5e6d8 nginx:latest "nginx -g 'daemon of…" 45 minutes ago Up 45 minutes 0.0.0.0:8000->80/tcp myweb1
### 对容器 myweb2 执行命令 /bin/bash 且进入交互式界面 ###
[root@Tang ~]# docker container exec -it myweb2 /bin/bash
root@8659f2ee9dcf:/# ls
bin boot dev etc home lib lib64 media mnt opt proc root run sbin srv sys tmp usr var
root@8659f2ee9dcf:/# exit
exit
docker container inspect 使用详解
[root@Tang ~]# docker container inspect --help
Usage: docker container inspect [OPTIONS] CONTAINER [CONTAINER...]
Display detailed information on one or more containers
Options:
-f, --format string Format the output using the given Go template
-s, --size Display total file sizes
### 查看容器 myweb1 的详细信息 ###
[root@Tang ~]# docker container inspect myweb1
[
{
"Id": "87d042f5e6d87f9ab74b6fb4535b85e3d3e72584ecadc081a1128f0e9a87e23d",
"Created": "2019-11-27T05:41:26.069799346Z",
"Path": "nginx",
"Args": [
"-g",
"daemon off;"
],
"State": {
"Status": "running",
"Running": true,
"Paused": false,
"Restarting": false,
"OOMKilled": false,
"Dead": false,
"Pid": 19055,
"ExitCode": 0,
"Error": "",
"StartedAt": "2019-11-27T05:41:27.911427268Z",
"FinishedAt": "0001-01-01T00:00:00Z"
},
[root@Tang ~]# 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 the volumes associated with the container
### 查看所有容器 ###
[root@Tang ~]# docker container ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
03bd73cb4dc8 nginx:latest "nginx -g 'daemon of…" 30 minutes ago Exited (0) 30 minutes ago myweb3
8659f2ee9dcf nginx:latest "nginx -g 'daemon of…" 30 minutes ago Up 19 minutes 80/tcp myweb2
87d042f5e6d8 nginx:latest "nginx -g 'daemon of…" About an hour ago Up About an hour 0.0.0.0:8000->80/tcp myweb1
### 删除容器 myweb3 ###
[root@Tang ~]# docker container rm myweb3
myweb3
[root@Tang ~]# docker container ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
8659f2ee9dcf nginx:latest "nginx -g 'daemon of…" 30 minutes ago Up 19 minutes 80/tcp myweb2
87d042f5e6d8 nginx:latest "nginx -g 'daemon of…" About an hour ago Up About an hour 0.0.0.0:8000->80/tcp myweb1
### 删除容器 myweb2 ,因正在运行无法删除 ###
[root@Tang ~]# docker container rm myweb2
Error response from daemon: You cannot remove a running container 8659f2ee9dcf0b270067b1c644851d2574fd36910306c6031c75471acd1a5318. Stop the container before attempting removal or force remove
### 使用 -f 可删除正在运行的容器 ###
[root@Tang ~]# docker container rm -f myweb2
myweb2
[root@Tang ~]# docker container ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
87d042f5e6d8 nginx:latest "nginx -g 'daemon of…" About an hour ago Up About an hour 0.0.0.0:8000->80/tcp myweb1
[root@Tang ~]# docker container rm -f `docker container ps -a -q`
afb9f9ee6cbe
61de46a6e5d6
87d042f5e6d8
[root@Tang ~]# docker container exec --help
Usage: docker container exec [OPTIONS] CONTAINER COMMAND [ARG...]
Run a command in a running container
Options:
-d, --detach Detached mode: run command in the background
--detach-keys string Override the key sequence for detaching a container
-e, --env list Set environment variables
-i, --interactive Keep STDIN open even if not attached
--privileged Give extended privileges to the command
-t, --tty Allocate a pseudo-TTY
-u, --user string Username or UID (format: <name|uid>[:<group|gid>])
-w, --workdir string Working directory inside the container
# -d # 在后台运行命令
# -e # 设置环境变量
# -i # 交互式
# -t # 打开一个终端
# -u # 用户名或UID
[root@Tang ~]# docker container exec -it myweb1 /bin/bash
root@87d042f5e6d8:/# ls
bin boot dev etc home lib lib64 media mnt opt proc root run sbin srv sys tmp usr var
root@87d042f5e6d8:/# exit
exit
[root@Tang ~]# docker container exec mycentos ping 172.17.0.1
PING 172.17.0.1 (172.17.0.1) 56(84) bytes of data.
64 bytes from 172.17.0.1: icmp_seq=1 ttl=64 time=0.105 ms
64 bytes from 172.17.0.1: icmp_seq=2 ttl=64 time=0.058 ms
[root@Tang ~]# docker container logs --help
Usage: docker container logs [OPTIONS] CONTAINER
Fetch the logs of a container
Options:
--details Show extra details provided to logs
-f, --follow Follow log output
--since string Show logs since timestamp (e.g. 2013-01-02T13:23:37) or relative (e.g. 42m for 42 minutes)
--tail string Number of lines to show from the end of the logs (default "all")
-t, --timestamps Show timestamps
--until string Show logs before a timestamp (e.g. 2013-01-02T13:23:37) or relative (e.g. 42m for 42 minutes)
[root@Tang ~]# docker container logs myweb1
172.17.0.1 - - [27/Nov/2019:05:51:26 +0000] "GET / HTTP/1.1" 200 612 "-" "curl/7.29.0" "-"
172.17.0.1 - - [27/Nov/2019:05:58:42 +0000] "GET / HTTP/1.1" 200 612 "-" "curl/7.29.0" "-"
172.16.141.209 - - [27/Nov/2019:06:00:22 +0000] "GET / HTTP/1.1" 200 612 "-" "curl/7.29.0" "-"
[root@Tang ~]# docker container stats --help
Usage: docker container stats [OPTIONS] [CONTAINER...]
Display a live stream of container(s) resource usage statistics
Options:
-a, --all Show all containers (default shows just running)
--format string Pretty-print images using a Go template
--no-stream Disable streaming stats and only pull the first result
--no-trunc Do not truncate output
[root@Tang ~]# docker container stats myweb1
CONTAINER ID NAME CPU % MEM USAGE / LIMIT MEM % NET I/O BLOCK I/O PIDS
87d042f5e6d8 myweb1 0.00% 1.406MiB / 7.556GiB 0.02% 2.59kB / 3.82kB 0B / 0B 2
### ctrl+p 然后在 ctrl+q 可使容易剥离当前终端 ###
[root@Tang ~]# docker container run --name mycentos1 -it myap:v2 /bin/bash
[root@afb9f9ee6cbe /]# ifconfig | grep inet -A 1
inet 172.17.0.4 netmask 255.255.0.0 broadcast 172.17.255.255
ether 02:42:ac:11:00:04 txqueuelen 0 (Ethernet)
--
inet 127.0.0.1 netmask 255.0.0.0
loop txqueuelen 1000 (Local Loopback)
[root@afb9f9ee6cbe /]# [root@Tang ~]#
### 容器剥离当前终端 ###
[root@Tang ~]#
[root@Tang ~]# docker container run --name mycentos -it myap:v2 /bin/bash
[root@57bd8a923604 /]# ps -aux
USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND
root 1 1.0 0.0 11828 1880 pts/0 Ss 07:16 0:00 /bin/bash
root 15 0.0 0.0 51752 1708 pts/0 R+ 07:16 0:00 ps -aux
[root@Tang ~]# w
15:17:31 up 16 days, 1:58, 2 users, load average: 0.04, 0.04, 0.05
USER TTY FROM LOGIN@ IDLE JCPU PCPU WHAT
root pts/0 172.16.141.198 13:12 59.00s 1.16s 0.03s /bin/bash
root pts/1 172.16.141.198 13:50 3.00s 0.12s 0.02s w
[root@Tang ~]# docker container ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
57bd8a923604 myap:v2 "/bin/bash" About a minute ago Up About a minute mycentos
[root@Tang ~]# docker container run --name mycentos -it myap:v2 /bin/bash
[root@57bd8a923604 /]# ps -aux
USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND
root 1 1.0 0.0 11828 1880 pts/0 Ss 07:16 0:00 /bin/bash
root 15 0.0 0.0 51752 1708 pts/0 R+ 07:16 0:00 ps -aux
[root@57bd8a923604 /]# exit
exit
[root@Tang ~]# docker container ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
[root@Tang ~]# docker container ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
57bd8a923604 myap:v2 "/bin/bash" 4 minutes ago Exited (130) 55 seconds ago mycentos