高木のブログ

「Failed to initialize, db service is unhealthy.」の原因と解決方法

2020/09/23

GitHub ActionsでRSpecを動かしたくて公式ドキュメントっぽいものを読みながらやっていたがうまく動かなかった
actions/setup-ruby: Set up your GitHub Actions workflow with a specific version of Ruby

.github/workflows/rspec.yml
name: Rails Unit Tests

on: [push, pull_request]

jobs:
  build:

    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 5

    steps:
    - uses: actions/checkout@v2
    - name: Set up Ruby 2.6
      uses: actions/setup-ruby@v1
      with:
        ruby-version: 2.6
    - name: Build and test with Rake
      env:
        PGHOST: 127.0.0.1
        PGUSER: postgres
        RAILS_ENV: test
      run: |
        sudo apt-get -yqq install libpq-dev
        gem install bundler
        bundle install --jobs 4 --retry 3
        bundle exec rails db:create
        bundle exec rails db:migrate
        bundle exec rails test

問題

「Initialize containers」のところでエラーを吐く
Failed to initialize, db service is unhealthy.

  7e73bf550139: Pull complete
  a1041ec044d9: Pull complete
  32614d1e0a40: Pull complete
  0089f8c8d3af: Pull complete
  03a7e22b7f89: Pull complete
  Digest: sha256:2caa3663eaa2edf1413a0702bc545c417b9d21b3611c4cba8e7a6b2c195e88e4
  Status: Downloaded newer image for postgres:11
  docker.io/library/postgres:11
  /usr/bin/docker create --name a5369412b9c144faa1c5f123334a16a1_postgres11_0bb424 --label 9e3346 --network github_network_e4b39d40e9764687b8eaf86f022f0951 --network-alias db -p 5432:5432 --health-cmd pg_isready --health-interval 10s --health-timeout 5s --health-retries 5 -e "POSTGRES_USER=postgres" -e "POSTGRES_DB=masamune_test" -e GITHUB_ACTIONS=true -e CI=true postgres:11
  7672b2c9b0386cf8bb8cd675b92f5f6e1c95fab2c656e1727e41d0c7ea7f747e
  /usr/bin/docker start 7672b2c9b0386cf8bb8cd675b92f5f6e1c95fab2c656e1727e41d0c7ea7f747e
  7672b2c9b0386cf8bb8cd675b92f5f6e1c95fab2c656e1727e41d0c7ea7f747e
  /usr/bin/docker ps --all --filter id=7672b2c9b0386cf8bb8cd675b92f5f6e1c95fab2c656e1727e41d0c7ea7f747e --filter status=running --no-trunc --format "{{.ID}} {{.Status}}"
  7672b2c9b0386cf8bb8cd675b92f5f6e1c95fab2c656e1727e41d0c7ea7f747e Up Less than a second (health: starting)
  /usr/bin/docker port 7672b2c9b0386cf8bb8cd675b92f5f6e1c95fab2c656e1727e41d0c7ea7f747e
  /usr/bin/docker inspect --format="{{if .Config.Healthcheck}}{{print .State.Health.Status}}{{end}}" 7672b2c9b0386cf8bb8cd675b92f5f6e1c95fab2c656e1727e41d0c7ea7f747e
  unhealthy
  ##[error]Failed to initialize, db service is unhealthy.

原因

セキュリティ関連の変更でこの問題が起きているらしい

解決方法

修正済みのimageを使う

image: postgres:11@sha256:85d79cba2d4942dad7c99f84ec389a5b9cc84fb07a3dcd3aff0fb06948cdc03b

参考

ruby on rails - docker postgres image - Failed to initialize, db service is unhealthy - Stack Overflow


SNS でシェアする


ytkg

Written by ytkg, Twitter, GitHub