Hello everyone. I have this docker db instance scr...
# server
d
Hello everyone. I have this docker db instance script :
Copy code
version: '3.7'
services:

  #MySQL Service
  db:
    image: mysql:5.7.22
    container_name: ${DOCKER_CONTAINER_NAME_SLUG}_db
    restart: unless-stopped
    tty: true
    ports:
      - ${DOCKER_MYSQL_PORTS}
    environment:
      MYSQL_ROOT_PASSWORD: ${DB_PASSWORD}
      MYSQL_DATABASE: ${DB_DATABASE}
      MYSQL_USER: ${DB_USERNAME}
      MYSQL_PASSWORD: ${DB_PASSWORD}
      SERVICE_TAGS: dev
      SERVICE_NAME: mysql
    volumes:
      - dbdata:/var/lib/mysql/
      - ./mysql/my.cnf:/etc/mysql/my.cnf
    networks:
      - my-network

#Docker Networks
networks:
  my-network:
    driver: bridge

#Volumes
volumes:
  dbdata:
    driver: local
then in a different spring boot project, I have this docker-compose file:
Copy code
version: '3'

services:
  app:
    container_name: api-app
    build:
      context: .
      dockerfile: Dockerfile
    ports:
      - "8080:8080"

networks:
  default:
    external:
      name: my-network
it fails to build because I get this error:
Copy code
java.sql.SQLNonTransientConnectionException: Could not create connection to database server. Attempted reconnect 3 times. Giving up.
Anyone see the problem ?
t
what happens if you put them in the same docker file? (at least good for debugging, then you know it is not network related) and is the mysql url and port correct in your spring boot configuration?
m
Don't expose the db port to the host. Use
expose
clause:
Copy code
expose:
  - 1234
Also, use container name as a host in your connection string. A docker-compose.yml file from one of my projects might help you
Copy code
version: '3'

volumes:
  db-data-postgres:
    driver: local

  db-data-postgres-config:
    driver: local

services:
  linda-postgres:
    image: postgres:15beta2-bullseye
    container_name: linda-postgres
    environment:
      POSTGRES_DB: "linda_data"
      POSTGRES_USER: "linda_data"
      POSTGRES_PASSWORD: "linda_data"
      PGDATA: /var/lib/postgresql/data/pgdata
    restart: always
    expose:
      - 5432
    networks:
      - linda-net
    volumes:
      - db-data-postgres:/var/lib/postgresql/data
      - db-data-postgres-config:/var/lib/postgresql/dataConfig

  linda:
    image: linda:0.8.0-beta.0
    container_name: linda
    networks:
      - linda-net
    ports:
      - "9292:9292"
    depends_on:
      - linda-postgres
    environment:
      SPRING_DATASOURCE_URL: jdbc:<postgresql://linda-postgres:5432/linda_data>
    restart: on-failure

networks:
  linda-net:
    driver: bridge
And why do you use
Dockerfile
??? You simply do
./gradlew bootBuildImage
d
Thanks guys, I’ll try all that after dinner. Btw, my Dockerfile is this:
Copy code
FROM gradle:7.5.0-jdk17-alpine as builder
USER root
WORKDIR /builder
ADD . /builder
RUN gradle build --info

FROM openjdk:17-alpine
WORKDIR /app
EXPOSE 8080
COPY --from=builder /builder/build/libs/demo-0.0.1-SNAPSHOT.jar .
CMD ["java", "-jar", "demo-0.0.1-SNAPSHOT.jar"]
@Ties I know the docker db image works because I can run it and connect my spring boot (non dockerized app to it). It’s only the dockerized spring boot app that I can’t connect to the the db.
@Mikhail I’ve changed my docker db image to work with expose (instead of ports):
Copy code
version: '3.7'
services:

  #MySQL Service
  db:
    image: mysql:5.7.22
    container_name: ${DOCKER_CONTAINER_NAME_SLUG}_db
    restart: unless-stopped
    tty: true
    expose:
      - "3306"
    environment:
      MYSQL_ROOT_PASSWORD: ${DB_PASSWORD}
      MYSQL_DATABASE: ${DB_DATABASE}
      MYSQL_USER: ${DB_USERNAME}
      MYSQL_PASSWORD: ${DB_PASSWORD}
      SERVICE_TAGS: dev
      SERVICE_NAME: mysql
    volumes:
      - dbdata:/var/lib/mysql/
      - ./mysql/my.cnf:/etc/mysql/my.cnf
    networks:
      - my-network

#Docker Networks
networks:
  my-network:
    driver: bridge

#Volumes
volumes:
  dbdata:
    driver: local
I’ve also added the
SPRING_DATASOURCE_URL: jdbc:<mysql://localhost:3306/kotlin_demo_app>
config to my spring boot docker-compose:
Copy code
version: '3'

services:
  app:
    container_name: api-app
    build:
      context: .
      dockerfile: Dockerfile
    ports:
      - "8080:8080"
    depends_on:
      - db
    environment:
      SPRING_DATASOURCE_URL: jdbc:<mysql://localhost:3306/kotlin_demo_app>
    restart: on-failure
    networks:
      - my-network

#Docker Networks
networks:
  my-network:
    driver: bridge
But I still get the same error
Could not create connection to database server. Attempted reconnect 3 times
t
And did you try to put them in the same docker compose file? (Just for debugging purposes)
m
Aa I said, try using the name of your db container instead of
localhost
d
@Mikhail how do you have it in your application.properties. ? localhost or linda-postgres ?
m
In my
application.yml
file I use
localhost
. But when running with
docker-compose
I override the host by defining
SPRING_DATASOURCE_URL
env var in my
docker-compose.yml
file
👍 1
d
ok I tried everything and I even gathered the two scripts into one:
Copy code
version: '3'

volumes:
  dbdata:
    driver: local

services:
  docker_db:
    image: mysql:5.7.22
    container_name: docker_db
    restart: unless-stopped
    tty: true
    expose:
      - "3306"
    environment:
      MYSQL_ROOT_PASSWORD: root
      MYSQL_DATABASE: kotlin_demo_app
      MYSQL_USER: root
      MYSQL_PASSWORD: root
      SERVICE_TAGS: dev
      SERVICE_NAME: mysql
    volumes:
      - dbdata:/var/lib/mysql/
      - ./mysql/my.cnf:/etc/mysql/my.cnf
    networks:
      - docker_db-net

  app:
    container_name: api-app
    build:
      context: .
      dockerfile: Dockerfile
    ports:
      - "8080:8080"
    environment:
      SPRING_DATASOURCE_URL: jdbc:<mysql://docker_db:3306/kotlin_demo_app?autoReconnect=true&useUnicode=true&characterEncoding=UTF-8&allowMultiQueries=true&useSSL=false>
    depends_on:
      - docker_db-net
    networks:
      - docker_db-net
    restart: on-failure

networks:
  docker_db-net:
    driver: bridge
but I still get the same error
Could not create connection to database server
. Btw, you can view the entire project here : https://github.com/danygiguere/springboot-kotlin-example