第三章:Docker容器管理


第三章:Docker容器管理
1.创建容器常用选项
  
[root@linux-node1 ~]# docker container --help
Usage: docker container COMMAND
Manage containers
Options:
Commands:
attach Attach local standard input, output, and error streams to a running container
commit Create a new image from a container's changes
cp Copy files/folders between a container and the local filesystem
create Create a new container
diff Inspect changes to files or directories on a container's filesystem
exec Run a command in a running container
export Export a container's filesystem as a tar archive
inspect Display detailed information on one or more containers
kill Kill one or more running containers
logs Fetch the logs of a container
ls List containers
pause Pause all processes within one or more containers
port List port mappings or a specific mapping for the container
prune Remove all stopped containers
rename Rename a container
restart Restart one or more containers
rm Remove one or more containers
run Run a command in a new container
start Start one or more stopped containers
stats Display a live stream of container(s) resource usage statistics
stop Stop one or more running containers
top Display the running processes of a container
unpause Unpause all processes within one or more containers
update Update configuration of one or more containers
wait Block until one or more containers stop, then print their exit codes

Run 'docker container COMMAND --help' for more information on a command.
创建并运行一个容器:
[root@linux-node1 ~]# docker container run -itd --name mynginx1 nginx:v1
ec769b13e4627eedf1900ea12e3bd0a13aa7717cca9b22b0ba9f86b929f05ece
[root@linux-node1 ~]# docker image ls
REPOSITORY TAG IMAGE ID CREATED SIZE
nginx latest b175e7467d66 12 days ago 109MB
nginx 1.11 5766334bdaa0 12 months ago 183MB
nginx v1 5766334bdaa0 12 months ago 183MB
[root@linux-node1 ~]# docker container ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
ec769b13e462 nginx:v1 "nginx -g 'daemon of…" 57 seconds ago Up 56 seconds 80/tcp, 443/tcp mynginx1
52a7ae4c6506 nginx:v1 "nginx -g 'daemon of…" 17 minutes ago Up 17 minutes 80/tcp, 443/tcp mynginx
说明:也可以简写啦!

启动该容器:
[root@linux-node1 ~]# docker container start mynginx1
mynginx1
[root@linux-node1 ~]# docker container ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
ec769b13e462 nginx:v1 "nginx -g 'daemon of…" 6 minutes ago Up 2 seconds 80/tcp, 443/tcp mynginx1
52a7ae4c6506 nginx:v1 "nginx -g 'daemon of…" 23 minutes ago Up 23 minutes 80/tcp, 443/tcp mynginx

[root@linux-node1 ~]# docker container attach mynginx1

-e参数设置环境变量,传递变量:
[root@linux-node1 ~]# docker container run -itd -e a=123 --name mynginx2 nginx:v1
0afaa0391d287db649adc019cdff5c862c9502e6234f05e929c7c1cfcd78a32b
[root@linux-node1 ~]# docker exec -it mynginx2 /bin/bash
root@0afaa0391d28:/# ls
bin dev home lib64 mnt proc run srv tmp var
boot etc lib media opt root sbin sys usr
root@0afaa0391d28:/# echo $a
123

-p参数指定端口:
[root@linux-node1 ~]# docker container run -itd -p 8080:80 --name mynginx3 nginx:v1
3f09c0c224c47329edff0b2fcc4ca302f065ed66d7abcf2c0307058bd36fc1c8
[root@linux-node1 ~]# docker container ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
3f09c0c224c4 nginx:v1 "nginx -g 'daemon of…" 21 seconds ago Up 19 seconds 443/tcp, 0.0.0.0:8080->80/tcp mynginx3
0afaa0391d28 nginx:v1 "nginx -g 'daemon of…" 4 minutes ago Up 4 minutes 80/tcp, 443/tcp mynginx2
ec769b13e462 nginx:v1 "nginx -g 'daemon of…" 2 hours ago Up 6 minutes 80/tcp, 443/tcp mynginx1
52a7ae4c6506 nginx:v1 "nginx -g 'daemon of…" 2 hours ago Up 2 hours 80/tcp, 443/tcp mynginx
输入:http://10.0.0.80:8080/
有页面显示成功!
Welcome to nginx!
[root@linux-node1 ~]# docker logs mynginx3
10.0.0.253 - - [23/Apr/2018:15:20:09 +0000] "GET / HTTP/1.1" 200 612 "-" "Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; WOW64; Trident/5.0)" "-"

[root@linux-node1 ~]# cat /var/lib/docker/containers/3f09c0c224c47329edff0b2fcc4ca302f065ed66d7abcf2c0307058bd36fc1c8/3f09c0c224c47329edff0b2fcc4ca302f065ed66d7abcf2c0307058bd36fc1c8-json.log
{"log":"10.0.0.253 - - [23/Apr/2018:15:20:09 +0000] \"GET / HTTP/1.1\" 200 612 \"-\" \"Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; WOW64; Trident/5.0)\" \"-\"\r\n","stream":"stdout","time":"2018-04-23T15:20:09.290283879Z"}

