尝试从两个未能正常工作的Docker运行命令构建一个Docker Compose文件。

huangapple go评论67阅读模式
英文:

Trying to build a docker-compose file from two docker run commands for two services that is not working

问题

我有两个镜像,mysqldbspringboot-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.

huangapple
  • 本文由 发表于 2023年3月7日 10:02:38
  • 转载请务必保留本文链接:https://go.coder-hub.com/75657430.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定