目录
一、Web应用容器使用
1. 运行Web应用
使用Docker构建一个Web应用程序Nginx。
1.1 随机映射端口
[root@localhost ~]# docker run -d -P --name nginx-v1 nginx
89c0b2769a2663db6f705bb82dafba372fed81072845ebc26e42cc8b6731d10a
命令参数解释:
- -d:容器在后台运行
- -P:将容器内部使用的网络端口随机映射到宿主机上
1.2 自定义端口映射
[root@localhost ~]# docker run -d -p 80:80 --name nginx-v2 nginx
5625c7e9fa412fd5f2a3edeb1ccf402df05197e5d0535a4d098e11a9f881ead8
命令参数解释:
- -p:自定义容器内部端口映射到宿主机端口上
2. 查看Web应用端口信息
2.1 docker ps命令
查看Web应用容器的详细信息。
1.查看随机端口映射:
[root@localhost ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
89c0b2769a26 nginx "/docker-entrypoint.…" 7 seconds ago Up 5 seconds 0.0.0.0:32768->80/tcp, :::32768->80/tcp nginx-v1
输出结果说明:
- PORTS:Web应用端口信息,Docker开放了80端口映射到宿主机32768端口上
通过浏览器访问IP:32769访问Web应用:
2.查看自定义端口映射:
[root@localhost ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
5625c7e9fa41 nginx "/docker-entrypoint.…" 5 seconds ago Up 3 seconds 0.0.0.0:80->80/tcp, :::80->80/tcp nginx-v2
输出结果说明:
- PORTS:Docker开放了80端口映射到宿主机80端口上
通过浏览器访问IP:80访问Web应用:
2.2 docker port命令
只查看指定Web应用容器的端口映射信息。
语法结构:
docker port 容器名/容器ID
[root@localhost ~]# docker port nginx-v1
80/tcp -> 0.0.0.0:32768
80/tcp -> [::]:32768
[root@localhost ~]# docker port nginx-v2
80/tcp -> 0.0.0.0:80
80/tcp -> [::]:80
3. 查看Web应用日志
查看容器内部的标准输出。
语法结构:
docker logs 容器名/容器ID
[root@localhost ~]# docker logs nginx-v1
/docker-entrypoint.sh: /docker-entrypoint.d/ is not empty, will attempt to perform configuration
/docker-entrypoint.sh: Looking for shell scripts in /docker-entrypoint.d/
...
[root@localhost ~]# docker logs -f nginx-v1 #实时查看,类似于tail -f命令
/docker-entrypoint.sh: /docker-entrypoint.d/ is not empty, will attempt to perform configuration
/docker-entrypoint.sh: Looking for shell scripts in /docker-entrypoint.d/
...
4. 查看Web应用容器进程
查看容器内部运行的进程。
语法结构:
docker top 容器名/容器ID
[root@localhost ~]# docker top nginx-v1
UID PID PPID C STIME TTY TIME CMD
root 3111 3091 0 14:43 ? 00:00:00 nginx: master process nginx -g daemon off;
101 3157 3111 0 14:43 ? 00:00:00 nginx: worker process
二、容器互连
Docker存在一个连接系统允许多个容器连接在一起,共享信息。Docker连接会创建一个父子关系,其中父容器可以看到子容器的信息。
1. 创建网络
创建一个网络用于容器之间的连接。
语法结构:
docker network create -d [网络类型] 网络名
[root@localhost ~]# docker network create -d bridge net
197627374b93dfe7811d44ee3d44873ccc163758ced94c2c40950d652f2450de
命令参数解释:
- -d:指定网络类型之一
bridge(网络桥接):默认情况下启动、创建容器。每次容器重启时会按顺序获取对应IP
none(无指定网络):启动容器通过--network=none,不分配局域网IP,无法访问网页
host(主机网络):网络附属在宿主机上,两者互通
2. 查看网络信息
语法结构:
docker network ls
[root@localhost ~]# docker network ls
NETWORK ID NAME DRIVER SCOPE
2940bea1f910 bridge bridge local
ff0b815341dc host host local
197627374b93 net bridge local
3. 连接容器
运行容器并连接到相同的网络中。
运行容器test1和test2,并将两个容器添加到网络net中:
[root@localhost ~]# docker run -itd --name test1 --network net ubuntu /bin/sh
6c4d3839015a7ed9c28b685cae6c1205a8f6230eb9fa2fd0bfb2e8d435e5f892
[root@localhost ~]# docker run -itd --name test2 --network net ubuntu /bin/sh
7f35c14c1fb7b20427eb11c78105e93a77722eb7603aea78b086be1b1e8c410a
命令参数解释:
- --network:添加到存在的网络
想要在test1和test2可以相互ping通,需要先运行容器test1,在test1内ping test2发现没有ping命令,于是更行软件列表并下载ping软件包,之后再ping test2即可成功。容器test2如果想要ping通test1重复上述相同的步骤即可。
但从如下命令可以看出我未使用上述方式在test2中下载ping软件包,而是使用docker commit(详细介绍见构建镜像篇)提交容器test1(已经下载了ping软件包)到新的镜像max/ubuntu:V1。
[root@localhost ~]# docker exec -it test1 /bin/bash
root@6c4d3839015a:/# ping test2
bash: ping: command not found
root@6c4d3839015a:/# apt-get update
root@6c4d3839015a:/# apt -y install iputils-ping
root@6c4d3839015a:/# exit
[root@localhost ~]# docker commit -m="has ping" -a="max" 6c4d3839015a max/ubuntu:V1
sha256:cc7fb7ff97544fa3198ace7e4c1943be38532a0715eeec3c56d0321248583f4d
命令参数解释:
- -m:提交的描述信息
- -a:指定镜像作者
- max/ubuntu:V1:所要创建的镜像名
重新构建了一个新的镜像max/ubuntu:V1,在此镜像上运行容器test3和test4,分别进入test3和test4容器ping对方。
[root@localhost ~]# docker run -itd --name test3 --network net max/ubuntu:V1 /bin/bash
1400ec874081e7cf5cf1fa326399bf706c9edb126da1de489c5a7f6a9bd74375
[root@localhost ~]# docker run -itd --name test4 --network net max/ubuntu:V1 /bin/bash
ab5755e4e7b28dbf2985c5bb211758d2ff5eb4731e3216762de32f245b27a560
[root@localhost ~]# docker exec -it test3 /bin/bash
root@1400ec874081:/# ping test4
PING test4 (172.18.0.5) 56(84) bytes of data.
64 bytes from test4.net (172.18.0.5): icmp_seq=1 ttl=64 time=0.122 ms
64 bytes from test4.net (172.18.0.5): icmp_seq=2 ttl=64 time=0.088 ms
^C
--- test4 ping statistics ---
4 packets transmitted, 4 received, 0% packet loss, time 3002ms
rtt min/avg/max/mdev = 0.088/0.103/0.122/0.012 ms
root@1400ec874081:/# exit
[root@localhost ~]# docker exec -it test4 /bin/bash
root@ab5755e4e7b2:/# ping test3
PING test3 (172.18.0.4) 56(84) bytes of data.
64 bytes from test3.net (172.18.0.4): icmp_seq=1 ttl=64 time=0.227 ms
64 bytes from test3.net (172.18.0.4): icmp_seq=2 ttl=64 time=0.135 ms
^C
--- test3 ping statistics ---
3 packets transmitted, 3 received, 0% packet loss, time 2003ms
rtt min/avg/max/mdev = 0.135/0.227/0.320/0.075 ms
root@ab5755e4e7b2:/# exit
如果想要多个容器之间互连,推荐使用Docker Compose。