docker-compose image named: “prefix_%s_1” instead of “%s”

 ̄綄美尐妖づ 提交于 2020-12-28 09:57:05

问题


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.

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"

After running 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

Output from the test_monkey_1 container:

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

回答1:


According to docker compose issue #745:

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

which is the equivalent to (the more readable):

$ docker-compose --project-name MY_PROJECT_NAME



回答2:


You could just set container name to what you want via container_name:

redis:
    build: ../storage/redis
    container_name: redis
    ports:
      - "6379:6379"

And container hostname could also be set via hostname.




回答3:


Docker compose will use the name of the directory with your compose file as the name of the project unless you set if via the -p option

-p, --project-name NAME     Specify an alternate project name (default: directory name)

Compose supports declaring default environment variables in an environment file named .env placed in the folder where the docker-compose command is executed (current working directory).

Where you'll want to set COMPOSE_PROJECT_NAME




回答4:


That's just how docker-compose names containers so that it can manage them.

The basename is the name of the directory containing the docker-compose.yaml file. This is followed by the name of the container as specified in your docker-compose.yaml file, and finally that is followed by an instance number which increases if you bring up multiple instances of a container using something like docker-compose scale.

This naming scheme is how docker-compose is able to identify your containers when you attempt to operate on them using something like docker-compose stop.

I don't think this conflicts with the documentation in any way. That is, if I start with, say, this docker-compose.yaml in a directory named sotest:

irc:
  image: docker.io/xena/elemental-ircd
  links:
    - web

web:
  image: larsks/thttpd

And then bring up the compose:

$ docker-compose up

I get two containers:

CONTAINER ID        IMAGE                           ...NAMES
960c1491c03e        docker.io/xena/elemental-ircd   ...sotest_irc_1
422bba313e71        larsks/thttpd                   ...sotest_web_1

If I look at the /etc/hosts file inside of sotest_irc_1, I see:

172.17.0.28 web 422bba313e71 sotest_web_1

In addition to a number of other names. So the linked host is available by name as described in the docs.



来源:https://stackoverflow.com/questions/33045358/docker-compose-image-named-prefix-s-1-instead-of-s

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!