(四)docker:为mysql和java jar运行环境创建同一网络,容器互联

  1. 看了很多资料,说做互联的一个原因是容器内ip不固定,关掉重启后如果有别的容器启动,之前的ip会被占用,所以做互联创建一个网络,让几个容器处于同一个网络,就可以互联还不受关闭再启动ip会改变的影响,但是其实我拿我做的mysql测试,不管是代码里还是sql连接工具,使用容器内ip根本连接不到,只有使用我本机的ip连接才能连接上,所以实际容器内部ip变化对我的使用并没有影响,但是我特别在网上查了很多,他们好像用本地ip都连接不上,只能用容器内的ip,这个我就不直到为什么了,不过我只是为了学习如何创建网络,以及将新建的容器和既存的容器连接到一个网络即可,现在不懂的地方可以在以后有机会的时候就能知道为什么了,忽略次要的,重点先学会用。
  2. 先创建一个网络
   docker network create -d bridge sixdog
  1. 查看创建的网络
sixdog@sixiaodong dockerfile % docker network ls
NETWORK ID     NAME      DRIVER    SCOPE
5451e1cd08f8   bridge    bridge    local
d8b19cbdca68   host      host      local
f46cec15b991   none      null      local
1dadf784140f   sixdog    bridge    local
  1. 我现在有一个既存的容器mysql-dev,准备把它连接到sixdog网络里

4.1 查看mysql-dev容器

sixdog@sixiaodong dockerfile % docker ps -a
CONTAINER ID   IMAGE                       COMMAND                   CREATED         STATUS                      PORTS                               NAMES
465ae184cd56   mysql:latest                "docker-entrypoint.s…"   23 months ago   Up 24 hours                 33060/tcp, 0.0.0.0:3307->3306/tcp   mysql-dev

4.2 查看mysql-dev部分网络信息

sixdog@sixiaodong dockerfile % docker inspect mysql-dev
其他部分省略
"Gateway": "172.17.0.1",
            "GlobalIPv6Address": "",
            "GlobalIPv6PrefixLen": 0,
            "IPAddress": "172.17.0.2",
            "IPPrefixLen": 16,
            "IPv6Gateway": "",
            "MacAddress": "02:42:ac:11:00:02",
            "Networks": {
                "bridge": {
                    "IPAMConfig": null,
                    "Links": null,
                    "Aliases": null,
                    "NetworkID": "5451e1cd08f83eeba5495ecd8041332382c20068fa44e103b759442217f62867",
                    "EndpointID": "4388a57b5eed4fb767d918eb274a8b631d1e8fa8793909e6373a9e2c5466c580",
                    "Gateway": "172.17.0.1",
                    "IPAddress": "172.17.0.2",
                    "IPPrefixLen": 16,
                    "IPv6Gateway": "",
                    "GlobalIPv6Address": "",
                    "GlobalIPv6PrefixLen": 0,
                    "MacAddress": "02:42:ac:11:00:02",
                    "DriverOpts": null
                }
            }

4.3 尝试连接

sixdog@sixiaodong dockerfile % docker network connect sixdog mysql-dev

4.4 查看网络信息

sixdog@sixiaodong dockerfile % docker inspect mysql-dev  
其他部分省略
"Gateway": "172.17.0.1",
            "GlobalIPv6Address": "",
            "GlobalIPv6PrefixLen": 0,
            "IPAddress": "172.17.0.2",
            "IPPrefixLen": 16,
            "IPv6Gateway": "",
            "MacAddress": "02:42:ac:11:00:02",
            "Networks": {
                "bridge": {
                    "IPAMConfig": null,
                    "Links": null,
                    "Aliases": null,
                    "NetworkID": "5451e1cd08f83eeba5495ecd8041332382c20068fa44e103b759442217f62867",
                    "EndpointID": "4388a57b5eed4fb767d918eb274a8b631d1e8fa8793909e6373a9e2c5466c580",
                    "Gateway": "172.17.0.1",
                    "IPAddress": "172.17.0.2",
                    "IPPrefixLen": 16,
                    "IPv6Gateway": "",
                    "GlobalIPv6Address": "",
                    "GlobalIPv6PrefixLen": 0,
                    "MacAddress": "02:42:ac:11:00:02",
                    "DriverOpts": null
                },
                "sixdog": {
                    "IPAMConfig": {},
                    "Links": null,
                    "Aliases": [
                        "465ae184cd56"
                    ],
                    "NetworkID": "1dadf784140fe418f4bd421b63ecd06ed46a1ebf41d386fba725e454349acb86",
                    "EndpointID": "1fc1d1d1dcaa6f37732690e82b684370638070a76c3d6a7ee08ebe94474711e8",
                    "Gateway": "172.18.0.1",
                    "IPAddress": "172.18.0.2",
                    "IPPrefixLen": 16,
                    "IPv6Gateway": "",
                    "GlobalIPv6Address": "",
                    "GlobalIPv6PrefixLen": 0,
                    "MacAddress": "02:42:ac:12:00:02",
                    "DriverOpts": {}
                }
            }

