1
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?

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

Posted at

背景

つい先日、Dockerを使ってRails+PostgreSQLの勉強をしようとしていた時の話です。以下のようなDockerfileとdocker-compose.ymlを作成しました。

FROM ruby:2.5
RUN apt-get update && apt-get install -y \
    build-essential\
    libpq-dev\
    nodejs\
    postgresql-client\
    yarn
WORKDIR /rails-dir
COPY Gemfile Gemfile.lock /rails-dir/
RUN bundle install
docker-compose.yml
version: '3'

volumes:
  db-data:

services:
  web:
    build: .
    ports:
      - '3000:3000'
    volumes:
      - '.:/rails-dir'
    environment:
      - 'DATABASE_PASSWORD=postgres'
    tty: true
    stdin_open: true
    depends_on:
      - db
    links:
      - db

  db:
    image: postgres:15
    volumes:
      - 'db-data:/var/lib/postgresql/data'
    environment:
      - 'POSTGRES_USER=postgres'
      - 'POSTGRES_PASSWORD=postgres'

さあ、コンテナを起動するぞ。

docker-compose up -d
         Name                       Command              State            Ports         
----------------------------------------------------------------------------------------
rails-dir_db_1    docker-entrypoint.sh postgres   Exit 1                         
rails-dir_web_1   irb                             Up       0.0.0.0:3000->3000/tcp

DBコンテナが起動していない!!!

原因

とりあえず、ログを確認してみましょう。

docker-compose logs
Attaching to rails-dir_web_1, rails-dir_db_1
db_1   | popen failure: Cannot allocate memory
db_1   | initdb: error: program "postgres" is needed by initdb but was not found in the same directory as "/usr/lib/postgresql/15/bin/initdb"

解決策

解決策
解決策に記載されている通り、PostgreSQLのDockerイメージを15からbullseyeイメージに切り替えてみたら良いらしい。

docker-compose.yml

 image: postgres:15-bullseye
 

実行

$docker-compose up --build -d
$docker-compose ps

         Name                       Command              State           Ports         
---------------------------------------------------------------------------------------
rails-dir_db_1    docker-entrypoint.sh postgres   Up      5432/tcp              
rails-dir_web_1   irb                             Up      0.0.0.0:3000->3000/tcp

無事DBコンテナが起動できました。

1
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
1
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?