1

I have 2 containers - mysql database and java backend service.

services:
  backend:
    image: dockerhub/repo:backend-tag
    ports:
      - "3000:3000"
    environment:
      DB_URL: mysql://database/databasename
    depends_on:
      - database
  database:
    container_name: database
    image: dockerhub/repo:database-tag
    ports:
      - "3306:3306"
    volumes:
      - testvolume:/data/db
    command: --default-authentication-plugin=mysql_native_password
    restart: always
    environment:
      MYSQL_ROOT_PASSWORD: password
      MYSQL_USER: db_user
      MYSQL_DATABASE: databasename
      MYSQL_PASSWORD: password

volumes:
  testvolume:

Backend can connect with database container from local build:

jdbcUrl ="jdbc:mysql://127.0.0.1:3306/..."

But it doesn't work between containers:

jdbcUrl = "jdbc:mysql://database:3306/..."

backend Docker file:

FROM gradle:7-jdk11 AS build
COPY --chown=gradle:gradle . /home/gradle/src
WORKDIR /home/gradle/src
RUN gradle shadowJar --no-daemon

FROM openjdk:11
EXPOSE 8080:8080
RUN mkdir /app
COPY --from=build /home/gradle/src/build/libs/*-all.jar /app/app.jar
ENTRYPOINT ["java","-jar","/app/app.jar"]
Dmitry
  • 130
  • 6
  • can you precisely define "doesn't work"? – Zeitounator Aug 09 '22 at 17:42
  • It sounds really weird. I have (almost - python/Flask instead of java) same setup and it is working (and [example in docs](https://docs.docker.com/compose/networking/) proposes similar code). Could you try to restart docker service first? Is it your full config file (I mean, isn't there any network configuration, for example)? Any clues in logs? Also, what is a base image of backend (which linux derivative)? What does `cat /etc/resolv.conf` and `cat /etc/hosts` in `backend` container output? – STerliakov Aug 09 '22 at 17:59
  • "doesn't work" means: `at com.mysql.cj.jdbc.exceptions.SQLError.createCommunicationsException(SQLError.java:174)` – Dmitry Aug 09 '22 at 18:06
  • @SUTerliakov, I restarted local PC couple times (windows) and also trying on VPS (Ubuntu 20). Yes, this docker-compose file is full, nothing extra. I'll try resolv and conf later, I need to change code, now container falls if there is no connection to DB. I'll add backend Docker file to the question now. – Dmitry Aug 09 '22 at 18:18
  • 1
    Add this restart: on-failure under backend – Shamith Wimukthi Aug 09 '22 at 18:20
  • 1
    And does your backend try to connect right after it is launched? `depends-on` does not wait for mysql to be ready - only to be up. If you connect before it is ready, you receive a failure, and then your backend dies. As a quick fix, `restart: on-failure` should help, but then it is better to wait explicitly (`mysqladmin ping` is the most reliable, but requires additional deps; `until nc -zvw30 "$DB_HOST" "$DB_PORT"; do sleep 1; done` uses smaller dependency, but is less reliable (adjust variable names for your case) – STerliakov Aug 09 '22 at 18:50
  • @SUTerliakov yes, that was the problem. I fixed it with `restart: on-failure`. I was relying on `depends_on`. Seems like I can add couple attempts on a init connection code without adding any new dependency. You can create an answer, it can be useful, since this problem is not explained in any articles and videos I watched. – Dmitry Aug 09 '22 at 19:14

0 Answers0