【问题标题】:Docker compose with Rails and Postgres could not connect to server: No route to host Is the serverDocker compose with Rails 和 Postgres 无法连接到服务器:没有到主机的路由是服务器
【发布时间】:2020-12-22 09:32:16
【问题描述】:

我目前在使用具有这些服务的 docker-compose 时遇到问题。 Rails 应用程序和 Postgres。这些是我的配置:

docker-compose.yml

version: '3'
services:
 db:
  image: postgres:alpine
  restart: always
  volumes:
   - ./tmp/db:/var/lib/postgresql/data
  ports:
    - "5432:5432"
  environment:
    - POSTGRES_USER=postgres
    - POSTGRES_PASSWORD=postgres
  
 app:
  build: .
  restart: always
  command: bash -c "rm -f tmp/pids/server.pid && rails s -p 3000 -b '0.0.0.0'"
  volumes:
   - .:/myapp
   - bundle_path:/bundle
  ports:
   - "3000:3000"
  depends_on:
   - db

volumes:
  bundle_path:

Dockerfile

FROM ruby:2.5.3-slim

# install rails dependencies
RUN apt-get update -qq \
  && apt-get install -y \
  # Needed for certain gems
  build-essential \
  # Needed for postgres gem
  libpq-dev \
  # Others
  nodejs \
  vim-tiny \   
  # The following are used to trim down the size of the image by removing unneeded data
  && apt-get clean autoclean \
  && apt-get autoremove -y \
  && rm -rf \
  /var/lib/apt \
  /var/lib/dpkg \
  /var/lib/cache \
  /var/lib/log

# Changes localtime to Singapore
RUN cp /usr/share/zoneinfo/Asia/Singapore /etc/localtime

# create a folder /myapp in the docker container and go into that folder
RUN mkdir /myapp

WORKDIR /myapp

COPY Gemfile /myapp/Gemfile

COPY Gemfile.lock /myapp/Gemfile.lock

# Run bundle install to install gems inside the gemfile
RUN bundle install

ADD . /myapp

CMD bash -c "rm -f tmp/pids/server.pid && rails s -p 3000 -b '0.0.0.0'"

database.yml

default: &default
  adapter: postgresql
  encoding: unicode
  pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>

development:
  <<: *default
  database: myapp_development
  host: db
  username: postgres
  password: postgres
  port: 5432
  

我可以使用docker-compose build 构建应用程序,但每当我docker-compose up 服务db 退出但我的rails app 正在运行时。

这是我运行docker-compose up时得到的日志

db_1   | The files belonging to this database system will be owned by user "postgres".
db_1   | This user must also own the server process.
db_1   |
db_1   | The database cluster will be initialized with locale "en_US.utf8".
db_1   | The default database encoding has accordingly been set to "UTF8".
db_1   | The default text search configuration will be set to "english".
db_1   |
db_1   | Data page checksums are disabled.
db_1   |
db_1   | initdb: error: directory "/var/lib/postgresql/data" exists but is not empty
db_1   | If you want to create a new database system, either remove or empty
db_1   | the directory "/var/lib/postgresql/data" or run initdb
db_1   | with an argument other than "/var/lib/postgresql/data".

我访问 http://localhost:3000 时遇到的错误是

could not connect to server: No route to host Is the server running on host "db" (172.18.0.2) and accepting TCP/IP connections on port 5432?

【问题讨论】:

    标签: ruby-on-rails postgresql docker docker-compose


    【解决方案1】:

    我认为你也应该为 Postgres 使用音量。

    services:
      db:
        image: postgres:alpine
        restart: always
        volumes:
         - postgres_volume:/var/lib/postgresql/data
    volumes:
      postgres_volume:
    

    我有类似的问题并解决了它。也尝试重新启动 Docker。

    【讨论】:

    • 现在可以使用了。想知道我的其他使用 docker-compose 的 rails 应用程序我不需要安装 postgres 的卷并且它正在工作。不确定这个是否需要。
    • 老实说,我不知道,但我花了一些时间处理容量问题,尤其是 Postgres。也许从我的角度来看这是一个糟糕的配置,但我从来没有弄清楚为什么/如何。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-08-10
    • 2015-08-26
    • 1970-01-01
    • 2019-10-06
    • 1970-01-01
    • 1970-01-01
    • 2012-11-04
    相关资源
    最近更新 更多