简体   繁体   English

如何在docker撰写中将spring app传递给mysql?

[英]How to communicate spring app to mysql in docker compose?

Although I've rudimentary managed Spring boot docker service to initiate after mysql docker service, there is still a communications link failure from the app. 虽然我已经在mysql docker服务之后启动了初步管理的Spring启动docker服务,但应用程序仍然存在通信链接故障。 I don't know what else to do, I've tried too many things now. 我不知道该怎么办,现在尝试了太多事情。

My docker-compose: 我的码头组成:

version: '3'
services:
  mysql:
    container_name: hernidb
    image: mysql
    volumes:
     - "./waitForMySQL.sh:/usr/local/bin/waitForMySQL.sh"
    ports:
      - '3306:3306'
    environment:
      USER: 'root'
      MYSQL_ROOT_PASSWORD: 'rida'
    restart: always
  app:
    container_name: herniemp
    restart: always
    build: ./EmployeeService
    working_dir: /app
    volumes:
      - ./EmployeeService:/app
      - ~/.m2:/root/.m2
    ports:
      - '8080:8080'
    command: bash -c "sleep 120 && mvn clean spring-boot:run -Dspring-boot.run.profiles=dev"
    depends_on:
      - mysql

I know the sleep 120 is shameful but with scripts put inside the volume property it gives me a permission denied message. 我知道sleep 120是可耻的但是将脚本放在volume属性中它会给我一个权限被拒绝的消息。 Also the link option is deprecated. 同时不建议使用link选项。

You need to keep these two containers in the same network. 您需要将这两个容器保持在同一网络中。 Consider following docker file for configuring in the same network: 考虑在同一网络中进行配置的以下docker文件:

version: '3'
services:
  mysql:
    container_name: hernidb
    image: mysql
    volumes:
     - "./waitForMySQL.sh:/usr/local/bin/waitForMySQL.sh"
    ports:
      - '3306:3306'
    environment:
      MYSQL_ROOT_PASSWORD:rida
    networks:
      movie-quote-network:
        aliases:
          - herni-database
    restart: always
  app:
    container_name: herniemp
    restart: always
    build: ./EmployeeService
    working_dir: /app
    networks:
      movie-quote-network:
        aliases:
          - herni-service
    volumes:
      - ./EmployeeService:/app
      - ~/.m2:/root/.m2
    ports:
      - '8080:8080'
    command: bash -c "sleep 120 && mvn clean spring-boot:run -Dspring-boot.run.profiles=dev"
    depends_on:
      - mysql

networks:
  herni-network:

As you can see, we've defined a network called herni-network at the bottom, with an empty body. 如您所见,我们在底部定义了一个名为herni-network的网络,其中有一个空的主体。 I also added a networks section to each container configuration and providing an alias for that specific container. 我还为每个容器配置添加了网络部分,并为该特定容器提供了别名。

Both containers will be able to communicate through each other by using the given alias as the hostname. 通过使用给定的别名作为主机名,这两个容器将能够相互通信。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM