Docker:按主机访问另一个容器 [英] Docker : Accessing another container by host
问题描述
我在docker-compose yaml文件中定义了两个容器,这些容器需要彼此通信,但是它们不能.
I have two containers defined in a docker-compose yaml file that need to talk to each other, but they can't.
version: "3.9"
networks:
localdev:
driver: 'bridge'
services:
master-db:
image: mysql:8.0
container_name: master-db
hostname: master-db
command: --default-authentication-plugin=mysql_native_password
restart: always
ports:
- "4000:3306"
networks:
- localdev
page-store:
hostname: page-store
build:
context: .
dockerfile: Dockerfile.page_store
container_name: page-store
ports:
- "2020:2020"
networks:
- localdev
links:
- master-db
在页面存储区的Python Flask微服务中,我尝试使用主机名master-db来访问MySQL数据库,但是该名称无法解析.
In the page-store Python Flask microservice, I try to access the MySQL database by using its hostname of master-db, but the name cannot resolve.
推荐答案
您应该能够使用各自的服务名称相互连接. master-db
和 page-store
删除 hostname
You should be able to connect each other using respective service names. master-db
and page-store
removing hostname
根据官方指南,如果您想在容器的/etc/hosts
中定义 master-db
, page-store
使用 hostname:page-store
等.
As per Official guide you may have to define master-db
,page-store
in container's /etc/hosts
, if you want to use hostname: page-store
etc.
请参考此 SO线程.
还使用>链接可能不是最佳选择.
Also using --links may not be the best option.
这篇关于Docker:按主机访问另一个容器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!