这就是你日日夜夜想要的docker!!!---------Docker常规操作--端口映射、数据卷管理 以及容器互联
文章目录一、端口映射1、-P随机端口2、映射指定一个端口3、映射指定多个端口4、映射到指定端口的指定地址5、映射到指定端口的任意地址6、查看端口映射情况二、数据卷与数据卷容器1、挂载宿主系统(数据卷)2、数据卷容器操作三、容器互联1、--link 通过虚链路连接2、通过中转网卡连接一、端口映射在启动容器的时候,如果不指定对应的参数,在容器外部是无法通过网络来访问容器内部的网络应用和服务的。 当容
文章目录
一、端口映射
在启动容器的时候,如果不指定对应的参数,在容器外部是无法通过网络来访问容器内部的网络应用和服务的。
当容器中运行一些网络应用,要让外部访问这些应用时,可以通过-p或-P参数来指定端口映射。当使用-P(大写P)标记时,Docker会随机映射一个端口到内部容器开放的网络端口
1、-P随机端口
通过docker ps命令查看具体端口映射
[root@localhost opt]# docker run -d --name nginx_1 -P nginx:latest
64a061f37a4948d4b8b4dc28d3b0c0b51ceb65e1aef98b36cbc063af69352e85
[root@localhost opt]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
64a061f37a49 nginx:latest "/docker-entrypoint.…" 9 seconds ago Up 8 seconds 0.0.0.0:32769->80/tcp nginx_1
91781d90ed6d nginx "/docker-entrypoint.…" 26 minutes ago Created nostalgic_kilby
通过浏览器访问本机ip地址加映射的端口 就能访问到容器内的nginx
[root@localhost opt]# docker logs nginx_1 '查看nginx_1容器的日志信息'
/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/
/docker-entrypoint.sh: Launching /docker-entrypoint.d/10-listen-on-ipv6-by-default.sh
10-listen-on-ipv6-by-default.sh: Getting the checksum of /etc/nginx/conf.d/default.conf
10-listen-on-ipv6-by-default.sh: Enabled listen on IPv6 in /etc/nginx/conf.d/default.conf
/docker-entrypoint.sh: Launching /docker-entrypoint.d/20-envsubst-on-templates.sh
/docker-entrypoint.sh: Configuration complete; ready for start up
192.168.110.1 - - [20/Sep/2020:11:27:41 +0000] "GET / HTTP/1.1" 200 612 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/85.0.4183.83 Safari/537.36" "-"
2020/09/20 11:27:41 [error] 28#28: *1 open() "/usr/share/nginx/html/favicon.ico" failed (2: No such file or directory), client: 192.168.110.1, server: localhost, request: "GET /favicon.ico HTTP/1.1", host: "192.168.110.133:32769", referrer: "http://192.168.110.133:32769/"
192.168.110.1 - - [20/Sep/2020:11:27:41 +0000] "GET /favicon.ico HTTP/1.1" 404 555 "http://192.168.110.133:32769/" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/85.0.4183.83 Safari/537.36" "-"
[root@localhost opt]# docker logs nginx_1
/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/
/docker-entrypoint.sh: Launching /docker-entrypoint.d/10-listen-on-ipv6-by-default.sh
10-listen-on-ipv6-by-default.sh: Getting the checksum of /etc/nginx/conf.d/default.conf
10-listen-on-ipv6-by-default.sh: Enabled listen on IPv6 in /etc/nginx/conf.d/default.conf
/docker-entrypoint.sh: Launching /docker-entrypoint.d/20-envsubst-on-templates.sh
/docker-entrypoint.sh: Configuration complete; ready for start up
192.168.110.1 - - [20/Sep/2020:11:27:41 +0000] "GET / HTTP/1.1" 200 612 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/85.0.4183.83 Safari/537.36" "-"
2020/09/20 11:27:41 [error] 28#28: *1 open() "/usr/share/nginx/html/favicon.ico" failed (2: No such file or directory), client: 192.168.110.1, server: localhost, request: "GET /favicon.ico HTTP/1.1", host: "192.168.110.133:32769", referrer: "http://192.168.110.133:32769/"
192.168.110.1 - - [20/Sep/2020:11:27:41 +0000] "GET /favicon.ico HTTP/1.1" 404 555 "http://192.168.110.133:32769/" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/85.0.4183.83 Safari/537.36" "-"
[root@localhost opt]#
2、映射指定一个端口
[root@localhost opt]# docker run -itd -p 5000:80 --name nginx_2 nginx:latest
c6209375b508a67cbaae20b93e140e24a233b9a5a9a4b97db386782a769214f4
[root@localhost opt]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
c6209375b508 nginx:latest "/docker-entrypoint.…" About a minute ago Up About a minute 0.0.0.0:5000->80/tcp nginx_2
64a061f37a49 nginx:latest "/docker-entrypoint.…" 18 minutes ago Up 18 minutes 0.0.0.0:32769->80/tcp nginx_1
91781d90ed6d nginx "/docker-entrypoint.…" 44 minutes ago Created nostalgic_kilby
[root@localhost opt]#
3、映射指定多个端口
[root@localhost opt]# docker run -itd -p 3000:80 -p 4000:80 --name nginx_3 nginx:latest
a242202d9061d83191e63c9b602f9b35927fb81008a1b82f0c9a2e203f51552d
[root@localhost opt]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
a242202d9061 nginx:latest "/docker-entrypoint.…" 4 seconds ago Up 3 seconds 0.0.0.0:3000->80/tcp, 0.0.0.0:4000->80/tcp nginx_3
c6209375b508 nginx:latest "/docker-entrypoint.…" 6 minutes ago Up 6 minutes 0.0.0.0:5000->80/tcp nginx_2
64a061f37a49 nginx:latest "/docker-entrypoint.…" 23 minutes ago Up 23 minutes 0.0.0.0:32769->80/tcp nginx_1
91781d90ed6d nginx "/docker-entrypoint.…" 49 minutes ago Created nostalgic_kilby
4、映射到指定端口的指定地址
[root@localhost opt]# docker run -itd -p 192.168.110.133:1000:80 --name nginx_4 nginx:latest
66a3161186e3f573a8ed55fa4abb974ceefcdca389330c9f8823bd4f0daaa102
[root@localhost opt]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
66a3161186e3 nginx:latest "/docker-entrypoint.…" 10 seconds ago Up 8 seconds 192.168.110.133:1000->80/tcp nginx_4
a242202d9061 nginx:latest "/docker-entrypoint.…" 13 minutes ago Up 13 minutes 0.0.0.0:3000->80/tcp, 0.0.0.0:4000->80/tcp nginx_3
c6209375b508 nginx:latest "/docker-entrypoint.…" 20 minutes ago Up 20 minutes 0.0.0.0:5000->80/tcp nginx_2
64a061f37a49 nginx:latest "/docker-entrypoint.…" 37 minutes ago Up 37 minutes 0.0.0.0:32769->80/tcp nginx_1
91781d90ed6d nginx "/docker-entrypoint.…" About an hour ago Created nostalgic_kilby
5、映射到指定端口的任意地址
使用IP::ContainerPort格式绑定本机的任意端口到容器的指定端口
[root@localhost opt]# docker run -itd -p 192.168.110.133::80 --name nginx_5 nginx:latest
3f4c88cce0fbf1b10629066c24d97a3a706cff319e6f64cf88455ef19eb96514
[root@localhost opt]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
3f4c88cce0fb nginx:latest "/docker-entrypoint.…" 9 seconds ago Up 8 seconds 192.168.110.133:32768->80/tcp nginx_5
66a3161186e3 nginx:latest "/docker-entrypoint.…" 6 minutes ago Up 6 minutes 192.168.110.133:1000->80/tcp nginx_4
a242202d9061 nginx:latest "/docker-entrypoint.…" 20 minutes ago Up 20 minutes 0.0.0.0:3000->80/tcp, 0.0.0.0:4000->80/tcp nginx_3
c6209375b508 nginx:latest "/docker-entrypoint.…" 26 minutes ago Up 26 minutes 0.0.0.0:5000->80/tcp nginx_2
64a061f37a49 nginx:latest "/docker-entrypoint.…" 43 minutes ago Up 43 minutes 0.0.0.0:32769->80/tcp nginx_1
91781d90ed6d nginx "/docker-entrypoint.…" About an hour ago Created nostalgic_kilby
6、查看端口映射情况
[root@localhost opt]# docker port nginx_1
80/tcp -> 0.0.0.0:32769
[root@localhost opt]# docker port nginx_2
80/tcp -> 0.0.0.0:5000
[root@localhost opt]# docker port nginx_3
80/tcp -> 0.0.0.0:4000
80/tcp -> 0.0.0.0:3000
[root@localhost opt]# docker port nginx_4
80/tcp -> 192.168.110.133:1000
[root@localhost opt]# docker port nginx_5
80/tcp -> 192.168.110.133:32768
二、数据卷与数据卷容器
使用容器时会产生一些日志或其他文件,或是我们需要把容器内的数据进行备份,甚至多个容器之间进行数据共享,这样就会涉及容器的数据管理操作。
容器中管理数据主要有两种方式:
- 数据卷:挂载宿主系统的存储空间
- 数据卷容器:挂载容器的存储空间
如果用户需要在容器之间共享一些持续更新的数据,最简单的方式是使用数据卷容器,数据卷容器其实是一个普通的容器,专门用来提供数据卷供其它容器挂载
原理图如下
1、挂载宿主系统(数据卷)
-v 指定宿主机目录:docker目录
–name 指定名称
-itd 打开标准型输入,新开一个虚拟tty 后台运行
centos:7 指定镜像
/bin/bash 指定bash环境
[root@localhost test]# docker run -v /opt/test:/data1 --name web -itd centos:7 /bin/bash
f2acf3b3c54331ca763e0b64f4e269fe3a09028c94229dbd23e0d865919644ff
[root@localhost test]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
f2acf3b3c543 centos:7 "/docker-entrypoint.…" 12 seconds ago Up 11 seconds 80/tcp web
eae6d77f4772 centos:7 "/docker-entrypoint.…" 4 minutes ago Exited (0) 4 minutes ago web1
[root@localhost opt]# cd /opt/test/ '进入到宿主机上创建文件 '
[root@localhost test]# touch dog
[root@localhost test]# docker exec -it f2 /bin/bash '进入到docker查看同步情况 f2是缩写 ps-a 查看id号 id号的前两位 '
root@f2acf3b3c543:/# ls
bin boot data1 dev docker-entrypoint.d docker-entrypoint.sh etc home lib lib64 media mnt opt proc root run sbin srv sys tmp usr var
root@f2acf3b3c543:/# cd data1/
root@f2acf3b3c543:/data1# ls
dog
2、数据卷容器操作
首先创建一个docker 并共享两个目录创建挂载点 data1和data2
[root@localhost opt]# docker run -v /data1 -v /data2 -itd --name test01 centos:7 /bin/bash
886ad4041f028d96f78ea031283c635c1ba78a1b472e84fbc210b1618ffdf141
[root@localhost opt]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
886ad4041f02 centos:7 "/docker-entrypoint.…" 9 seconds ago Up 8 seconds 80/tcp test01
[root@localhost opt]# docker exec -it 886 /bin/bash '进入到docker中查看共享目录是否存在'
root@886ad4041f02:/# ls
bin data1 dev docker-entrypoint.sh home lib64 mnt proc run srv tmp var
boot data2 docker-entrypoint.d etc lib media opt root sbin sys usr
root@886ad4041f02:/#
对docker test01的挂载电data进行远程挂载
[root@localhost opt]# docker run -itd --volumes-from test01 --name test02 centos:7 /bin/bash
3161a893a34899e1b660e5b93a1bd1f268b825c19503f9eac91c803c067c2746
[root@localhost opt]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
3161a893a348 centos:7 "/docker-entrypoint.…" 24 seconds ago Up 23 seconds 80/tcp test02
886ad4041f02 centos:7 "/docker-entrypoint.…" 2 minutes ago Up 2 minutes 80/tcp test01
进入到test1创建文件 观察test2内时候也进行同步生成了
[root@localhost opt]# docker exec -it 316 /bin/bash
root@3161a893a348:/# ls
bin data1 dev docker-entrypoint.sh home lib64 mnt proc run srv tmp var
boot data2 docker-entrypoint.d etc lib media opt root sbin sys usr
root@3161a893a348:/# cd data1
root@3161a893a348:/data1# ls
root@3161a893a348:/data1# touch 111
root@3161a893a348:/data1# cd..
bash: cd..: command not found
root@3161a893a348:/data1# cd ..
root@3161a893a348:/# cd data2
root@3161a893a348:/data2# ls
root@3161a893a348:/data2# touch 222
root@3161a893a348:/data2# exit
exit
进入到test02中查看是否同步
[root@localhost opt]# docker exec -it 886 /bin/bash
root@886ad4041f02:/# ls
bin boot data1 data2 dev docker-entrypoint.d docker-entrypoint.sh etc home lib lib64 media mnt opt proc root run sbin srv sys tmp usr var
root@886ad4041f02:/# ls data1
111
root@886ad4041f02:/# ls data2
222
root@886ad4041f02:/#
三、容器互联
1、–link 通过虚链路连接
–link 两个容器通过映射的端口进行互联
[root@localhost opt]# docker run -itd -P --name test01 centos:7 /bin/bash
b55327c3ae74296a81304ae4563e3da54d66409ce322bb2d8223bc59882eef8f
[root@localhost opt]# docker run -itd -P --name test02 --link test01 centos:7 /bin/bash
57582aa8724545f55f14973b6c9c06c89a96cc835dea1148050de8c298a12c3a
[root@localhost opt]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS
57582aa87245 centos:7 "/docker-entrypoint.…" 26 seconds ago Up 26 seconds
b55327c3ae74 centos:7 "/docker-entrypoint.…" 45 seconds ago Up 45 seconds
[root@localhost opt]# docker exec -it 57 /bin/bash '进入到test02内ping test01'
root@57582aa87245:/# ping test01
PING test001 (172.17.0.5) 56(84) bytes of data.
64 bytes from test001 (172.17.0.5): icmp_seq=1 ttl=64 time=0.177 ms
64 bytes from test001 (172.17.0.5): icmp_seq=2 ttl=64 time=0.147 ms
64 bytes from test001 (172.17.0.5): icmp_seq=3 ttl=64 time=0.157 ms
64 bytes from test001 (172.17.0.5): icmp_seq=4 ttl=64 time=0.202 ms
对于容器A和容器B之间使用–link连接,必须先启动其中一个容器,比如容器A,然后在启动容器B时,将其连接到容器A。这样,就可以在容器B中使用网络别名连接容器A了。
2、通过中转网卡连接
推荐使用这个方式。
默认网络中的link是静态的,不允许链接容器重启,而自定义网络下的link是动态的,支持链接容器重启(以及IP变化)
因此,使用–link时链接的容器,在默认网络中必须提前创建好,而自定义网络下不必预先建好。使用网络别名后,不管容器ip地址如何变化,都可以根据别名进行连接。
而且这种方式可以使得多个容器互联
[root@localhost ~]# docker network create mynet '首先在主机上创建一个网络'
[root@localhost ~]# ifconfig '查看新创建的网卡'
br-3b261b710b8a: flags=4099<UP,BROADCAST,MULTICAST> mtu 1500
inet 172.18.0.1 netmask 255.255.0.0 broadcast 172.18.255.255
ether 02:42:0a:1e:3b:a3 txqueuelen 0 (Ethernet)
RX packets 88 bytes 7680 (7.5 KiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 88 bytes 7680 (7.5 KiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
–network mynet表示使用mynet网络
–network-alias centos1表示该容器运行时,使用的网络别名为centos1。(网络别名的作用类似hostname,不管容器IP如何变化,都可以使用同一个别名。
–name 表示此容器的别名
-itd 表示后台运行 打开一个虚拟终端 打开标准型输入
[root@localhost docker]# docker run -itd --name centos1 --network mynet --network-alias centos1 centos /bin/bash
1d2ff212498c05769b180e32e7277c25b1f23d868a3edc6a99524f7106414aad
[root@localhost docker]# docker run -itd --name centos2 --network mynet --network-alias centos2 centos /bin/bash
2ed5fab1c899a2f2170294c94dd8db8582052ac37d8f06f16e79b7e601599e75
进入到centos1进行验证
[root@localhost docker]# docker exec -it centos1 /bin/bash
[root@1d2ff212498c /]# exit
exit
[root@localhost docker]# docker exec -it centos2 /bin/bash
[root@2ed5fab1c899 /]# ping centos1
PING centos1 (172.18.0.2) 56(84) bytes of data.
64 bytes from centos1.mynet (172.18.0.2): icmp_seq=1 ttl=64 time=0.105 ms
64 bytes from centos1.mynet (172.18.0.2): icmp_seq=2 ttl=64 time=0.091 ms
^C
--- centos1 ping statistics ---
2 packets transmitted, 2 received, 0% packet loss, time 2ms
rtt min/avg/max/mdev = 0.091/0.098/0.105/0.007 ms
[root@2ed5fab1c899 /]# exit
exit
进入到centos2中进行验证
[root@localhost docker]# docker exec -it centos1 /bin/bash
[root@1d2ff212498c /]# ping centos2
PING centos2 (172.18.0.3) 56(84) bytes of data.
64 bytes from centos2.mynet (172.18.0.3): icmp_seq=1 ttl=64 time=0.085 ms
64 bytes from centos2.mynet (172.18.0.3): icmp_seq=2 ttl=64 time=0.098 ms
64 bytes from centos2.mynet (172.18.0.3): icmp_seq=3 ttl=64 time=0.085 ms
^C
--- centos2 ping statistics ---
3 packets transmitted, 3 received, 0% packet loss, time 3ms
rtt min/avg/max/mdev = 0.085/0.089/0.098/0.009 ms
[root@1d2ff212498c /]#
更多推荐
所有评论(0)