0

I have a problem when i try to run my api container that should connect to the db container.

The problem should be appearing when building the project or creating the image, because if i just run the project it successfully connect every time to the container.

Here i have my Dockerfile

FROM openjdk:11
RUN mkdir -p /app/
ADD build/libs/core-0.0.1-SNAPSHOT.jar /app/core-0.0.1-SNAPSHOT.jar
ENTRYPOINT ["java", "-jar", "/app/core-0.0.1-SNAPSHOT.jar"]

Also here the docker-compose.yml file

version: '3.8'

services:
  book-catalog-db:
    container_name: mysqlbc
    image: mysql:latest
    volumes:
      - ./proyecto/sql-data/db:/var/lib/mysql
    ports:
      - 3306:3306
    hostname: localhost
    environment:
      MYSQL_USER: 'admin'
      MYSQL_PASSWORD: 'admin'
      MYSQL_ROOT_PASSWORD: 'root'
      MYSQL_ROOT_PASSWORD: 'root'
  book-catalog-angular:
    container_name: angularbc
    image: book-catalog-client:latest
    volumes:
     - ./proyecto/web-assets:/usr/share/nginx/html/assets
    ports:
     - 4200:80
  book-catalog-api:
    container_name: apibc
    image: coreapi:latest
    ports:
     - 8080:8080

I hope i get an answer fast since i dont have much time. Good day to everyone!

i tried some things of this answer

Ruben
  • 1
  • 1
  • What did you use as the hostname to the database server? You need to use the hostname defined in the docker-compose file (`mysqlbc`) to connect between containers, and make sure the containers are on the same network, and that the Java application depends on the database. – Mark Rotteveel Dec 11 '22 at 10:27
  • It could also be that the hostname is `book-catalog-db`, I always forget if docker-compose uses the container-name or not. – Mark Rotteveel Dec 11 '22 at 10:33
  • maybe it would work with container-name as hostname, but it doesnt work with my defined hostname 'localhost' – Ruben Dec 13 '22 at 11:52
  • i just saw that i have to use service name as hostname, you were right when you said hostname may be 'book-catalog-db' – Ruben Dec 13 '22 at 11:55

1 Answers1

-1

ok, so i used

docker inspect -f '{{range.NetworkSettings.Networks}}{{.IPAddress}}{{end}}' mysqlbc

i got ip address from mysql container and i changed ip address on project built image, NOW IT WORKS

Ruben
  • 1
  • 1