gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 09:47:24 25 4
gpt4 key购买 nike

我尝试按照此处的指南进行操作: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,内部版本 5d8c71b2,Docker 版本 18.03.0-ce,内部版本 0520e24302

最佳答案

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

通常,当出现此错误时,可能意味着运行 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

由于我有数据库数据的共享卷,因此为了确定起见,我从存储库中删除了 tmp/db 文件夹。

rm -rf tmp/*

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

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

最后我再次启动容器:

docker-compose up --build

一切终于顺利了。

摘要

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

我希望这会对遇到该问题的任何人有所帮助。

关于ruby-on-rails - 如何在 Docker 中运行 Rails? PG::ConnectionBad 无法将主机名 "pg"转换为地址:没有与主机名关联的地址,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50090012/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com