Docker Compose主机名命令不起作用 [英] Docker Compose hostname command not working
问题描述
I'm unable to get the Docker Compose hostname
command to work.
我正在运行一个简单的 docker-compose.yml
:
I'm running a simple docker-compose.yml
:
version: '3'
services:
redis1:
image: "redis:alpine"
hostname: redis1host
redis2:
image: "redis:alpine"
hostname: redis2host
一旦我使用 docker-compose up
运行它,我应该能够运行 docker-compose exec redis1/bin/ash
,然后 ping redis2host
与其他Redis容器通信,但ping不能到达其目的地.我可以使用 ping redis2
ping其他Redis容器.
Once I run this with docker-compose up
, I should be able to run docker-compose exec redis1 /bin/ash
and then ping redis2host
to talk to the other Redis container, but the ping just doesn't reach its destination. I can ping the other Redis container with ping redis2
.
ping redishost2
应该可以,不是吗?
推荐答案
hostname
指令只是在容器内设置主机名(即,您为响应而返回的名称主机名
或 uname -n
命令).它不会导致该服务的DNS别名.为此,您需要别名指令.由于该指令是针对每个网络的,因此您需要明确说明网络,而不是使用compose默认值,例如:
The hostname
directive simply sets the hostname inside the container (that is, the name you get back in response to the hostname
or uname -n
commands). It does not result in a DNS alias for the service. For that, you want the aliases directive. Since that directive is per-network, you need to be explicit about networks rather than using the compose default, for example:
version: '3'
services:
redis1:
image: "redis:alpine"
hostname: redis1host
networks:
redis:
aliases:
- redis1host
redis2:
image: "redis:alpine"
hostname: redis2host
networks:
redis:
aliases:
- redis2host
networks:
redis:
这篇关于Docker Compose主机名命令不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!