docker笔记之容器管理
1.根据镜像创建容器、删除容器
#创建一个容器
[root@heibing ~]# docker container create --name my_nginx nginx:latest
1f54c86b3aabbdf11fc42ab67532aac2077bc53e367045d0b0c59585ec352f10
[root@heibing ~]# docker create --name my_nginx02 nginx:latest
ca67d9e6ede856890bb64ba3595b5cf913c0188fe6d057ca363f1748ea5b13d3
#删除一个容器
[root@heibing ~]# docker rm my_nginx
my_nginx
#查看运行的容器
[root@heibing ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
7c5e092fa50b nginx "nginx -g 'daemon of…" 10 seconds ago Up 9 seconds 0.0.0.0:80->80/tcp distracted_edison
#查看已经创建的容器
[root@heibing ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
7c5e092fa50b nginx "nginx -g 'daemon of…" 14 seconds ago Up 13 seconds 0.0.0.0:80->80/tcp distracted_edison
e66f507a489d nginx "nginx -g 'daemon of…" About a minute ago Exited (0) 21 seconds ago zen_liskov
备注:创建、删除、启动、停止等容器命令,默认不用写container,直接操作容器即可。
2.启动容器、停止容器
#查看当前容器运行状态
[root@heibing ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
#根据id启动一个容器,id不需要全部输入,只需要可以区别其他容器即可
[root@heibing ~]# docker start 7c5
7c5
[root@heibing ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
7c5e092fa50b nginx "nginx -g 'daemon of…" 3 minutes ago Up 1 second 0.0.0.0:80->80/tcp distracted_edison
#停止容器运行
[root@heibing ~]# docker stop 7c5
7c5
[root@heibing ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
备注:启动、停止一个容器,可以根据容器id或者容器的名字来进行相关操作
3.docker container命令参数解析
attach 将本地标准输入、输出和错误流附加到正在运行的容器中
commit 通过容器内容创建一个新的镜像
cp 在容器和本地系统之间复制文件
create 创建一个新的容器
diff 检查容器文件系统上文件或目录的更改
exec 在一个运行的容器上运行命令
export 将容器的文件系统导出为tar存档文件
inspect 显示一个或多个容器的详细信息
kill 杀掉一个或多个正在运行的容器
logs 获取容器的日志
ls 显示容器列表
pause 暂停一个或多个容器中的所有进程
unpause 恢复一个或多个容器暂停的所有进程
port 列出容器的端口映射或特定映射
prune 删除所有停止运行的容器
rename 为一个容器重命名
restart 重启一个或多个容器
rm 删除一个或多个容器
run 在一个新的容器中运行命令
start 启动一个或多个停止运行的容器
stats 显示容器资源使用统计数据的实时流
stop 关闭一个或多个运行的容器
top 显示容器正在运行的进程
update 更新一个或多个容器的配置
wait 阻塞,直到一个或多个容器停止,然后打印它们的退出代码
容器命令示例
#查看当前运行的容器
[root@heibing ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
da1cc40d9e75 nginx "nginx -g 'daemon of…" 4 days ago Up 4 days 0.0.0.0:80->80/tcp elegant_maxwell
#查看容器中运行的进程
[root@heibing ~]# docker top da
UID PID PPID C STIME TTY TIME CMD
root 2071 2055 0 03:19 ? 00:00:00 nginx: master process nginx -g daemon off;
101 2100 2071 0 03:19 ? 00:00:00 nginx: worker process
#查看容器的端口映射
[root@heibing ~]# docker port da
80/tcp -> 0.0.0.0:80
#为容器重命名
[root@heibing ~]# docker rename da my_nginx
[root@heibing ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
da1cc40d9e75 nginx "nginx -g 'daemon of…" 4 days ago Up 4 days 0.0.0.0:80->80/tcp my_nginx
#查看容器日志
[root@heibing ~]# docker logs my_nginx
172.17.0.1 - - [12/Sep/2019:01:50:29 +0000] "GET / HTTP/1.1" 200 612 "-" "curl/7.29.0" "-"
#查看容器的实时状态
[root@heibing ~]# docker stats my_nginx
CONTAINER ID NAME CPU % MEM USAGE / LIMIT MEM % NET I/O BLOCK I/O PIDS
da1cc40d9e75 my_nginx 0.00% 1.402MiB / 976.3MiB 0.14% 1.7kB / 1.23kB 1.81MB / 0B 2
CONTAINER ID NAME CPU % MEM USAGE / LIMIT MEM % NET I/O BLOCK I/O PIDS
da1cc40d9e75 my_nginx 0.00% 1.402MiB / 976.3MiB 0.14% 1.7kB / 1.23kB 1.81MB / 0B 2
CONTAINER ID NAME CPU % MEM USAGE / LIMIT MEM % NET I/O BLOCK I/O PIDS
da1cc40d9e75 my_nginx 0.00% 1.402MiB / 976.3MiB 0.14% 1.7kB / 1.23kB 1.81MB / 0B 2
CONTAINER ID NAME CPU % MEM USAGE / LIMIT MEM % NET I/O BLOCK I/O PIDS
da1cc40d9e75 my_nginx 0.00% 1.402MiB / 976.3MiB 0.14% 1.7kB / 1.23kB 1.81MB / 0B 2
CONTAINER ID NAME CPU % MEM USAGE / LIMIT MEM % NET I/O BLOCK I/O PIDS
da1cc40d9e75 my_nginx 0.00% 1.402MiB / 976.3MiB 0.14% 1.7kB / 1.23kB 1.81MB / 0B 2
CONTAINER ID NAME CPU % MEM USAGE / LIMIT MEM % NET I/O BLOCK I/O PIDS
da1cc40d9e75 my_nginx 0.00% 1.402MiB / 976.3MiB 0.14% 1.7kB / 1.23kB 1.81MB / 0B 2
CONTAINER ID NAME CPU % MEM USAGE / LIMIT MEM % NET I/O BLOCK I/O PIDS
da1cc40d9e75 my_nginx 0.00% 1.402MiB / 976.3MiB 0.14% 1.7kB / 1.23kB 1.81MB / 0B 2
CONTAINER ID NAME CPU % MEM USAGE / LIMIT MEM % NET I/O BLOCK I/O PIDS
da1cc40d9e75 my_nginx 0.00% 1.402MiB / 976.3MiB 0.14% 1.7kB / 1.23kB 1.81MB / 0B 2
#查看容器的详细信息
[root@heibing ~]# docker inspect my_nginx
[
{
"Id": "da1cc40d9e7557428234a55b7c8c2e7fd9e3bb4768b1dc907a46ed3ada368120",
"Created": "2019-09-11T16:56:12.911285703Z",
"Path": "nginx",
"Args": [
"-g",
"daemon off;"
],
"State": {
"Status": "running",
"Running": true,
"Paused": false,
"Restarting": false,
"OOMKilled": false,
"Dead": false,
"Pid": 2071,
"ExitCode": 0,
"Error": "",
"StartedAt": "2019-09-11T16:56:13.825761926Z",
"FinishedAt": "0001-01-01T00:00:00Z"
},
......
"SandboxKey": "/var/run/docker/netns/e05241fb2ebd",
"SecondaryIPAddresses": null,
"SecondaryIPv6Addresses": null,
"EndpointID": "e5de8e5e0d7fc676af6d99a558fc8f9fcc0efc4eb95e3dc519e8abc6dc647db1",
"Gateway": "172.17.0.1",
"GlobalIPv6Address": "",
"GlobalIPv6PrefixLen": 0,
"IPAddress": "172.17.0.2",
"IPPrefixLen": 16,
"IPv6Gateway": "",
"MacAddress": "02:42:ac:11:00:02",
"Networks": {
"bridge": {
"IPAMConfig": null,
"Links": null,
"Aliases": null,
"NetworkID": "3fb516fe6680bbd1e6c5d36e6de84eba967edcaf404ccfc96de4aea3d1041408",
"EndpointID": "e5de8e5e0d7fc676af6d99a558fc8f9fcc0efc4eb95e3dc519e8abc6dc647db1",
"Gateway": "172.17.0.1",
"IPAddress": "172.17.0.2",
"IPPrefixLen": 16,
"IPv6Gateway": "",
"GlobalIPv6Address": "",
"GlobalIPv6PrefixLen": 0,
"MacAddress": "02:42:ac:11:00:02",
"DriverOpts": null
}
}
}
}
]
[root@heibing ~]#
4.docker run命令参数解析
docker run == docker create + docker start,所以可以直接通过run命令直接创建并启动镜像
常用参数解析
--name 为容器指定一个名字
-p 80:80 为容器映射一个外网端口
-v /var/www/ /usr/share/nginx/html/ 为容器映射一个外部的文件,容器生成的数据将保存在外部宿主机上,防止数据丢失。
-c 指定分配的cpu权重,用于共享CPU环境
--cpuset="", 设置容器可以使用哪些CPU,此参数可以用来容器独占CPU
-m 为容器分配指定的最大可用内存
-u, --user="", 指定容器的用户
-w, --workdir="", 指定容器的工作目录
-e, --env=[], 指定环境变量,容器中可以使用该环境变量
--cidfile="", 运行容器后,在指定文件中写入容器PID值,一种典型的监控系统用法
-h, 指定容器的主机名
-v, 给容器挂载存储卷,挂载到容器的某个目录
--name , 指定容器名字,后续可以通过名字进行容器管理,links特性需要使用名字
--restart="no", 指定容器停止后的重启策略:no:容器退出时不重启/on-failure:容器故障退出(返回值非零)时重启/always:容器退出时总是重启(常驻容器需要配置此参数)
示例
#docker进行容器操作的方式
[root@heibing ~]#
[root@heibing ~]# docker exec -it my_nginx /bin/bash
root@da1cc40d9e75:/# ls
bin boot dev etc home lib lib64 media mnt opt proc root run sbin srv sys tmp usr var
root@da1cc40d9e75:/# /etc/init.d/nginx status
[ ok ] nginx is running.
root@da1cc40d9e75:/# exit
exit
[root@heibing ~]#
备注:要让一个容器一直保持在后台运行,其内部的第一个进程必须保持运行状态,比如nginx容器
[root@heibing ~]# docker top mynginx
UID PID PPID C STIME TTY TIME CMD
root 2071 2055 0 03:19 ? 00:00:00 nginx: master process nginx -g daemon off;
101 2100 2071 0 03:19 ? 00:00:00 nginx: worker process
[root@heibing ~]# docker exec -it my_nginx /bin/bash
Error: No such container: my_nginx
[root@heibing ~]# docker exec -it mynginx /bin/bash
root@da1cc40d9e75:/# /etc/init.d/nginx stop
[root@heibing ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
#当我关闭容器内的nginx进程后,容器自动退出关闭,在查看运行的容器,已处于无的状态