服务热线
135-6963-3175
Docker容器映射连接
1、容器网络端口映射
docker run -d -P mysql:latest
-P:是容器内部端口随机映射到宿主机的高端口
-p:容器内部端口绑定到执行的主机端口,格式:宿主机端口:容器端口
例:
docker run -d -p 3306:3306 mysql:latest
注:还可指定容器绑定的网络地址:比如
docker run -d -p 127.0.0.1:8081:8080 springboot.jar
这样就可以通过127.0.0.1:8081来访问容器的8080端口。默认都是tcp端口,如果要绑定udp,可以在端口后面加/udp,例:
docker run -d -p 127.0.0.1:8081:8080/udp springboot.jar
注: 可因能同时存在多张网卡,当绑定到127.0.0.1后用,用127.0.0.1:8081或者localhost:8081可以访问,但是通过局域网ip 192.168.1.100:8081就无法访问,反之。
docker port可以查看容器端口的绑定情况
docker port 容器id 3306
[root@localhost ~]# docker port f3d 3306 0.0.0.0:3306
Docker容器互联
docker除了通过端口绑定映射连接容器,还可以通过其提供的连接系统将多个容器连接在一起,共享连接信息。
docker连接会创建一个父子关系,其中父容器可以看到子容器的信息。
容器命名
docker 参数 -name
例如:
[root@localhost ~]#docker run -itd --name mysql-dev -p 3306:3306 -e MYSQL_ROOT_PASSWORD=123456 mysql [root@localhost ~]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES f3de50b00c3c mysql "docker-entrypoint.s…" 17 minutes ago Up 17 minutes 0.0.0.0:3306->3306/tcp, 33060/tcp mysql-dev
创建容器网络
查看当前网络 [root@localhost ~]# docker network ls NETWORK ID NAME DRIVER SCOPE d624be153292 bridge bridge local 683012808dee host host local 4b9609169f55 none null local 创建网络 [root@localhost ~]# docker network create -d bridge test-net 6b7c1ae0fdb436211a40a61c36bf086dcc9461825c16bfd2f6d5339519d34cd2 查看网络,多出一个当前创建的网络 [root@localhost ~]# docker network ls NETWORK ID NAME DRIVER SCOPE d624be153292 bridge bridge local 683012808dee host host local 4b9609169f55 none null local 6b7c1ae0fdb4 test-net bridge local 启动一个mysql实例mysql-fat,网络为上面创建的test-net [root@localhost ~]# docker run -itd --name mysql-fat --network test-net -p 3307:3306 -e MYSQL_ROOT_PASSWORD=123456 mysql b1de8fba00ca6cef761eb316a5e44681212b7cad3ecd841be877eb3dcbce3015 再启动一个mysql实例mysql-prod,网络为上面创建的test-net [root@localhost ~]# docker run -itd --name mysql-prod --network test-net -p 3308:3306 -e MYSQL_ROOT_PASSWORD=123456 mysql 3e0f0091deaf42561efb9f11654eb8d9d1d0bcdc200d3866ada9773818537548
测试网路是否互通
进入容器 [root@localhost ~]# docker exec -it mysql-fat /bin/bash 发现没有ping命令 root@b1de8fba00ca:/# ping mysql-prod bash: ping: command not found root@b1de8fba00ca:/# yum install ping bash: yum: command not found 进行安装 root@b1de8fba00ca:/# apt-get update Get:1 http://security.debian.org/debian-security buster/updates InRelease [65.4 kB] Get:2 http://deb.debian.org/debian buster InRelease [122 kB] Get:3 http://repo.mysql.com/apt/debian buster InRelease [21.5 kB] Get:4 http://repo.mysql.com/apt/debian buster/mysql-8.0 amd64 Packages [7172 B] Get:2 http://deb.debian.org/debian buster InRelease [122 kB] Get:5 http://deb.debian.org/debian buster-updates InRelease [51.9 kB] Get:6 http://deb.debian.org/debian buster/main amd64 Packages [7906 kB] Get:6 http://deb.debian.org/debian buster/main amd64 Packages [7906 kB] Reading package lists... Done ... root@b1de8fba00ca:/# apt install iputils-ping Reading package lists... Done Building dependency tree Reading state information... Done The following additional packages will be installed: libcap2 libcap2-bin libpam-cap The following NEW packages will be installed: iputils-ping libcap2 libcap2-bin libpam-cap 0 upgraded, 4 newly installed, 0 to remove and 0 not upgraded. Need to get 104 kB of archives. After this operation, 319 kB of additional disk space will be used. Do you want to continue? [Y/n] y Get:1 http://deb.debian.org/debian buster/main amd64 libcap2 amd64 1:2.25-2 [17.6 kB] Get:2 http://deb.debian.org/debian buster/main amd64 iputils-ping amd64 3:20180629-2+deb10u1 [43.3 kB] Get:3 http://deb.debian.org/debian buster/main amd64 libcap2-bin amd64 1:2.25-2 [28.8 kB] Get:4 http://deb.debian.org/debian ... root@b1de8fba00ca:/# ping mysql-prod PING mysql-prod (172.18.0.3) 56(84) bytes of data. 64 bytes from mysql-prod.test-net (172.18.0.3): icmp_seq=1 ttl=64 time=0.124 ms 64 bytes from mysql-prod.test-net (172.18.0.3): icmp_seq=2 ttl=64 time=0.064 ms 64 bytes from mysql-prod.test-net (172.18.0.3): icmp_seq=3 ttl=64 time=0.073 ms 然后进入到mysql-prod进行同样的操作 root@b1de8fba00c1:/# ping mysql-fat PING mysql-fat (172.18.0.3) 56(84) bytes of data. 64 bytes from mysql-fat.test-net (172.18.0.3): icmp_seq=1 ttl=64 time=0.124 ms 64 bytes from mysql-fat.test-net (172.18.0.3): icmp_seq=2 ttl=64 time=0.064 ms 64 bytes from mysql-fat.test-net (172.18.0.3): icmp_seq=3 ttl=64 time=0.073 ms 表明容器网络已互联
若有多个容器需要互联,推荐使用docker-compose。
我们可以在宿主机 /etc/docker/daemon.json 来设置所有容器的DNS
{ "registry-mirrors": ["https://registry.docker-cn.com","https://docker.mirrors.ustc.edu.cn","https://pee6w651.mirror.aliyuncs.com","http://hub-mirror.c.163.com"], "live-restore": true, "dns" : [ "114.114.114.114", "8.8.8.8" ] }
[root@localhost docker]# service docker restart Redirecting to /bin/systemctl restart docker.service [root@localhost docker]# docker run -it --rm -h mysql-test -p 3309:3306 -e MYSQL_ROOT_PASSWORD=123456 mysql cat etc/resolv.conf nameserver 114.114.114.114 nameserver 8.8.8.8 [root@localhost docker]#
手动指定dns方式:
docker run -it --rm -h mysql-test --dns=114.114.114.114 --dns-search=test.com -p 3309:3306 -e MYSQL_ROOT_PASSWORD=123456 mysql cat etc/resolv.conf
--rm:容器退出时自动清理容器内部的文件系统。
-h HOSTNAME 或者 --hostname=HOSTNAME: 设定容器的主机名,它会被写到容器内的 /etc/hostname 和 /etc/hosts。
--dns=IP_ADDRESS: 添加 DNS 服务器到容器的 /etc/resolv.conf 中,让容器用这个服务器来解析所有不在 /etc/hosts 中的主机名。
--dns-search=DOMAIN: 设定容器的搜索域,当设定搜索域为 .example.com 时,在搜索一个名为 host 的主机时,DNS 不仅搜索 host,还会搜索 host.example.com。
如果在容器启动时没有指定 --dns 和 --dns-search,Docker 会默认用宿主主机上的 /etc/resolv.conf 来配置容器的 DNS。