3.容器管理
3.1 查看正在运行的容器
命令:docker ps 可以通过docker ps -a查看所有运行的容器
示例:
[root@vms41 ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
3.2 创建容器
docker run --help 查看docker run的选项
命令: docker run --name=c1 tag
可选参数:
参数 | 说明 |
---|---|
-a stdin: | 指定标准输入输出内容类型,可选 STDIN/STDOUT/STDERR 三项; |
--restart=always | 容器内容进程挂了以后,立即重启 |
-d | 后台运行容器,并返回容器ID |
-t | 为容器重新分配一个伪输入终端,通常与 -i 同时使用 |
-i | 以交互模式运行容器,通常与 -t 同时使用 |
--name="nginx-lb": | 为容器指定一个名称; |
-p | 指定端口映射,格式为:主机(宿主)端口:容器端口 |
-P | 随机端口映射,容器内部端口随机映射到主机的端口 |
-h "mars" | 指定容器的hostname |
-e username="ritchie": | 设置环境变量 |
--env-file=[]: | 从指定文件读入环境变量; |
--cpuset="0-2" or --cpuset="0,1,2": | 绑定容器到指定CPU运行; |
--net="bridge": | 指定容器的网络连接类型,支持 bridge/host/none/container: 四种类型; |
--link=[]: | 添加链接到另一个容器; |
--expose=[]: | 开放一个端口或一组端口 |
-m : | 设置容器使用内存最大值; |
--volume | -v: 绑定一个卷 |
--dns 8.8.8.8 | 指定容器使用的DNS服务器,默认和宿主一致 |
--dns-search example.com | 指定容器DNS搜索域名,默认和宿主一致 |
生成一个容器,同时使用命令来规范管理
示例:
[root@vms41 ~]# docker images ##查看已经存在的镜像
REPOSITORY TAG IMAGE ID CREATED SIZE
nginx latest 62d49f9bab67 2 weeks ago 133MB
hub.c.163.com/library/wordpress latest dccaeccfba36 3 years ago 406MB
centos v1 328edcd84f1b 3 years ago 193MB
hub.c.163.com/library/centos latest 328edcd84f1b 3 years ago 193MB
hub.c.163.com/library/mysql latest 9e64176cd8a2 4 years ago 407MB
[root@vms41 ~]# docker run --name=c1 centos:v1 ##运行一个容器,并命名为c1,从而规范管理
[root@vms41 ~]# docker ps -a ##查看确认容器运行状态
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
##发现状态为exited,因为根据docker history centos:v1,发现centos:v1运行的程序为bash,bash瞬间执行完成后退出了
cc9415d1ad5c centos:v1 "/bin/bash" 18 seconds ago Exited (0) 8 seconds ago c1
[root@vms41 ~]# docker history centos:v1 --no-trunc
IMAGE CREATED CREATED BY S
IZE COMMENTsha256:328edcd84f1bbf868bc88e4ae37afe421ef19be71890f59b4b2d8ba48414b84d 3 years ago /bin/sh -c #(nop) CMD ["/bin/bash"] 0
B <missing> 3 years ago /bin/sh -c #(nop) LABEL name=CentOS Base Image vendor=CentOS license=GPLv2 build-date=20170801 0
B <missing> 3 years ago /bin/sh -c #(nop) ADD file:63492ba809361c51e75605d70390b549ff1187076b6d00485a1a0bb175daa40e in / 1
93MB
3.3 删除容器
命令:docker rm 容器名
示例:
[root@vms41 ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
cc9415d1ad5c centos:v1 "/bin/bash" 10 minutes ago Exited (0) 10 minutes ago c1
[root@vms41 ~]# docker rm c1
c1
[root@vms41 ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
[root@vms41 ~]# docker run -t --name=c1 centos:v1 使用-t指定在容器中开通一个终端
[root@a10f2f1184da /]# ##进入到c1容器中
无法删除运行的容器
##重开一个终端,尝试删除正在运行的容器c1
[root@vms41 ~]# docker rm c1
Error response from daemon: You cannot remove a running container a10f2f1184da73a81d6a8ce85587c36f5a693a8959e2438b7c8ed3da23a4d62f. Stop the container before attempting removal or force rem
ove
##报错删除不了正在运行的容器,需要首先停止
[root@vms41 ~]# docker rm -f c1 ##-f参数强制删除
c1
[root@vms41 ~]# docker ps -a ##查看确认已经删除
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
[root@vms41 ~]# docker run -it --name=c1 centos:v1 ##增加-i选项,发现已经可以操作
[root@8f5c8a3158f6 /]# ##可以在容器中执行命令
[root@vms41 ~]# docker ps -a ##另开一台终端查看,发现状态为运行
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
a10f2f1184da centos:v1 "/bin/bash" About a minute ago Up About a minute c1
[root@8f5c8a3158f6 /]# exit ##执行退出容器命令后
exit
[root@vms41 ~]# docker ps -a ##新开终端发现状态变为exited
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
8f5c8a3158f6 centos:v1 "/bin/bash" 28 seconds ago Exited (0) 4 seconds ago c1
[root@vms41 ~]# docker run -it --restart=always --name=c1 centos:v1 ##使用--restart=always参数,即使退出容器后,容器依然正常运行
[root@vms41 ~]# docker ps ##新开终端,发现状态正常
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
f260af0a2b8e centos:v1 "/bin/bash" 7 seconds ago Up 6 seconds c1
[root@f260af0a2b8e /]# exit
exit
[root@vms41 ~]# docker ps ##新开终端发现,在容器中退出后,容器状态依然为正常
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
f260af0a2b8e centos:v1 "/bin/bash" 15 seconds ago Up 3 seconds c1
3.4 进入正在运行的容器
命令:
docker attach 容器名称
示例:
[root@vms41 ~]# docker attach c1
[root@f260af0a2b8e /]#
[root@vms41 ~]# docker run -dit --name=c1 --restart=always centos:v1 ##可以通过docker run添加-d(detach),是的创建好容器以后并不直接进入容器
80f709b67927774534400843eaa374dff55716f0dae4382d5b91911ace5dc6ec
3.5 查看docker进程,修改容器运行进程
命令:docker top 容器名称
示例:
[root@vms41 ~]# docker top c1
UID PID PPID C STIME TTY TIME CMD
root 17722 17700 0 23:23 pts/0 00:00:00 /bin/bash
命令:
docker run -dit --name=c1 --restart=always centos:v1 sleep 20
docker run命令最后添加运行的命令
[root@vms41 ~]# docker run -dit --name=c1 --restart=always centos:v1 sleep 20
765345a5e639bba3c6237a9d30985b66f4ba6b1c21045320a8f0b9fd42447dd8
[root@vms41 ~]# docker top c1
UID PID PPID C STIME TTY TIME CMD
root 17876 17854 0 23:25 pts/0 00:00:00 sleep 20
3.6 给容器临时额外运行进程
命令:docker exec -it 容器名 进程名,相当于临时给容器带入一个进程
示例:
docker exec -it c1 bsh
[root@vms41 ~]# docker run -dit --name=c1 --restart=always centos:v1 sleep 200 ##创建容器c1,并运行sleep
6bb0614a809eb228c1ed69dc4a10b5cbf0e4b37c9ffeba28e9b0c3d6a3e4b0ab
[root@vms41 ~]# docker attach c1 ##进入容器c1,因为运行的是sleep,所以无bash进程,所以无法执行交互
[root@vms41 ~]# docker top c1 ##查看容器c1运行进程,发现只有sleep
UID PID PPID C STIME TTY TIME CMD
root 19086 19065 0 08:32 pts/0 00:00:00 sleep 200
[root@vms41 ~]# docker exec -it c1 bash ##给容器c1运行bash进程
[root@vms41 ~]# docker top c1 ##查看容器c1的进程,发现已经有bash和slleep
UID PID PPID C STIME TTY TIME CMD
root 19086 19065 0 08:32 pts/0 00:00:00 sleep 200
root 19166 19065 2 08:32 pts/1 00:00:00 bash
[root@6bb0614a809e /]# exit ##退出c1容器,再次查看发现已经无bash进程
[root@vms41 ~]# docker top c1
UID PID PPID C STIME TTY TIME CMD
root 19086 19065 0 08:32 pts/0 00:00:00 sleep 200
3.7 查看容器运行的日志
命令:docker logs 容器名
示例:
[root@vms41 ~]# docker logs db
error: database is uninitialized and password option is not specified
You need to specify one of MYSQL_ROOT_PASSWORD, MYSQL_ALLOW_EMPTY_PASSWORD and MYSQL_RANDOM_ROOT_PASSWORD
error: database is uninitialized and password option is not specified
You need to specify one of MYSQL_ROOT_PASSWORD, MYSQL_ALLOW_EMPTY_PASSWORD and MYSQL_RANDOM_ROOT_PASSWORD
error: database is uninitialized and password option is not specified
You need to specify one of MYSQL_ROOT_PASSWORD, MYSQL_ALLOW_EMPTY_PASSWORD and MYSQL_RANDOM_ROOT_PASSWORD
error: database is uninitialized and password option is not specified
You need to specify one of MYSQL_ROOT_PASSWORD, MYSQL_ALLOW_EMPTY_PASSWORD and MYSQL_RANDOM_ROOT_PASSWORD
error: database is uninitialized and password option is not specified
You need to specify one of MYSQL_ROOT_PASSWORD, MYSQL_ALLOW_EMPTY_PASSWORD and MYSQL_RANDOM_ROOT_PASSWORD
error: database is uninitialized and password option is not specified
You need to specify one of MYSQL_ROOT_PASSWORD, MYSQL_ALLOW_EMPTY_PASSWORD and MYSQL_RANDOM_ROOT_PASSWORD
error: database is uninitialized and password option is not specified
You need to specify one of MYSQL_ROOT_PASSWORD, MYSQL_ALLOW_EMPTY_PASSWORD and MYSQL_RANDOM_ROOT_PASSWORD
error: database is uninitialized and password option is not specified
You need to specify one of MYSQL_ROOT_PASSWORD, MYSQL_ALLOW_EMPTY_PASSWORD and MYSQL_RANDOM_ROOT_PASSWORD
error: database is uninitialized and password option is not specified
You need to specify one of MYSQL_ROOT_PASSWORD, MYSQL_ALLOW_EMPTY_PASSWORD and MYSQL_RANDOM_ROOT_PASSWORD
error: database is uninitialized and password option is not specified
You need to specify one of MYSQL_ROOT_PASSWORD, MYSQL_ALLOW_EMPTY_PASSWORD and MYSQL_RANDOM_ROOT_PASSWORD
error: database is uninitialized and password option is not specified
You need to specify one of MYSQL_ROOT_PASSWORD, MYSQL_ALLOW_EMPTY_PASSWORD and MYSQL_RANDOM_ROOT_PASSWORD
docker logs -f db
持续的查看容器日志
[root@vms41 ~]# docker logs -f db
error: database is uninitialized and password option is not specified
You need to specify one of MYSQL_ROOT_PASSWORD, MYSQL_ALLOW_EMPTY_PASSWORD and MYSQL_RANDOM_ROOT_PASSWORD
error: database is uninitialized and password option is not specified
You need to specify one of MYSQL_ROOT_PASSWORD, MYSQL_ALLOW_EMPTY_PASSWORD and MYSQL_RANDOM_ROOT_PASSWORD
error: database is uninitialized and password option is not specified
You need to specify one of MYSQL_ROOT_PASSWORD, MYSQL_ALLOW_EMPTY_PASSWORD and MYSQL_RANDOM_ROOT_PASSWORD
3.8 容器端口到物理机端口映射
命令:
docker run命令,使用-p参数指定端口映射
示例:
[root@vms41 ~]# docker run -dit --name db --restart=always -p 3307:3306 -e MYSQL_ROOT_PASSWORD=haha001 hub.c.163.com/library/mysql
##使用-p参数指定端口映射,冒号前面是物理机端口,冒号后面是虚机端口
c8e138490e6d491ef4482c883e9e8f0f37bf3dc953234a72a2aa0f20a86c9ec2
[root@vms41 ~]# docker ps ##查看运行的容器状态,发现容器mysql3306端口映射到3307端口
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
c8e138490e6d hub.c.163.com/library/mysql "docker-entrypoint.s…" 46 seconds ago Up 44 seconds 0.0.0.0:3307->3306/tcp, :::3307->3306/tcp db