Docker-compose image named: "prefix_% s_1" instead of "% s"

When I create a pair of Docker containers in the docker-compose.yaml with links, the container name ends in the format prefix_%s_1 instead of %s , and the alias in /etc/hosts the same when linking the container.

Why is the redis container redis 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 

Exiting container 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 
+20
docker docker-compose
source share
4 answers

According to the docker make number # 745 :

By default, Compose bases the project name on basename. Directory-compilation commands are run from. The project name can be redefined by passing the option -p / - -p object-name for each command or setting the environment variable COMPOSE_PROJECT_NAME.

You can set the prefix of your choice for container names by writing something like this:

 $ docker-compose -p MY_PROJECT_NAME 

which is equivalent (more readable):

 $ docker-compose --project-name MY_PROJECT_NAME 
+16
source share

You can simply set the container name to what you want via container_name :

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

And the hostname of the container can also be set via hostname .

+8
source share

Docker compose will use the directory name with your compose file as the project name if you did not install it using the -p option

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

Compose supports the declaration of default environment variables in an environment file named .env placed in the folder in which the docker-compose command is run (the current working directory).

Where do you want to set COMPOSE_PROJECT_NAME

+5
source share

This is how name containers are for docker containers so that they can manage them.

The base name is the name of the directory containing the docker-compose.yaml . It is followed by the container name specified in your docker-compose.yaml , and finally, followed by the instance number, which increases if you call multiple instances of the container using something like docker-compose scale .

This naming scheme is how the docker-compose command can identify your containers when you try to work with them using something like docker-compose stop .

I do not think this is somehow in conflict with the documentation. That is, if I start with, say, this docker-compose.yaml in a directory called sotest :

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

And then create the composition:

 $ 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 inside sotest_irc_1 , I see:

 172.17.0.28 web 422bba313e71 sotest_web_1 

In addition to a number of other names. Thus, the linked node is accessible by name, as described in the docs.

+4
source share

All Articles