英文:
Trying to build a docker-compose file from two docker run commands for two services that is not working
问题
我有两个镜像,mysqldb
和 springboot-service
,它们应该一起运行。当我按顺序运行两个 CLI 命令时,它们之间的交互可以正常工作。
docker run --name mysqldb --network springboot-mysql-net --hostname mysqldb -e MYSQL_ROOT_PASSWORD=1234 -e MYSQL_USER=a -e MYSQL_PASSWORD=a -e MYSQL_DATABASE=studentdb -p 3306:3306 -d mysql:latest
docker run --network springboot-mysql-net --name springboot-container -p 8080:8080 -e dbname=mysqldb -d springboot-studentservice
我已经创建了一个 docker-compose
文件,具体代码如下,但是当我运行 docker-compose up
时,Springboot 应用程序无法连接到数据库。它抛出的错误是
2023-03-07 09:52:49 Caused by: com.mysql.cj.exceptions.CJCommunicationsException: Communications link failure
docker-compose
services:
mysqldb:
image: mysql
restart: always
environment:
MYSQL_ROOT_PASSWORD: 1234
MYSQL_USER: a
MYSQL_PASSWORD: a
MYSQL_DATABASE: studentdb
hostname: mysqldb
ports:
- "3306:3306"
networks:
- springboot-mysql-net
springboot-container:
image: springboot-studentservice
environment:
dbname: mysqldb
ports:
- "8080:8080"
depends_on:
- mysqldb
networks:
- springboot-mysql-net
networks:
springboot-mysql-net:
driver: bridge
对于为什么会发生这种情况,我略感困惑!将不胜感激任何帮助。
英文:
I have two images mysqldb
and a springboot-service
that are meant to be runned together. I have gotten the interaction between both to work when running the two CLI commands sequentially.
docker run --name mysqldb --network springboot-mysql-net --hostname mysqldb -e MYSQL_ROOT_PASSWORD=1234 -e MYSQL_USER=a -e MYSQL_PASSWORD=a -e MYSQL_DATABASE=studentdb -p 3306:3306 -d mysql:latest
docker run --network springboot-mysql-net --name springboot-container -p 8080:8080 -e dbname=mysqldb -d springboot-studentservice
I have created a docker-compose
file with the following code, but when I run docker-compose up
, the Springboot application is unable to connection to the db. The error it throws is
2023-03-07 09:52:49 Caused by: com.mysql.cj.exceptions.CJCommunicationsException: Communications link failure
docker-compose
services:
mysqldb:
image: mysql
restart: always
environment:
MYSQL_ROOT_PASSWORD: 1234
MYSQL_USER: a
MYSQL_PASSWORD: a
MYSQL_DATABASE: studentdb
hostname: mysqldb
ports:
- "3306:3306"
networks:
- springboot-mysql-net
springboot-container:
image: springboot-studentservice
environment:
dbname: mysqldb
ports:
- "8080:8080"
depends_on:
- mysqldb
networks:
- springboot-mysql-net
networks:
springboot-mysql-net:
driver: bridge
Am slightly confused as in to why this happens! Help will be appreciated.
答案1
得分: 1
你可以尝试删除 mysqldb 端口。
services:
mysqldb:
image: mysql
restart: always
environment:
MYSQL_ROOT_PASSWORD: 1234
MYSQL_USER: a
MYSQL_PASSWORD: a
MYSQL_DATABASE: studentdb
hostname: mysqldb
networks:
- springboot-mysql-net
springboot-container:
image: springboot-studentservice
environment:
dbname: mysqldb
ports:
- "8080:8080"
depends_on:
- mysqldb
networks:
- springboot-mysql-net
networks:
springboot-mysql-net:
driver: bridge
并且你创建了 'studentdb' 数据库吗?如果没有,你需要为 mysqldb 设置一个卷并创建它。
英文:
You could try to remove mysqldb ports.
services:
mysqldb:
image: mysql
restart: always
environment:
MYSQL_ROOT_PASSWORD: 1234
MYSQL_USER: a
MYSQL_PASSWORD: a
MYSQL_DATABASE: studentdb
hostname: mysqldb
networks:
- springboot-mysql-net
springboot-container:
image: springboot-studentservice
environment:
dbname: mysqldb
ports:
- "8080:8080"
depends_on:
- mysqldb
networks:
- springboot-mysql-net
networks:
springboot-mysql-net:
driver: bridge
And do you create 'studentdb' database? If not you have to set a volum for mysqldb and create it.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论