docker-compose图片,名为:"prefix_%s_1"而不是“%s" [英] docker-compose image named: "prefix_%s_1" instead of "%s"
问题描述
当我在带有链接的 docker-compose.yaml
文件中设置几个Docker容器时,这些容器的名称最终将以 prefix_%s_1
的格式显示而不是%s
,并且链接容器上的/etc/hosts
中的别名是相同的.
When I set up a couple of Docker containers in docker-compose.yaml
file with links, the name of the containers ends up being of the format prefix_%s_1
instead of %s
, and the alias in /etc/hosts
on the linking container is the same.
为什么 redis
容器的别名 test_redis_1
而不是 redis
?
Why is the alias of the redis
container test_redis_1
instead of redis
?
以下是相关文件和输出:
Here are the relevant files and output:
# docker-compose.yaml
monkey:
build: ../../monkey
dockerfile: test.Dockerfile
links:
- redis:redis
ports:
- "9006:9006"
redis:
build: ../storage/redis
ports:
- "6379:6379"
运行 docker-compose build&&之后docker-compose up
:
$ docker-compose ps
Name Command State Ports
---------------------------------------------------------------------------
test_redis_1 redis-server /usr/local/et ... Up 0.0.0.0:6379->6379/tcp
test_monkey_1 python -m SimpleHTTPServer ... Up 0.0.0.0:9006->9006/tcp
test_monkey_1
容器的输出:
Step 14 : RUN cat /etc/hosts
---> Running in 1c104e3d9bf5
172.17.1.26 75a485df1325
127.0.0.1 localhost
::1 localhost ip6-localhost ip6-loopback
fe00::0 ip6-localnet
ff00::0 ip6-mcastprefix
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters
172.17.1.26 thirsty_lumiere.bridge
172.17.0.220 test_redis_1
172.17.0.220 test_redis_1.bridge
172.17.1.26 thirsty_lumiere
---> 3a06bac9b3ca
Removing intermediate container 1c104e3d9bf5
Step 15 : RUN echo
---> Running in a1c5b5f8ae0f
---> 385c9ee44332
Removing intermediate container a1c5b5f8ae0f
Step 16 : RUN ifconfig eth0 | grep inet addr
---> Running in 17cd638c6473
inet addr:172.17.1.28 Bcast:0.0.0.0 Mask:255.255.0.0
---> 21235e29abc1
Removing intermediate container 17cd638c6473
Step 17 : RUN echo
---> Running in 8c0b1db2a69b
---> e2dd190eb4d1
Removing intermediate container 8c0b1db2a69b
Step 18 : RUN env
---> Running in 50cd1b6bf9da
HOSTNAME=75a485df1325
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
PWD=/data/web/tunnelbear.com/play/tPWD=/data/web/tunnelbear.com/root
_=/usr/bin/env
推荐答案
根据 docker撰写问题#745 :
默认情况下,Compose将项目名称基于从中运行目录撰写命令.项目名称可以是通过为每个传递-p/--project-name选项覆盖命令或设置COMPOSE_PROJECT_NAME环境变量.
By default, Compose bases the project name on basename of the directory compose commands are run from. The project name can be overridden either by passing a -p / --project-name option for each command or setting the COMPOSE_PROJECT_NAME environment variable.
您可以通过编写如下内容来为容器名称设置选择的前缀:
You can set the prefix of your choice to your container names by writing something like this:
$ docker-compose -p MY_PROJECT_NAME
相当于(更易读):
$ docker-compose --project-name MY_PROJECT_NAME
这篇关于docker-compose图片,名为:"prefix_%s_1"而不是“%s"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!