簡體   English   中英

為什么我的 Docker PostgreSQL 容器不再運行?

[英]Why won't my Docker PostgreSQL container run anymore?

自從我將 Windows 的 Docker 桌面從 2.1.0.5 更新到版本 2.2.0.3 以來,我的應用程序的 db 容器出現了問題。 之前在 2.1.0.5 上這一切都很好,但現在即使在我刪除 2.2.0.3 並恢復到 2.1.0.5 之后它也無法工作。 真希望我現在沒有更新。

我得到的錯誤是:

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

當我運行 docker ps 時,我可以看到 postgres 容器甚至沒有運行。

我嘗試過的一些事情是:

  1. 將 db 添加到我的 Windows 10 主機文件中。

  2. 在 docker-compose.yml 中為數據庫公開端口 5432

沒有任何效果。

任何人都可以提供任何見解來幫助我理解和解決問題嗎?

我的 Docker 文件:

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

我的 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:

我的數據庫.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:

編輯:為了回應@Brits,這里是 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

這很有趣,因為我之前沒有指定 POSTGRES_PASSWORD,它運行良好。

感謝@Brits,我能夠找到問題所在。 這是由最近對 docker-library/postgres 的未公開更改引起的,該更改需要默認指定 POSTGRES_PASSWORD,如此Github 問題中所指出的

感謝@Brits,我能夠找到問題所在。

這是由最近對 docker-library/postgres 的未公開更改引起的,該更改需要默認指定 POSTGRES_PASSWORD,如此Github 問題中所指出的

以下是修復方法:

將 POSTGRES_PASSWORD 作為環境變量添加到 docker-compose.yml(postgres 容器以及應用程序容器):

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:

然后在應用的 database.yml 中為 POSTGRES_PASSWORD 添加 ENV 變量:

# 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'] %>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM