Docker体系结构
容器管理
运行容器
交互式容器
应用场景:工具类: 开发,测试,临时性的任务
命令:docker container run IMAGE ID
-it 以交互式运行;–name 命名
运行之后会登陆进去
[root@localhost ~]# docker image ls
REPOSITORY TAG IMAGE ID CREATED SIZE
nginx latest daee903b4e43 7 days ago 133MB
centos 6.9 2199b8eb8390 20 months ago 195MB
centos 7.5.1804 cf49811e3cdb 20 months ago 200MB
以centos6.9镜像开启容器,登陆之后查看版本号
[root@localhost ~]# docker container run -it --name="centos6_server1" 2199b8eb8390
[root@01355db802ad /]# cat /etc/redhat-release
CentOS release 6.9 (Final)
守护式容器
应用场景:长期运行,适合运行应用程序和服务服务
-d 以守护进程运行
例:跑一个nginx的守护式容器
[root@localhost ~]# docker container run -d --name="nginx_server" daee903b4e43
8d6e3c68e599452edbf79efebb4d64bebbad44214577a8de3ef1fe00488b655e
[root@localhost ~]#
在宿主机验证
先用docker container inspect查看nginx容器的详细信息,找到nginx容器的IP地址,然后测试
[root@localhost ~]# curl -I 172.17.0.3
HTTP/1.1 200 OK
Server: nginx/1.19.4
Date: Thu, 26 Nov 2020 03:17:53 GMT
Content-Type: text/html
Content-Length: 612
Last-Modified: Tue, 27 Oct 2020 15:09:20 GMT
Connection: keep-alive
ETag: "5f983820-264"
Accept-Ranges: bytes
容器的查看
查看正在运行的容器
在宿主机查看正在运行的容器
命令:docker container ls
例:
查看刚刚跑起来的名为 centos6_server1 的容器
[root@localhost ~]# docker container ls
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
01355db802ad 2199b8eb8390 "/bin/bash" 14 minutes ago Up 14 minutes centos6_server1
CONTAINER ID : 容器的唯一号码(自动生成的)
STATUS : 容器的运行状态 ( Exited , Up)
NAMES : 容器的名字(可以自动,也可以手工指定)
查看所有容器
在宿主机查看所有容器
命令:docker container ls -a
先在容器内输入exit 命令会退出容器
[root@01355db802ad /]# exit
exit
[root@localhost ~]#
在查看所有容器 状态为退出
[root@localhost ~]# docker container ls -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
01355db802ad 2199b8eb8390 "/bin/bash" 27 minutes ago Exited (0) About a minute ago centos6_server1
查看容器的详细信息
命令:docker container inspect (CONTAINER ID or NAMES)
例:
[root@localhost ~]# docker container inspect nginx_server
[
{
"Id": "8d6e3c68e599452edbf79efebb4d64bebbad44214577a8de3ef1fe00488b655e",
"Created": "2020-11-26T03:06:15.595976335Z",
"Path": "/docker-entrypoint.sh",
"Args": [
"nginx",
"-g",
"daemon off;"
],
.....
容器的删除
删除单个容器
命令:docker container rm (CONTAINER ID or NAMES)
例:
[root@localhost ~]# docker container rm 01355db802ad
01355db802ad
删除所有容器
[root@localhost ~]# docker container rm -f `docker container ls -a -q`
设置退出容器退出之后自动删除 跑容器的时候加 --rm 参数
例:
[root@localhost ~]# docker container run -it --name="centos6_server3" --rm 2199b8eb8390
[root@5eccb532fba5 /]# exit
exit
[root@localhost ~]#
在宿主机查看退出之后删除成功
[root@localhost ~]# docker container ls -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
5eccb532fba5 2199b8eb8390 "/bin/bash" 48 seconds ago Up 46 seconds centos6_server3
8d6e3c68e599 daee903b4e43 "/docker-entrypoint.…" 25 minutes ago Up 25 minutes 80/tcp nginx_server
68c3b57162c8 2199b8eb8390 "/bin/bash" 32 minutes ago Up 32 minutes centos6_server2
[root@localhost ~]# docker container ls -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
8d6e3c68e599 daee903b4e43 "/docker-entrypoint.…" 25 minutes ago Up 25 minutes 80/tcp nginx_server
68c3b57162c8 2199b8eb8390 "/bin/bash" 32 minutes ago Up 32 minutes centos6_server2
容器的启动\关闭\连接
守护式容器的关闭和启动
stop CONTAINER ID or NAMES
[root@localhost ~]# docker container stop 8d6e3c68e599
8d6e3c68e599
start CONTAINER ID or NAMES
[root@localhost ~]# docker container start 8d6e3c68e599
8d6e3c68e599
交互式的容器的关闭和启动
stop CONTAINER ID or NAMES
[root@localhost ~]# docker container stop centos6_server2
centos6_server2
后台启动 start CONTAINER ID or NAMES
[root@localhost ~]# docker container start centos6_server2
centos6_server2
交互式启动 start -i CONTAINER ID or NAMES
[root@localhost ~]# docker container start -i centos6_server2
容器的连接方法:
attach
退出之后会停掉容器
[root@localhost ~]# docker container attach centos6_server2
[root@68c3b57162c8 /]#
[root@68c3b57162c8 /]# exit
exit
[root@localhost ~]# docker container ls -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
8d6e3c68e599 daee903b4e43 "/docker-entrypoint.…" 2 hours ago Up 11 minutes 80/tcp nginx_server
68c3b57162c8 2199b8eb8390 "/bin/bash" 2 hours ago Exited (0) 43 seconds ago centos6_server2
子进程的方式登录
在已有工作容器中生成子进程,做登录.可以用于进行容器的调试,退出时也不会影响到当前容器
root@localhost ~]# docker container exec -it centos6_server2 /bin/bash
[root@68c3b57162c8 /]# exit
exit
[root@localhost ~]#
[root@localhost ~]# docker container ls -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
8d6e3c68e599 daee903b4e43 "/docker-entrypoint.…" 2 hours ago Up 16 minutes 80/tcp nginx_server
68c3b57162c8 2199b8eb8390 "/bin/bash" 2 hours ago Up 2 minutes centos6_server2
容器的后台及前台运行:
(1) run容器之后按组合键 ctrl + P+Q 退出,容器仍在后台运行,然后attach 调用到前台
[root@localhost ~]# docker container run -it --name="centos6_server3" centos:6.9
[root@da9aef826483 /]# [root@localhost ~]#
[root@localhost ~]# docker container ls -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
da9aef826483 centos:6.9 "/bin/bash" 22 seconds ago Up 21 seconds centos6_server3
8d6e3c68e599 daee903b4e43 "/docker-entrypoint.…" 2 hours ago Up 26 minutes 80/tcp nginx_server
68c3b57162c8 2199b8eb8390 "/bin/bash" 2 hours ago Up 12 minutes centos6_server2
(2)让程序前台一直允许(夯在前台)
制作守护式容器时,常用的方法
[root@localhost ~]# docker container run --name="nginx_server1" daee903b4e43
容器的网络映射
指定映射(docker 会自动添加一条iptables规则来实现端口映射)
端口对端口
-p hostPort:containerPort
例:
创建并开启一台nginx容器,并将nginx容器的80端口映射到宿主机的8080端口,
[root@localhost ~]# docker container run -d --name="nginx_server" -p 8080:80 nginx:latest
b573c1347286afcdedcefe0cf39e871e740f90d8c8e0aa41e37055a2229fbb39
查看端口验证
[root@localhost ~]# netstat -nltp |grep 8080
tcp6 0 0 :::8080 :::* LISTEN 21553/docker-proxy
浏览器测试成功
指定IP
-p ip:hostPort:containerPort
例:
[root@localhost ~]# docker container run -d --name="nginx_server1" -p 192.168.153.174:8081:80 nginx:latest
8b17d56068a7ad81dddd3c86e316974109394428e7cea6f7e9678364b8b48a52
[root@localhost ~]#
随机端口
-p ip::containerPort(随机端口:32768-60999)
例:
[root@localhost ~]# docker container run -d --name="nginx_server3" -p 192.168.153.174::80 nginx:latest
8367d0e6381031ddc951ddb28a6491257d362df3b3b63f6527ceb33ee6b47f6c
[root@localhost ~]#
默认映射tcp端口,设置映射udp端口
-p hostPort:containerPort/udp
[root@localhost ~]# docker container run -d --name="nginx_server4" -p 53:53/udp nginx:latest
3a0a78792f73a0c5884fbd9ad89bd3e34c6491731fa7fd66175be48a983c2312
随机映射
docker run -P 80(随机端口)
例:
[root@localhost ~]# docker container run -d --name="nginx_server2" -p 80 nginx:latest
4929ea66064611fe9616ce661f42a2dd1bf488481f6b69001d84003d47f2d855
多端口映射
[root@localhost ~]# docker container run -d --name="nginx_server5" -p 8080:80 -p 33060:3306 -p 4430:443 nginx:latest
0145d6ed7f733659fbce4e02160f52725e731d3072f042f7116f585a47c1e023
[root@localhost ~]#
其他
docker ps -a -q 等价于 docker container ls -a -q
docker top ba9143bcaf74 等价于 docker container top ba9143bcaf74
查看日志::
[root@localhost ~]# docker container logs nginx_server1
[root@localhost ~]# docker container logs -t nginx_server1 // -t 时间戳
[root@localhost ~]# docker container logs -tf nginx_server1 // -f 动态查看
[root@localhost ~]# docker container logs -tf --tail 1 nginx_server1 //查看最后一行
小结
- 镜像类:
docker image
search
pull
ls *****
inspect *****
rm ****
save
load
- 容器类
docker container
run *****
start ****
stop ****
restart
kill
attach
exec *****
ls *****
top ****
logs
inspect ****