Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why won't my Docker PostgreSQL container run anymore?

reEver since I updated Docker Desktop for Windows from 2.1.0.5 to Version 2.2.0.3 I've had a problem with the db container for my app. This all worked fine back on 2.1.0.5 before, but now it won't work even after I removed 2.2.0.3 and reverted back to 2.1.0.5. Really wish I hadn't updated now.

The error I get is:

could not translate host name "db" to address: Name or service not known

When I run docker ps, I can see that the postgres container isn't even running.

Some things I have tried are:

  1. Adding db to my Windows 10 hosts file.

  2. Exposing port 5432 for the db in docker-compose.yml

Nothing worked.

Can anyone offer any insight to help me understand and fix the problem?

My Docker file:

FROM ruby:2.5
RUN apt-get update -qq && apt-get install -y build-essential libpq-dev nodejs
RUN mkdir /my-app
WORKDIR /my-app
RUN echo 'gem: --no-document' >> ~/.gemrc
COPY . /my-app
EXPOSE 3000

My docker-compose.yml:

version: '3'
services:
  db:
    image: postgres
    volumes:
      - postgres:/var/lib/postgresql/data/
  redis:
    image: 'redis:4-alpine'
    environment:
      - REDIS_DISABLE_COMMANDS=FLUSHDB,FLUSHALL
    ports:
      - '6379:6379'
    command: redis-server --requirepass somepassword
    volumes:
      - redis:/data/
  myapp:
    build: ./myapp/
    command: bash -c "rm -f tmp/pids/server.pid && bundle exec rails s -p 3000 -b '0.0.0.0'"
    environment:
      - RAILS_ENV=development
      - PORT=3000
      - REDIS_URL=redis://:somepassword@redis:6379/0
    ports:
      - '3000:3000'
    working_dir: /myapp/
    volumes:
      - ./myapp:/myapp:cached
      - myapp_gems:/usr/local/bundle/
      - /myapp/tmp/
    depends_on:
      - db
      - redis
  myapp_sidekiq:
    build: ./myapp/
    command: bash -c "rm -f tmp/pids/server.pid && bundle exec sidekiq -C config/sidekiq.yml"
    environment:
      - RAILS_ENV=development
      - PORT=3000
      - REDIS_URL=redis://:somepassword@redis:6379/0
    working_dir: /myapp/
    volumes:
      - ./myapp:/myapp:cached
      - myapp_gems:/usr/local/bundle/
      - /myapp/tmp/
    depends_on:
      - db
      - redis

volumes:
  postgres:
    external: true
  redis:
    external: true
  myapp_gems:

My database.yml:

# Configure Using Gemfile
# gem 'pg'
#
default: &default
  adapter: postgresql
  encoding: unicode
  # For details on connection pooling, see rails configuration guide
  # http://guides.rubyonrails.org/configuring.html#database-pooling
  pool: 5
  host: db
  username: postgres
  password:

EDIT: In response to @Brits, here is the output of docker-compose up db:

Attaching to dev_db_1
db_1                | Error: Database is uninitialized and superuser password is not specified.      
db_1                |        You must specify POSTGRES_PASSWORD to a non-empty value for the
db_1                |        superuser. For example, "-e POSTGRES_PASSWORD=password" on "docker run".
db_1                | 
db_1                |        You may also use "POSTGRES_HOST_AUTH_METHOD=trust" to allow all
db_1                |        connections without a password. This is *not* recommended.     
db_1                | 
db_1                |        See PostgreSQL documentation about "trust":
db_1                |        https://www.postgresql.org/docs/current/auth-trust.html        
dev_db_1 exited with code 1

This is interesting because I had not specified a POSTGRES_PASSWORD previously, and it worked fine.

Thanks to @Brits I was able to track down the problem. It was caused by a recent, under-the-radar change to docker-library/postgres that requires a POSTGRES_PASSWORD to be specified by default going forward, as pointed out in this Github issue

like image 463
NeyLive Avatar asked Feb 24 '20 02:02

NeyLive


1 Answers

Thanks to @Brits I was able to track down the problem.

It was caused by a recent, under-the-radar change to docker-library/postgres that requires a POSTGRES_PASSWORD to be specified by default going forward, as pointed out in this Github issue

Here's how to fix it:

Add the POSTGRES_PASSWORD as an environment variable to docker-compose.yml (the postgres container as well as the app container):

version: '3'
services:
  db:
    image: postgres
    environment:
      - POSTGRES_PASSWORD=somepassword
    volumes:
      - postgres:/var/lib/postgresql/data/
  redis:
    image: 'redis:4-alpine'
    environment:
      - REDIS_DISABLE_COMMANDS=FLUSHDB,FLUSHALL
    ports:
      - '6379:6379'
    command: redis-server --requirepass somepassword
    volumes:
      - redis:/data/
  myapp:
    build: ./myapp/
    command: bash -c "rm -f tmp/pids/server.pid && bundle exec rails s -p 3000 -b '0.0.0.0'"
    environment:
      - RAILS_ENV=development
      - PORT=3000
      - REDIS_URL=redis://:somepassword@redis:6379/0
      - POSTGRES_PASSWORD=somepassword
    ports:
      - '3000:3000'
    working_dir: /myapp/
    volumes:
      - ./myapp:/myapp:cached
      - myapp_gems:/usr/local/bundle/
      - /myapp/tmp/
    depends_on:
      - db
      - redis
  myapp_sidekiq:
    build: ./myapp/
    command: bash -c "rm -f tmp/pids/server.pid && bundle exec sidekiq -C config/sidekiq.yml"
    environment:
      - RAILS_ENV=development
      - PORT=3000
      - REDIS_URL=redis://:somepassword@redis:6379/0
    working_dir: /myapp/
    volumes:
      - ./myapp:/myapp:cached
      - myapp_gems:/usr/local/bundle/
      - /myapp/tmp/
    depends_on:
      - db
      - redis

volumes:
  postgres:
    external: true
  redis:
    external: true
  myapp_gems:

Then add the ENV variable for the POSTGRES_PASSWORD in the app's database.yml:

# Configure Using Gemfile
# gem 'pg'
#
default: &default
  adapter: postgresql
  encoding: unicode
  # For details on connection pooling, see rails configuration guide
  # http://guides.rubyonrails.org/configuring.html#database-pooling
  pool: 5
  host: db
  username: postgres
  password: <%= ENV['POSTGRES_PASSWORD'] %>
like image 171
NeyLive Avatar answered Oct 12 '22 01:10

NeyLive