docker network create -d bridge sixdog
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
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可以
#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,修改后重启容器,画面可以打开,证明联网后容器可以互相访问,但是要注意端口等信息以容器内为准,而不是创建容器时外边的端口