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
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
.
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.
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
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