简体   繁体   English

Docker compose with Rails and Postgres 无法连接到服务器:没有路由到主机 是服务器

[英]Docker compose with Rails and Postgres could not connect to server: No route to host Is the server

I'm currently having an issue with my docker-compose that have these services.我目前的 docker-compose 有这些服务的问题。 Rails app and Postgres. Rails 应用程序和 Postgres。 These are my configurations:这些是我的配置:

docker-compose.yml 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 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数据库.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
  

I can build the app using docker-compose build but whenever I docker-compose up the service db exited but my rails app is running.我可以使用docker-compose build构建应用程序,但每当我docker-compose up ,服务db就会退出,但我的 Rails app正在运行。

This is the logs I'm getting when I run docker-compose up这是我运行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".

The error I'm getting when I access http://localhost:3000 is我访问 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?

I think you should use volume for Postgres too.我认为您也应该为 Postgres 使用卷。

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

I had similar issue and fixed it with that.我有类似的问题并解决了它。 Try also to restart Docker.也尝试重新启动 Docker。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Docker:Docker-compose ruby​​ + postgres - PG::ConnectionBadcould 无法连接到服务器 - Docker: Docker-compose ruby + postgres - PG::ConnectionBadcould not connect to server Postgres 无法连接到服务器 - Postgres could not connect to server Rails-Postgres-无法连接到服务器:连接被拒绝(PG :: ConnectionBad) - Rails - Postgres - could not connect to server: Connection refused (PG::ConnectionBad) Postgres 服务器错误 -&gt; PGError:无法连接到服务器 - Postgres Server error -> PGError: could not connect to server Rails在Mac OS X / PG :: ConnectionBad上找不到我的Postgres服务器:无法连接到服务器 - Rails cannot find my Postgres server on Mac OS X / PG::ConnectionBad: could not connect to server Rails-在服务器上创建postgres数据库会引发错误“无法连接到服务器:连接被拒绝” - Rails - Creating postgres database on server throws error “could not connect to server: Connection refused” 如何将 Rails 应用程序连接到主机上的 docker postgres - how to connect Rails app to docker postgres on host machine 如何编写 Docker 文件并为 Rails 服务器编写 - How to write Docker file and compose for rails server Docker-撰写Rails Postgres - Docker-compose rails postgres 如何在Docker上将Rails与Postgresql连接? 正在获取:无法连接到服务器:连接被拒绝 - How to connect rails with postgresql on docker? Getting: could not connect to server: Connection refused
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM