[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 newimagefrom a container's changes
cp Copy files/folders between a container and the local filesystem
create Create a newcontainer
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 newcontainer
start Start one or more stopped containers
stats Display a live stream ofcontainer(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
1.2 子命令帮助
[root@Tang ~]# docker container run --help
[root@Tang ~]# docker container ls --help
......
2、Docker Container Run 启动容器
2.1 Command Help & Usage
[root@Tang ~]# docker container run --help
Usage: docker container run [OPTIONS]IMAGE[COMMAND][ARG...]
Run a command in a newcontainer
Options:--add-host list Add a custom host-to-IPmapping(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(default0)--blkio-weight-device list Block IOweight(relative device weight)(default[])--cap-add list Add Linux capabilities
......
### 宿主机地址查看 ###
[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 ACCEPT1317 packets,91798 bytes)
pkts bytes target prot opt in out source destination
30929887DOCKER all --**0.0.0.0/00.0.0.0/0ADDRTYPE match dst-type LOCAL
Chain INPUT(policy ACCEPT208 packets,22398 bytes)
pkts bytes target prot opt in out source destination
Chain OUTPUT(policy ACCEPT2 packets,120 bytes)
pkts bytes target prot opt in out source destination
00DOCKER all --**0.0.0.0/0!127.0.0.0/8ADDRTYPE match dst-type LOCAL
Chain POSTROUTING(policy ACCEPT3 packets,180 bytes)
pkts bytes target prot opt in out source destination
21813733MASQUERADE all --*!docker0 172.17.0.0/160.0.0.0/030820521MASQUERADE all --* enp1s0 0.0.0.0/00.0.0.0/000MASQUERADE all --* tang 0.0.0.0/00.0.0.0/000MASQUERADE tcp --**172.17.0.2172.17.0.2 tcp dpt:80
Chain DOCKER(2 references)
pkts bytes target prot opt in out source destination
4336RETURN all -- docker0 *0.0.0.0/00.0.0.0/0160DNAT tcp --!docker0 *0.0.0.0/00.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>
2.3.3 其它主机访问该容器
### 其它主机进行对该容易进行访问,能够访问成功 ###
[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 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
3.2 Example
[root@Tang ~]# docker container ps -a # 显示所有容器
CONTAINERIDIMAGECOMMANDCREATEDSTATUSPORTSNAMES03bd73cb4dc8 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 # 只显示正在运行的容器
CONTAINERIDIMAGECOMMANDCREATEDSTATUSPORTSNAMES8659f2ee9dcf 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
4、Docker Container Kill / Stop 停止正在运行的容器
4.1 Command Help & Usage
[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(default10)
4.2 Example
[root@Tang ~]# docker container ps
CONTAINERIDIMAGECOMMANDCREATEDSTATUSPORTSNAMES8659f2ee9dcf 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
CONTAINERIDIMAGECOMMANDCREATEDSTATUSPORTSNAMES87d042f5e6d8 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
CONTAINERIDIMAGECOMMANDCREATEDSTATUSPORTSNAMES03bd73cb4dc8 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
5、Docker Container Start 激活处于关闭状态的容器
5.1 Command Help & Usage
[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 # 交互式
5.2 Example
### 查看所有容器状态 ###
[root@Tang ~]# docker container ps -a
CONTAINERIDIMAGECOMMANDCREATEDSTATUSPORTSNAMES03bd73cb4dc8 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
CONTAINERIDIMAGECOMMANDCREATEDSTATUSPORTSNAMES8659f2ee9dcf 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
[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
[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
7.2 Example
### 查看所有容器 ###
[root@Tang ~]# docker container ps -a
CONTAINERIDIMAGECOMMANDCREATEDSTATUSPORTSNAMES03bd73cb4dc8 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
CONTAINERIDIMAGECOMMANDCREATEDSTATUSPORTSNAMES8659f2ee9dcf 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
CONTAINERIDIMAGECOMMANDCREATEDSTATUSPORTSNAMES87d042f5e6d8 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 exec --help
Usage: docker container exec [OPTIONS]CONTAINERCOMMAND[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
[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.1PING172.17.0.1(172.17.0.1)56(84) bytes of data.64 bytes from172.17.0.1: icmp_seq=1 ttl=64 time=0.105 ms
64 bytes from172.17.0.1: icmp_seq=2 ttl=64 time=0.058 ms
9、Docker Container Logs 查询容器的运行日志
9.1 Command Help & Usage
[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 for42 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 for42 minutes)
[root@Tang ~]# docker container stats --help
Usage: docker container stats [OPTIONS][CONTAINER...]
Display a live stream ofcontainer(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 ~]# w
15:17:31 up 16 days,1:58,2 users, load average:0.04,0.04,0.05USERTTYFROMLOGIN@ IDLEJCPUPCPUWHAT
root pts/0172.16.141.19813:1259.00s 1.16s 0.03s /bin/bash
root pts/1172.16.141.19813:503.00s 0.12s 0.02s w
[root@Tang ~]# docker container ps
CONTAINERIDIMAGECOMMANDCREATEDSTATUSPORTSNAMES57bd8a923604 myap:v2 "/bin/bash" About a minute ago Up About a minute mycentos