我在 docker 容器上遇到了问题。当我执行docker-compose up
以启动应用程序时,Postgres 容器未启动。
我在码头工人撰写后收到的错误
/usr/local/bundle/gems/activerecord-4.2.0/lib/active_record/connection_adapters/postgresql_adapter.rb:651:in `initialize': could not translate host name "db" to address: Name or service not known (PG::ConnectionBad)
现在它经常嘶吼。我尝试了几个步骤作为db
容器的添加端口,即5432:5432
.我曾经启动-停止特定的数据库容器,以便重新建立连接,但它不起作用。
申请详情:
轨道版本:4.2.0红宝石版本:2.2.0
docker-compose.yml
version: '3.7'
services:
selenium:
image: selenium/standalone-chrome-debug:3.141.59-krypton
ports: ['4444:4444', '5900:5900']
logging:
driver: none
redis:
image: redis:3.0.0
elastic:
image: elasticsearch:1.5.2
db:
image: postgres:9.3.10
volumes:
- ./tmp/db:/var/lib/postgresql/data
- .:/home
XYZ:
build: .
command: bash -c "rm -f tmp/pids/server.pid && bundle exec rails s -p 3000 -b '0.0.0.0'"
stdin_open: true
tty: true
volumes:
- XYZ-sync:/home:nocopy
ports:
- "3000:3000"
depends_on:
- db
- redis
- elastic
- selenium
environment:
- REDIS_URL=redis://redis:6379/0
- ELASTICSEARCH_URL=elastic://elastic:9200/0
- SELENIUM_HOST=selenium
- SELENIUM_PORT=4444
- TEST_APP_HOST=XYZ
- TEST_PORT=3000
数据库日志
db_1 | LOG: database system was shut down at 2019-09-10 07:37:08 UTC
db_1 | LOG: MultiXact member wraparound protections are now enabled
db_1 | LOG: database system is ready to accept connections
db_1 | LOG: autovacuum launcher started
db_1 | LOG: received smart shutdown request
db_1 | LOG: autovacuum launcher shutting down
db_1 | LOG: shutting down
db_1 | LOG: database system is shut down
db_1 | LOG: database system was shut down at 2019-09-10 07:37:50 UTC
db_1 | LOG: MultiXact member wraparound protections are now enabled
db_1 | LOG: database system is ready to accept connections
db_1 | LOG: autovacuum launcher started
db_1 | LOG: database system was interrupted; last known up at 2019-09-10 07:38:31 UTC
db_1 | LOG: received smart shutdown request
db_1 | LOG: database system was interrupted; last known up at 2019-09-10 07:38:31 UTC
db_1 | LOG: database system was not properly shut down; automatic recovery in progress
db_1 | LOG: record with zero length at 0/1D8F0120
db_1 | LOG: redo is not required
db_1 | LOG: MultiXact member wraparound protections are now enabled
db_1 | LOG: autovacuum launcher started
db_1 | LOG: database system is ready to accept connections
db_1 | LOG: stats_timestamp 2019-09-10 08:02:39.288642+00 is later than collector's time 2019-09-10 08:02:39.189551+00 for database 0
db_1 | LOG: database system was interrupted; last known up at 2019-09-10 08:18:02 UTC
db_1 | FATAL: the database system is starting up
docker-compose ps
输出
xyz_db_1 /docker-entrypoint.sh postgres Up 5432/tcp
xyz_elastic_1 /docker-entrypoint.sh elas ... Up 9200/tcp, 9300/tcp
xyz_xyz_1 bash -c rm -f tmp/pids/ser ... Exit 1
xyz_redis_1 /entrypoint.sh redis-server Up 6379/tcp
xyz_selenium_1 /opt/bin/entry_point.sh Up 0.0.0.0:4444->4444/tcp, 0.0.0.0:5900->5900/tcp
database.yml
default: &default
adapter: postgresql
encoding: unicode
pool: 5
username: postgres
password:
host: db
development:
<<: *default
database: XYZ_development
test:
<<: *default
database: XYZ_test
development_migrate:
adapter: mysql2
encoding: utf8
database: xyz_ee
username: root
password:
host: localhost
pool: 5
任何帮助将不胜感激。
我在@jayDosrsey建议的帮助下解决了我的问题。 数据库容器在主Web容器之前启动,因此它总是无法启动容器,我再次需要重新启动Web容器。
通过在启动 rails 服务器时添加运行状况检查条件来解决此问题。
XYZ:
build: .
command: bash -c "while !</dev/tcp/db/5432; do sleep 1; done; rm -f tmp/pids/server.pid && bundle exec rails s -p 3000 -b '0.0.0.0'"
...
Now I am able to start the container in sequence.