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
You may name your Dockerfiles however you like. The default filename is Dockerfile (without an extension), and using the default can make various tasks easier while working with containers.
You can rename your docker image by docker tag command. Use the below given command to do that. To rename docker container, use the rename sub-command as shown, in the following example, we renaming the container discourse_app to a new name disc_app.
Following the deprecation of Compose on Kubernetes, support for Kubernetes in the stack and context commands in the docker CLI is now marked as deprecated as well.
depends_on is a Docker Compose keyword to set the order in which services must start and stop. For example, suppose we want our web application, which we'll build as a web-app image, to start after our Postgres container.
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
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
.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With