2
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

Docker PostgreSQLコンテナが起動しない

Posted at

概要

  • 今までMySQLのDBコンテナを使っていたが、訳あってPostgreSQLのコンテナに返る事になった。
  • DBコンテナがエラーで起動せずちょっと詰まったので簡単にまとめておく。

問題までの経緯

  • docker-composer.ymlのdbサービスで下記のように記載した。

    docker-composer.yml
    db:
      build:
        context: ./.hoge/db
        dockerfile: Dockerfile
      ports:
        - 3542:5432
      environment:
        POSTGRES_DATABASE: hoge_app
        POSTGRES_USER: hoge_app
        POSTGRES_PASSWORD: xxxxxxxx
        TZ: "Asia/Tokyo"
      volumes:
        - db-volume:/var/lib/postgresql/data:cached
    
  • docker-composer up実行しコンテナの起動を試みた。

問題発生

  • 下記のエラーがでてdbサービスのコンテナが起動しない。

    YYYY-MM-DD HH:MM:SS initdb: error: directory "/var/lib/postgresql/data" exists but is not empty
    YYYY-MM-DD HH:MM:SS initdb: hint: If you want to create a new database system, either remove or empty the directory "/var/lib/postgresql/data" or run initdb with an argument other than "/var/lib/postgresql/data".
    

解決までの経緯

  • ちょっと調べたところどうやらDBのデータ永続化ボリュームにすでにデータがあるためdbサービスを初期化できない旨のエラーっぽい。

  • ホストPCで下記コマンドを実行してボリューム一覧を確認する。

    docker volume ls
    
  • 下記のように出力された。(testって付いてる方はテストコード用のdbのボリューム)

    DRIVER VOLUME NAME
    local hoge-app_db-volume
    local hoge-app_test-db-volume
    
  • どうやらhoge-app_db-volumeの方のボリュームが存在し、空じゃないから初期化できずエラーになるっぽい。

  • すでにDBで保存している情報は失われるが、下記コマンドを実行して当該のボリュームを削除した。

    docker volume rm hoge-app_db-volume
    
  • 再びdocker-composer upをすると問題なくコンテナが起動した。

2
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?