2

I have one postgres container and one spring boot container. I am running both of them using docker-compose.

Below is the docker-compose.yml file:

version: '3'
services:
  backend:
    build:
      context: .
      dockerfile: Dockerfile.dev
    ports:
      - "8080:8080"
    depends_on:
      - postgres-db
    restart: always
    environment:
      SPRING_DATASOURCE_URL: jdbc:postgresql://postgres-db:5432/testDb
      SPRING_DATASOURCE_USERNAME: root
      SPRING_DATASOURCE_PASSWORD: root
  postgres-db:
    working_dir: /home/models
    image: postgres:11.3-alpine
    restart: always
    environment:
      - POSTGRES_USER=root
      - POSTGRES_PASSWORD=root
      - POSTGRES_DB=testDb
    ports:
      - "5432:5432"
    volumes:
      - ./pgdata:/var/lib/postgresql/data
      - ./init.sql:/docker-entrypoint-initdb.d/init.sql

Here's the Dockerfile.dev

FROM maven:3-jdk-8

ENV HOME=/home/usr/app

RUN mkdir -p $HOME

WORKDIR $HOME

ADD pom.xml $HOME

RUN ["/usr/local/bin/mvn-entrypoint.sh", "mvn", "dependency:go-offline"]

ADD . $HOME

RUN ["mvn", "package"]

CMD ["java","-jar","/usr/app/testApp-1.0.0-SNAPSHOT.jar"]

and here's the application.properties file

spring.datasource.url=jdbc:postgresql://postgres-db:5432/testDb
spring.datasource.username=root
spring.datasource.password=root
spring.jpa.hibernate.ddl-auto=validate

but when I run the containers using docker-container up I am getting the error saying the connection to database failed and the container stops.

Edit: I have added the following db-check.sh file for checking the status of db

#!/bin/sh
# wait-for-postgres.sh

set -e

host="$1"
shift
cmd="$@"

until PGPASSWORD="password" psql -h "$host" -U "root" -c '\q'; do
  >&2 echo "Postgres is unavailable - sleeping"
  sleep 1
done


>&2 echo "Postgres is up - executing command"
exec $cmd

and the new Dockerfile.dev looks like:

FROM maven:3-jdk-8

ENV HOME=/home/usr/app

RUN mkdir -p $HOME

WORKDIR $HOME

ADD db-check.sh $HOME

RUN chmod +x db-check.sh

RUN ["./db-check.sh", "postgres-db:5432", "/usr/local/bin/mvn-entrypoint.sh", "mvn", "dependency:go-offline"]

ADD . $HOME

RUN ["mvn", "package"]

CMD ["java","-jar","/usr/app/testApp-1.0.0-SNAPSHOT.jar"]

but for this script to run successfully I need psql installed in the backend container. Is there any other way of checking this because if feel that I am putting a lot of things in this single backend container for the sake of just checking the status of postgres container.

5
  • Looks like your java container is started before postgres is fully launched. Have a look at Control startup and shutdown order in Compose Commented Oct 29, 2019 at 15:46
  • So depends_on parameter won't work alone on it's own and I need to add a supporter script @michalk? Commented Oct 29, 2019 at 15:49
  • depends_on only makes sure that container is started before the others - it does not necesairly mean it is ready to accept connections. Commented Oct 29, 2019 at 15:50
  • The documentation link cleared my doubts. Thanks! Commented Oct 29, 2019 at 15:52
  • @michalk I have edited the question with some more doubts. Can you help me with this? Commented Oct 29, 2019 at 18:04

1 Answer 1

1

As others stated, probably the postgres-db container is not ready yet, because it has to initialzed at first run.

As a cheap workaround you can do this:

  1. Start postgres-db container only: $ docker-compose up -d postgres-db
  2. Wait a moment (let's say 10-20 sec)
  3. $ docker-compose up -d --build

Hope this helps.

Sign up to request clarification or add additional context in comments.

1 Comment

Yeah this solution works and I was using this earlier but I want a more automated way which will also help in the deployment phase.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.