(4) Docker: Create the same network for mysql and java jar operating environments, and interconnect containers

  1. After reading a lot of information, one of the reasons for interconnection is that the IP address in the container is not fixed. If another container starts after shutting down and restarting, the previous IP will be occupied. Therefore, interconnection creates a network so that several containers can be on the same Network, you can connect to the Internet without being affected by the change of IP when shutting down and restarting. But in fact, when I tested mysql, whether it was in the code or the sql connection tool, I couldn’t connect to it using the IP in the container. I could only use my local machine. The IP connection can only be connected, so the actual change of the IP inside the container has no impact on my use, but I checked a lot on the Internet. They seem to be unable to connect using the local IP, and can only use the IP in the container. This I I don’t know why, but I just want to learn how to create a network and connect new containers and existing containers to a network. If you don’t understand something now, you can know why when you have the opportunity in the future. Ignore Secondly, the key is to learn to use it first.
  2. Create a network first
 docker network create -d bridge sixdog
  1. View the created network
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. I now have an existing container mysql-dev and am ready to connect it to the sixdog network.

4.1 View the mysql-dev container

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 View some network information of mysql-dev

sixdog@sixiaodong dockerfile % docker inspect mysql-dev
Omit other parts
"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 Try to connect

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

4.4 View network information

sixdog@sixiaodong dockerfile % docker inspect mysql-dev
Omit other parts
"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 I used dbeaver to connect using 172.18.0.2, but I couldn’t connect to mysql-dev. I could use my local IP.

  1. Create a new jar operating environment, change the mysql connection IP in the external configuration file at startup to 172.18.0.2, and add the newly created container to the sixdog network
    5.1 Create a new jar runtime environment
#db access configuration
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 View containers

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 View network configuration

sixdog@sixiaodong dockerfile % docker inspect appsixdog
Partially omitted
"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 Access pages
…Unfortunately, it may be that the external configuration file did not take effect, so the screen access of 8082 failed. I deleted it and tried again using 8081.
5.5. I remembered that I need to copy the configuration file into the container.

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

5.6 Enter the container to view the confirmation content

docker exec -i -t appsixdog /bin/bash

5.7. Restart the container

sixdog@sixiaodong dockerfile % docker restart appsixdog
appsixdog

5.8 Confirmed the screen again, and an error was reported. After investigation, it was found that the IP of mysql in the container could be solved. Finally, I realized that the port configured in eclipse was mapped to 3307 outside when the container was created, while the port inside the container was 3306, restart the container after modification, and the screen can be opened, proving that the containers can access each other after networking. However, please note that the port and other information are based on the inside of the container, not the external port when creating the container.