4.5 我用dbeaver使用172.18.0.2连接了下,连接不上mysql-dev,用我本机的ip可以

  1. 新建一个jar的运行环境,配置启动时的外部配置文件里的mysql连接ip改为172.18.0.2,并把这个新创建的容器加到sixdog网络里
    5.1 新建一个jar的运行环
#db访问配置
spring.datasource.url=jdbc:mysql://172.18.0.2:3307/test
spring.datasource.username=root
spring.datasource.password=123456
spring.datasource.driverClassName =com.mysql.cj.jdbc.Driver

server.port=8082
sixdog@sixiaodong dockerfile % docker run --name appsixdog --network sixdog -p 8082:8082 -d app:v1
74c9a0faaaff167802fd6532278e8e10fe0a1d3cb993c275fe32e064e130fb2d

5.2 查看容器

sixdog@sixiaodong dockerfile % docker ps -a
CONTAINER ID   IMAGE                       COMMAND                   CREATED         STATUS                      PORTS                               NAMES
74c9a0faaaff   app:v1                      "java -Dspring.confi…"   9 seconds ago   Up 8 seconds                0.0.0.0:8082->8082/tcp              appsixdog

5.3 查看网络配置

sixdog@sixiaodong dockerfile % docker inspect appsixdog
部分省略
"Networks": {
                "sixdog": {
                    "IPAMConfig": null,
                    "Links": null,
                    "Aliases": [
                        "74c9a0faaaff"
                    ],
                    "NetworkID": "1dadf784140fe418f4bd421b63ecd06ed46a1ebf41d386fba725e454349acb86",
                    "EndpointID": "9c64f3dc6bbe49a7ffbfd699b946e55b9ff8d006591d38c2e0fe4fe86602f565",
                    "Gateway": "172.18.0.1",
                    "IPAddress": "172.18.0.3",
                    "IPPrefixLen": 16,
                    "IPv6Gateway": "",
                    "GlobalIPv6Address": "",
                    "GlobalIPv6PrefixLen": 0,
                    "MacAddress": "02:42:ac:12:00:03",
                    "DriverOpts": null
                }
            }

5.4 访问页面
…很遗憾,可能是外部配置的文件没有生效所以8082的画面访问失败,我删掉了使用8081再来一遍
5.5.想起来了,需要把配置文件拷贝到容器内

sixdog@sixiaodong dockerfile % docker cp ./application.properties 00581903345c:/home/application.properties
                                             Successfully copied 2.56kB to 00581903345c:/home/application.properties

5.6 进容器内查看确认内容

docker exec -i -t appsixdog /bin/bash

5.7.重启容器

sixdog@sixiaodong dockerfile % docker restart appsixdog
appsixdog

5.8 再次确认画面,报错了,调查后发现其实在容器内品mysql的ip是可以的拼通的,最后意识到,在eclipse里配置的端口是创建容器时映射到外边的3307,而容器内是3306,修改后重启容器,画面可以打开,证明联网后容器可以互相访问,但是要注意端口等信息以容器内为准,而不是创建容器时外边的端口
(四)docker:为mysql和java jar运行环境创建同一网络,容器互联_第1张图片

你可能感兴趣的:(docker,java,docker,mysql)