如何在Docker中运行Rails? PG :: ConnectionBad无法将主机名“pg”转换为地址:没有与主机名关联的地址

问题描述 投票:3回答:2

我试着按照这里的指南:https://docs.docker.com/compose/rails/

docker-compose.yml
version: '3'
services:
  pg:                                     ######### LOOK HERE!
    image: postgres
    volumes:
      - ./tmp/db:/var/lib/postgresql/data
  web:
    build: .
    command: bundle exec rails server -p 3000 -b '0.0.0.0'
    volumes:
      - .:/app
    ports:
      - "3000:3000"
    depends_on:
      - pg
    links:
      - pg                                     ######### LOOK HERE!
  cms:
    image: joomla
    restart: always
    links:
      - mysql:joomladb
    ports:
      - 8080:80
    environment:
      JOOMLA_DB_HOST: mysql
      JOOMLA_DB_PASSWORD: example
  mysql:
    image: mysql:5.6
    restart: always
    environment:
      MYSQL_ROOT_PASSWORD: example
config/database.yml
# SQLite version 3.x
#   gem install sqlite3
#
#   Ensure the SQLite 3 gem is defined in your Gemfile
#   gem 'sqlite3'
#
default: &default
  adapter: postgresql
  encoding: unicode
  pool: 5
  timeout: 5000
  host: pg                                     ######### LOOK HERE!
  username: postgres
  password:

development:
  <<: *default
  database: project

# Warning: The database defined as "test" will be erased and
# re-generated from your development database when you run "rake".
# Do not set this db to the same as development or production.
test:
  <<: *default
  database: project_test

production:
  <<: *default
  database: project

安慰

C:\Users\Chloe\workspace\project\src>docker-compose run web rake db:create
Starting src_pg_1 ... done
could not translate host name "pg" to address: No address associated with hostname
Couldn't create database for {"adapter"=>"postgresql", "encoding"=>"unicode", "pool"=>5, "timeout"=>5000, "host"=>"pg", "username"=>"postgres", "password"=>nil, "database"=>"project"}
rake aborted!
PG::ConnectionBad: could not translate host name "pg" to address: No address associated with hostname

docker-compose版本1.20.1,build 5d8c71b2,Docker版本18.03.0-ce,build 0520e24302

ruby-on-rails docker docker-compose
2个回答
2
投票

这个设置适用于我在docker上运行rails。基本上需要指定一个URL,这样你就可以使用.env文件来指定postgres url,你将使用你的config / database文件在_development _test和_production之间进行更改,只需添加env即可。文件到您的Web服务docker-compose文件:

/docker-compose.阴谋论:

services:
  postgres:
    image: postgres:9.6
    restart: always
    environment:
      POSTGRES_USER: kunzig #DockerHub postgres docs state this is optional but must be used when password is set.  It will also create a db under the supplied username which you'll use to connect to in rails console such as: $docker-compose exec postgres psql -U YourUserNameHere
      POSTGRES_PASSWORD: 'whateverPWYouWant'
    ports:
      - '5432' #Was originally 5432:5432 with the Left hand side being port on host machine, right hand side is the port on the docker container.  However I let docker choose the port it will use by supplying because 5432 is running on my local for other projects.  
    volumes:
      - postgres:/var/lib/postgresql/data 



  web:
    build: . #Runs the docker build command on the current directory
    links: #Links the listed services to our application so containers can talk to eachother
      - postgres
      - redis
    restart: always
    volumes:
      - .:/kunzig #Left hand side is current directory of compose file, right hand side is container folder.  This needs to be same as Install_path folder in Dockerfile.
    ports:
      - '8000:8000' #Left is the local port and the right side is the container port
    env_file:
      - .YourProjectNameHere.env #This should be in your root project directory along side the Dockerfile and docker-compose file
    depends_on:
      - 'redis'
      - 'postgres'

/.project那么.恶女

DATABASE_URL=postgresql://PostgresUserNameHere:PasswordFromDockerCompose@postgres:5432/PostgresUsername?encoding=utf8&pool=5&timeout=5000

/config/database.阴谋论

development:
  url: <%= ENV['DATABASE_URL'].gsub('?', '_development?' ) %>
test:
  url: <%= ENV['DATABASE_URL'].gsub('?', '_test?' ) %>
production:
  url: <%= ENV['DATABASE_URL'].gsub('?', '_production?' ) %>

保持您的网络服务与pg服务链接,你应该好好去注意我改为调用我的服务postgres。


1
投票

好的,我会试着回答这个问题:

通常当出现此错误时,可能意味着运行db容器会返回一些错误并停止。

我有同样的问题,经过一些挖掘发现我有

LANG='en_US.UTF-8'
LANGUAGE='en_US:en'
LC_ALL='en_US.UTF-8'

在我的环境变量中设置并导致db容器停止。因此,由于容器没有运行,我没有db主机。

这是我的docker-compose.yml文件的样子:

version: '3'

services:
  db:
    image: postgres
    volumes:
      - ./tmp/db:/var/lib/postgresql/data
    env_file: .env
  app:
    build: .
    command: bundle exec puma -p 3000 -C config/puma.rb
    env_file: .env
    volumes:
      - .:/app
    ports:
      - "3031:3000"
    depends_on:
      - db

这是config/database.yml

default: &default
  adapter: postgresql
  encoding: unicode
  host: db
  pool: 5
  username: <%= ENV['POSTGRES_USER'] %>
  password: <%= ENV['POSTGRES_PASSWORD'] %>

development:
  <<: *default
  database: driggl_dev

test:
  <<: *default
  database: driggl_test

production:
  <<: *default
  database: driggl_prod

...以及用于开发的.env文件:

POSTGRES_USER=driggl
POSTGRES_PASSWORD=driggl

由于我共享了db数据卷,因此我确保从存储库中删除了tmp/db文件夹。

rm -rf tmp/*

然后我删除了所有容器和图像

docker rm $(docker ps -q -a) -f
docker rmi $(docker images -q) -f

最后我再次旋转容器:

docker-compose up --build

一切都很顺利。

摘要

  1. 确保系统中某处没有缓存任何内容
  2. 确保您的db容器在启动时不会返回错误。

我希望这会帮助那些遇到这个问题的人。

© www.soinside.com 2019 - 2024. All rights reserved.