简体   繁体   中英

Docker: Springboot container cannot connect to PostgreSql Container

I am building a Spring Boot application which uses PostgreSQL with docker-compose. When I run my containers using docker-compose up --build , my Spring Boot application fails to start because it does not find the PostgreSQL container's hostname.

Spring Boot Dockerfile

FROM maven:3.6.3-openjdk-14-slim AS build  
COPY src /usr/src/app/src  
COPY pom.xml /usr/src/app  
RUN mvn -f /usr/src/app/pom.xml clean package

FROM openjdk:14-slim
COPY --from=build /usr/src/app/target/server-0.0.1-SNAPSHOT.jar /usr/app/server-0.0.1-SNAPSHOT.jar
EXPOSE 9000  
ENTRYPOINT ["java","-jar","/usr/app/server-0.0.1-SNAPSHOT.jar"]

docker-compose.yml

version: '3'

services:
  db:
    image: postgres
    environment:
      POSTGRES_USER: postgres
      POSTGRES_PASSWORD: postgres
      POSTGRES_DB: my_db
    ports:
      - "5432:5432"
    volumes:
      - db-data:/var/lib/postgresql/data
    networks:
      - db-network
    restart: always

  server:
    build: './server'
    depends_on:
      - db
    restart: always
    ports:
      - "9000:9000"
    networks:
      - db-network
    volumes:
      - ./server:/server


networks:
  db-network:

volumes:
  db-data:

application.properties

spring.datasource.url=jdbc:postgresql://db:5432/my_db
spring.datasource.username=postgres
spring.datasource.password=postgres

Error output

Caused by: java.net.UnknownHostException: db

My guess is that docker-compose's virtual.network isn't created yet during the build stage of the Spring Boot Dockerfile. Any idea on how to solve this issue?

Lots of info here: https://docs.docker.com/compose.networking/

Within the web container, your connection string to db would look like postgres://db:5432, and from the host machine, the connection string would look like postgres://{DOCKER_IP}:8001.

What this is saying is db:5432 is fine to use within docker-compose.yaml and the IP address will be passed (not "db"), but using it externally within your application code isn't going to work. You could however pass from docker-compose.yaml db as an application input variable, which your application could fill in in the configuration file. This would enable you then to connect.

Externalising configuration like this is fairly common practice so should be a relatively easy fix.

eg:

Docker-compose.yaml

version: '3'

services:
  db:
    container_name: db
    image: postgres
    environment:
      POSTGRES_USER: postgres
      POSTGRES_PASSWORD: postgres
      POSTGRES_DB: my_db
    ports:
      - "5432:5432"
    volumes:
      - db-data:/var/lib/postgresql/data
    networks:
      - db-network
    restart: always

  server:
    build: './server'
    depends_on:
      - db
    environment:
      DB_HOST: db # untested, but there should be a way to pass this in
      DB_PORT: 5432
      DB_DATABASE: my_db
      DB_USER: postgres
      DB_PASSWORD: postgres
    restart: always
    ports:
      - "9000:9000"
    networks:
      - db-network
    volumes:
      - ./server:/server


networks:
  db-network:

volumes:
  db-data:

Then have an application.properties file located under src/main/java/resources/application.properties

spring.datasource.url=jdbc:postgresql://${DB_HOST}:${DB_PORT}/${DB_DATABASE}
spring.datasource.username=${DB_USERNAME}
spring.datasource.password=${DB_PASSWORD}

This post completely solved my issue. It turns out that maven was trying to establish the connection to the database while building the.jar file. All I had to do is modify my Dockerfile with this line RUN mvn -f /usr/src/app/pom.xml clean package -DskipTests .

Please do note while building the images the service will not have access to the database as it is not yet running. Only after the images are built and the containers are running do the services have access. So when you try to pass a host as db, it is not yet available in the build stage. It is available only once the db container starts running.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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