Get values .env and assign them to docker-compose.yml but database cannot be connected

I can get values named DATABASE_USERNAME and DATABASE_PASSWORD form .env file and define them in docker-compose.yml file.

Here is my .env shown below

DATABASE_USERNAME=value
DATABASE_PASSWORD=value

Here is the docker-compose.yml shown below

version: "3.9"

services:
  database:
    container_name: database
    image: mysql:8.0.33
    restart: always
    env_file:
      - .env  # Use the .env file for environment variables
    environment:
      MYSQL_DATABASE: bookdelivery
      MYSQL_PASSWORD: ${DATABASE_PASSWORD}
      MYSQL_ROOT_PASSWORD: ${DATABASE_PASSWORD}
      MYSQL_ROOT_HOST: '%'
      MYSQL_PORT: 3307
    volumes:
      - ./db:/var/lib/mysql
    ports:
      - "3307:3306"
    networks:
      - bookDeliveryNetwork

  bookdelivery:
    build:
      context: .
      dockerfile: Dockerfile
    container_name: bookdelivery
    restart: on-failure
    env_file:
      - .env  # Use the .env file for environment variables
    ports:
      - "1221:1221"
    environment:
      - server.port=1221
      - spring.datasource.username=${DATABASE_USERNAME}
      - spring.datasource.password=${DATABASE_PASSWORD}
      - spring.datasource.url=jdbc:mysql://host.docker.internal:3307/bookdelivery
    depends_on:
      - database
    networks:
      - bookDeliveryNetwork


networks:
  bookDeliveryNetwork:

Here is the docker compose config result

name: bookdelivery                                     
services:                                              
  bookdelivery:                                        
    build:                                             
      context: C:\Users\Noyan\IdeaProjects\bookdelivery
      dockerfile: Dockerfile                           
    container_name: bookdelivery                       
    depends_on:
      database:
        condition: service_started
    environment:
      DATABASE_PASSWORD: value
      DATABASE_USERNAME: value
      server.port: "1221"
      spring.datasource.password: value
      spring.datasource.url: jdbc:mysql://host.docker.internal:3307/bookdelivery
      spring.datasource.username: root
    networks:
      bookDeliveryNetwork: null
    ports:
    - mode: ingress
      target: 1221
      published: "1221"
      protocol: tcp
    restart: on-failure
  database:
    container_name: database
    environment:
      DATABASE_PASSWORD: value
      DATABASE_USERNAME: value
      MYSQL_DATABASE: bookdelivery
      MYSQL_PASSWORD: value
      MYSQL_PORT: "3307"
      MYSQL_ROOT_HOST: '%'
      MYSQL_ROOT_PASSWORD: value
    image: mysql:8.0.33
    networks:
      bookDeliveryNetwork: null
    ports:
    - mode: ingress
      target: 3306
      published: "3307"
      protocol: tcp
    restart: always
    volumes:
    - type: bind
      source: C:\Users\Username\IdeaProjects\bookdelivery\db
      target: /var/lib/mysql
      bind:
        create_host_path: true
networks:
  bookDeliveryNetwork:
    name: bookdelivery_bookDeliveryNetwork

When I run docker-compose up -d,

I got this issue shown below

Caused by: com.mysql.cj.exceptions.CJCommunicationsException: Communications link failure

Upvotes: 0

Views: 129

Answers (1)

2pha
2pha

Reputation: 10165

Why are you setting an environment file and setting environment variables in the compose file?

remove:

environment:
      MYSQL_PASSWORD: ${DATABASE_PASSWORD}
      MYSQL_ROOT_PASSWORD: ${DATABASE_PASSWORD}

How you have it above Substitutes from the shell, not from the .env file

Just put them all in the .env file or atleast the ones you want hidden.

Docs page for environment variables is here

Upvotes: 0

Related Questions