--restart=always参数表示进程服务挂了导容器退出则重启容器:
[root@linux-node1 ~]# docker container run -itd -p 8081:80 --name mynginx4 --restart=always nginx:v1
aa6eca60fec7d53646f03d95bcfcf07cd7209d8bc7e29b4ce7097265792b8ee9
[root@linux-node1 ~]# docker container ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
aa6eca60fec7 nginx:v1 "nginx -g 'daemon of…" 6 seconds ago Up 5 seconds 443/tcp, 0.0.0.0:8081->80/tcp mynginx4
3f09c0c224c4 nginx:v1 "nginx -g 'daemon of…" 10 minutes ago Up 10 minutes 443/tcp, 0.0.0.0:8080->80/tcp mynginx3
0afaa0391d28 nginx:v1 "nginx -g 'daemon of…" 14 minutes ago Up 14 minutes 80/tcp, 443/tcp mynginx2
ec769b13e462 nginx:v1 "nginx -g 'daemon of…" 2 hours ago Up 15 minutes 80/tcp, 443/tcp mynginx1
52a7ae4c6506 nginx:v1 "nginx -g 'daemon of…" 2 hours ago Up 2 hours 80/tcp, 443/tcp mynginx
输入:http://10.0.0.80:8081/
有页面显示成功!
Welcome to nginx!

docker容器资源使用限制:
[root@linux-node1 ~]# docker container run -itd --cpus 1 --memory 512m --name mynginx5 nginx:v1
4dea3165dc92c1038d9bae2689195ffc8dcd755e0cfed45028a643dbd43a99cd
[root@linux-node1 ~]# docker container ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
4dea3165dc92 nginx:v1 "nginx -g 'daemon of…" 7 seconds ago Up 6 seconds 80/tcp, 443/tcp mynginx5
aa6eca60fec7 nginx:v1 "nginx -g 'daemon of…" 10 minutes ago Up 10 minutes 443/tcp, 0.0.0.0:8081->80/tcp mynginx4
3f09c0c224c4 nginx:v1 "nginx -g 'daemon of…" 20 minutes ago Up 20 minutes 443/tcp, 0.0.0.0:8080->80/tcp mynginx3
0afaa0391d28 nginx:v1 "nginx -g 'daemon of…" 24 minutes ago Up 24 minutes 80/tcp, 443/tcp mynginx2
ec769b13e462 nginx:v1 "nginx -g 'daemon of…" 2 hours ago Up 26 minutes 80/tcp, 443/tcp mynginx1
52a7ae4c6506 nginx:v1 "nginx -g 'daemon of…" 2 hours ago Up 2 hours 80/tcp, 443/tcp mynginx
[root@linux-node1 ~]# docker stats mynginx5

CONTAINER ID NAME CPU % MEM USAGE / LIMIT MEM % NET I/O BLOCK I/O PIDS
4dea3165dc92 mynginx5 0.00% 2.676MiB / 512MiB 0.52% 648B / 0B 0B / 0B 2


2.管理容器常用命令
  
新版本将不同的管理工作交给不同的管理指令。

查看当前运行的容器:
[root@linux-node1 ~]# docker container ls
[root@linux-node1 ~]# docker container ps
[root@linux-node1 ~]# docker ps

查看容器的详细信息:
[root@linux-node1 ~]# docker container inspect mynginx5

不退出形式进入容器:
[root@linux-node1 ~]# docker container attach mynginx5 //阻塞。
[root@linux-node1 ~]# docker top mynginx5
UID PID PPID C STIME TTY TIME CMD
root 4679 4667 0 23:51 pts/0 00:00:00 nginx: master process nginx -g daemon off;
104 4710 4679 0 23:51 pts/0 00:00:00 nginx: worker process
[root@linux-node1 ~]# docker exec -it mynginx5 bash //进入。
root@4dea3165dc92:/# ls
bin dev home lib64 mnt proc run srv tmp var
boot etc lib media opt root sbin sys usr
root@4dea3165dc92:/# exit
exit

将一个容器提交给一个镜像:
[root@linux-node1 ~]# docker container commit mynginx5 nginx:v2
sha256:41238fb31cda1616acdd03b10a2dde24f6356f2901598a85830f7e595448dd04
[root@linux-node1 ~]# docker image ls
REPOSITORY TAG IMAGE ID CREATED SIZE
nginx v2 41238fb31cda 18 seconds ago 183MB
nginx latest b175e7467d66 12 days ago 109MB
nginx 1.11 5766334bdaa0 12 months ago 183MB
nginx v1 5766334bdaa0 12 months ago 183MB
说明:持久化到了这个镜像中。不建议,应该用Dockerfile来组织。

将宿主机本地文件复制到容器中:
[root@linux-node1 ~]# docker container exec mynginx5 ls /root
[root@linux-node1 ~]# docker container cp docker_in.sh mynginx5:/root
[root@linux-node1 ~]# docker container exec mynginx5 ls /root
docker_in.sh
重启后还在:
[root@linux-node1 ~]# docker container restart mynginx5
mynginx5
[root@linux-node1 ~]# docker container exec mynginx5 ls /root
docker_in.sh

查看容器日志:
[root@linux-node1 ~]# docker container logs mynginx5

查看容器端口映射:
[root@linux-node1 ~]# docker container port mynginx2
[root@linux-node1 ~]# docker port mynginx2
[root@linux-node1 ~]# docker container port mynginx5
[root@linux-node1 ~]# docker container port mynginx3
80/tcp -> 0.0.0.0:8080
[root@linux-node1 ~]# docker container port mynginx4
80/tcp -> 0.0.0.0:8081

查看容器实时资源利用率等情况:
[root@linux-node1 ~]# docker container stats mynginx5

CONTAINER ID NAME CPU % MEM USAGE / LIMIT MEM % NET I/O BLOCK I/O PIDS
4dea3165dc92 mynginx5 0.00% 2.586MiB / 512MiB 0.51% 648B / 0B 0B / 0B 2

在线时更改CPU、内存等:
[root@linux-node1 ~]# docker update mynginx5
you must provide one or more flags when using this command

 

posted @ 2018-04-24 00:26  bkycrmn  阅读(1076)  评论(0)    收藏  举报