[英]Docker container in docker-compose can't find other containers by hostname
I am upgrading a docker-compose file from v1 to v3 and have a problem to link the containers together. 我正在将docker-compose文件从v1升级到v3,并且将容器链接在一起时遇到问题。
Before the docker-compose.yml was this: 在docker-compose.yml之前是这样的:
database:
working_dir: /mnt
volumes:
- .:/mnt
links:
- postgresql_db
ports:
- '3000:3000'
build: ./database
entrypoint: python database/server.py
postgresql_db:
build: ./database
ports:
- '5432:5432'
test_database:
working_dir: /mnt
volumes:
- .:/mnt
links:
- test_postgresql_db
ports:
- '5053:5053'
build: ./database/test
test_postgresql_db:
image: postgres:latest
ports:
- '5432:5432'
But links has been deprecated. 但链接已被弃用。 Now containers should share a network (which they should do by default) and be able to find each other using hostnames (see: here ).
现在容器应共享一个网络(默认情况下应该这样做),并且能够使用主机名找到彼此(请参阅: 此处 )。
So I modified the file to: 所以我将文件修改为:
version: '3'
networks:
dbnet:
driver: bridge
volumes:
postgresql_data: {}
postgresql_test_data: {}
services:
database:
build: database
environment:
APPLICATION_DB_CONTAINER: db
APPLICATION_POSTGRES_HOST: db
working_dir: /mnt
volumes:
- .:/mnt
networks:
- dbnet
ports:
- '3000:3000'
command: python database/server.py
db:
image: postgres:latest
volumes:
- postgresql_data:/var/lib/postgresql/data
networks:
- dbnet
depends_on:
- database
test_database:
build: database/test
environment:
APPLICATION_DB_CONTAINER: testdb
working_dir: /mnt
volumes:
- .:/mnt
command: python -m pytest --cov=database --cov-report term --cov-report html:htmlcov database/test/
testdb:
image: postgres:latest
volumes:
- postgresql_test_data:/var/lib/postgresql/data
The problem I have is that the database container can't find the db container by hostname and I get (my software tries to connect every second for 10 seconds and then stops): 我遇到的问题是数据库容器无法通过主机名找到数据库容器,我得到(我的软件尝试连接每秒10秒然后停止):
ERROR: Database postgresql://postgres:@db:5432/postgres NOT found (10 attempts)
错误:数据库postgresql:// postgres:@db:5432 / postgres NOT found(10次尝试)
I checked and the db container is up, if I use its IP address then it works but using the hostname does not. 我检查并且数据库容器已启动,如果我使用其IP地址然后它可以工作,但使用主机名却没有。
How can I link the database and db container and make sure it finds it each time ? 如何链接数据库和数据库容器并确保每次都找到它?
Info: I run docker Version 18.06.0-ce-mac70 on MacOs. 信息:我在MacOs上运行docker Version 18.06.0-ce-mac70。
Thank you ! 谢谢 !
EDIT: Tried to add this in the compose file for db, but did not change the problem. 编辑:试图在db的compose文件中添加它,但没有改变问题。
ports: '5432:5432'
港口:'5432:5432'
sqlalchemy.exc.OperationalError: (psycopg2.OperationalError) could not translate host name "db" to address: Name or service not known database_1 |
sqlalchemy.exc.OperationalError:(psycopg2.OperationalError)无法将主机名“db”转换为地址:名称或服务未知数据库_1 (Background on this error at: http://sqlalche.me/e/e3q8 )
(关于此错误的背景: http : //sqlalche.me/e/e3q8 )
I get this error from sqlalchemy that indicates that the database container does no know what db (or db-1) is and the file /etc/hosts does not contain any mention of another container 我从sqlalchemy收到此错误,指示数据库容器不知道db(或db-1)是什么,文件/ etc / hosts不包含任何其他容器的提及
As @sachav indicated in the comments, I inverted the depends_on. 正如@sachav在评论中指出的那样,我颠倒了depends_on。
In the db service, 在数据库服务中,
db:
depends_on:
- database
...
forced the db service to be started before database, thus preventing database to know that the host db existed ! 强制db服务在数据库之前启动,从而阻止数据库知道主机db存在!
Taking it out and adding 拿出来并加入
database:
depends_on:
- db
...
made the trick :) 诀窍:)
Thank you @sachav, I'll go face palm myself now. 谢谢@sachav,我现在就自己去面对。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.