Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

docker postgres image - Failed to initialize, db service is unhealthy

I am writing a Github action where I want to use postgres:11 image as a service. But everytime, it tries to start I get error Failed to initialize, db service is unhealthy.

Github action looks like:

name: CI

on: [push]

jobs:
  unit_tests:
    name: 'Run unit tests'
    runs-on: ubuntu-latest
    services:
      db:
        image: postgres:11
        ports: ['5432:5432']
        options: >-
          --health-cmd pg_isready
          --health-interval 10s
          --health-timeout 5s
          --health-retries 15
    steps:
      - uses: actions/checkout@v2
      - name: Set up Ruby 2.6
        uses: actions/setup-ruby@v1
        with:
          ruby-version: 2.6.x
      - name: Build and run tests
        if: success()
        working-directory: backend/ruby
        env:
          DATABASE_URL: postgres://postgres:@localhost:5432/surveymaster_test
          RAILS_ENV: test
        run: |
          gem install bundler
          bundle install --jobs 4 --retry 3
          bundle exec rspec ./spec

The error is:

1de39762bafa: Pull complete
e72de942a5bc: Pull complete
5ed4b3beed09: Pull complete
bc4fa92d5de6: Pull complete
881ba21508a4: Pull complete
a1f3f1480763: Pull complete
0a652322d262: Pull complete
a4c4345d4a61: Pull complete
84378331cde0: Pull complete
e4b51862ee73: Pull complete
ef2d3c443e8e: Pull complete
Digest: sha256:acac53623c6e6f748e2a8a47064432095ddf8a2d04e83b49e39f3d0b2194fad7
Status: Downloaded newer image for postgres:11
docker.io/library/postgres:11
/usr/bin/docker create --name e9bbb0d5ce654fa7b92a57d3f5109fc4_postgres11_fe5d44 --label e87b52 --network github_network_397e7f044e964fdcbe2f21b74d50f6bc --network-alias db -p 5432:5432 --health-cmd pg_isready --health-interval 10s --health-timeout 5s --health-retries 5 -e GITHUB_ACTIONS=true postgres:11
5a8978184b3d44077563fa1f432270bbd51e8a89edb279e089dfc0c1593a1f2e
/usr/bin/docker start 5a8978184b3d44077563fa1f432270bbd51e8a89edb279e089dfc0c1593a1f2e
5a8978184b3d44077563fa1f432270bbd51e8a89edb279e089dfc0c1593a1f2e
/usr/bin/docker ps --all --filter id=5a8978184b3d44077563fa1f432270bbd51e8a89edb279e089dfc0c1593a1f2e --filter status=running --no-trunc --format "{{.ID}} {{.Status}}"
5a8978184b3d44077563fa1f432270bbd51e8a89edb279e089dfc0c1593a1f2e Up Less than a second (health: starting)
/usr/bin/docker port 5a8978184b3d44077563fa1f432270bbd51e8a89edb279e089dfc0c1593a1f2e
/usr/bin/docker inspect --format="{{if .Config.Healthcheck}}{{print .State.Health.Status}}{{end}}" 5a8978184b3d44077563fa1f432270bbd51e8a89edb279e089dfc0c1593a1f2e
unhealthy
##[error]Failed to initialize, db service is unhealthy.
like image 757
Arup Rakshit Avatar asked Mar 10 '20 12:03

Arup Rakshit


2 Answers

Postgres container requires that postgres user be setup with password in order to start it due to security reasons. we can add the password to the PostgreSQL like following:

env:
  POSTGRES_PASSWORD: postgres

Now the container will be up. We can read more about it here Creating PostgreSQL service containers .

like image 90
Arup Rakshit Avatar answered Sep 20 '22 15:09

Arup Rakshit


changing the docker version to:

postgres:11@sha256:85d79cba2d4942dad7c99f84ec389a5b9cc84fb07a3dcd3aff0fb06948cdc03b

solved it for me apparently with latest 11 version due to some security related changes we get this issue.

like image 29
Anjan Avatar answered Sep 19 '22 15:09

Anjan