docker(8):docker容器的端口映射
docker(8):docker容器的端口映射
cker的端口映射,能保证在外部网络 访问 同一设备上不同容器时候 不会因为端口问题产生冲突。
docker 可将容器对外提供服务的端口映射到 host 的某个端口,外网通过该端口访问容器。容器启动时通过-p
参数映射端口:
指定映射
- -p hostPort:containerPort #0.0.0.0:80
- -p ip:hostPort:containerPort #指定宿主机IP地址,例如192.168.0.136:80,多个容器都想使用80端口,添加多个IP
- -p ip::containerPort #随机端口 ,32768端口,内核参数决定的net.ipv4.ip_local_port_range = 32768 60999
- -p hostPort:containerPort:udp #udp
- -p 81:80 –p 443:443 #指定多个
默认
[root@docker-136 ~]# docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES [root@docker-136 ~]# docker run --name web1 -d -p 80:80 nginx 9d2300fd85bed6f22dec3cf7ca4cab0c1f3e753e34bdf0b6dcc152246cbecd39 [root@docker-136 ~]# netstat -lntp Active Internet connections (only servers) Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 6862/sshd tcp6 0 0 :::80 :::* LISTEN 27641/docker-proxy tcp6 0 0 :::22 :::* LISTEN 6862/sshd
指定端口
[root@docker-136 ~]# ifconfig ens33:1 192.168.0.199/24 up [root@docker-136 ~]# ifconfig ens33:1 ens33:1: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500 inet 192.168.0.199 netmask 255.255.255.0 broadcast 192.168.0.255 ether 00:0c:29:51:f5:dd txqueuelen 1000 (Ethernet) [root@docker-136 ~]# [root@docker-136 ~]# docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES [root@docker-136 ~]# docker run --name web2 -d -p 192.168.0.199:80:80 nginx e765cce89d8041a33b832875476ad5114d1c6521d1e8c23a4e96c31ef6ab4b3d [root@docker-136 ~]# docker run --name web1 -d -p 192.168.0.136:80:80 nginx 234d7356881bc1717d30e5bd639b2a607bf67933faad0106ee8d9d0a0a06cd1e [root@docker-136 ~]# netstat -lntp Active Internet connections (only servers) Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name tcp 0 0 192.168.0.136:80 0.0.0.0:* LISTEN 28469/docker-proxy tcp 0 0 192.168.0.199:80 0.0.0.0:* LISTEN 28370/docker-proxy tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 6862/sshd tcp6 0 0 :::22 :::* LISTEN 6862/sshd [root@docker-136 ~]#
随机
[root@docker-136 ~]# docker run --name web3 -d -p 192.168.0.136::80 nginx d13119eb08088b264a0727d1afd4d5961e9309ba4f28f507f5809666cccde27f [root@docker-136 ~]# netstat -lntp Active Internet connections (only servers) Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name tcp 0 0 192.168.0.136:80 0.0.0.0:* LISTEN 28469/docker-proxy tcp 0 0 192.168.0.199:80 0.0.0.0:* LISTEN 28370/docker-proxy tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 6862/sshd tcp 0 0 192.168.0.136:32768 0.0.0.0:* LISTEN 28877/docker-proxy tcp6 0 0 :::22 :::* LISTEN 6862/sshd [root@docker-136 ~]#
多个端口
[root@docker-136 ~]# docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES d13119eb0808 nginx "nginx -g 'daemon of…" 2 minutes ago Up 2 minutes 192.168.0.136:32768->80/tcp web3 [root@docker-136 ~]# [root@docker-136 ~]# docker run --name web4 -d -p 80:80 -p 443:443 nginx 4927f0ef45ab2741d894a5ae5e5ccec373970a477c07bf2f9f2e9fb0ddd9f018 [root@docker-136 ~]# docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 4927f0ef45ab nginx "nginx -g 'daemon of…" 3 seconds ago Up 1 second 0.0.0.0:80->80/tcp, 0.0.0.0:443->443/tcp web4 d13119eb0808 nginx "nginx -g 'daemon of…" 2 minutes ago Up 2 minutes 192.168.0.136:32768->80/tcp web3 [root@docker-136 ~]# netstat -lntp Active Internet connections (only servers) Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 6862/sshd tcp 0 0 192.168.0.136:32768 0.0.0.0:* LISTEN 28877/docker-proxy tcp6 0 0 :::80 :::* LISTEN 29287/docker-proxy tcp6 0 0 :::22 :::* LISTEN 6862/sshd tcp6 0 0 :::443 :::* LISTEN 29276/docker-proxy [root@docker-136 ~]#