[BUG] <.env variable network name not working>
Description
His guys, i have problems with .env passing variables but the problem is only in the network service definition, because the error show the correct value in the redis service, also the project name works in the redis container_name
(edit: extra info: i want to create a network, i dont wat to call a external network)
thanks!
(both files .yml and .env in same folder)
error: root@server:/home/xxx/dev# docker compose -f docker-compose.yml config service "redis" refers to undefined network xxx-dev-network: invalid compose project
versions root@server:/home/xxx/dev# docker -v Docker version 27.0.1, build 7fafd33
root@server:/home/xxx/dev# docker compose version Docker Compose version v2.28.1
------docker-compose.yml
services:
redis:
container_name: ${PROJECT_NAME}_redis
image: redis:alpine
restart: always
networks:
- ${NETWORK_NAME}
networks:
${NETWORK_NAME}:
name: ${NETWORK_NAME}
-------my .env
PROJECT_NAME=xxx
AMBIENTE=dev
NETWORK_NAME=${PROJECT_NAME}-${AMBIENTE}-network
( also with plain text value without nesting variables not work, this not work NETWORK_NAME=network)
Steps To Reproduce
No response
Compose Version
No response
Docker Environment
No response
Anything else?
No response
Hello @elgordodevops,
Interpolation is only possible for values and not for yaml keys. Generally, I believe this approach is problematic as it introduces unnecessary complexity.
networks:
${NETWORK_NAME}: <-- this is won't work, interpolation does not work here
name: ${NETWORK_NAME} <-- OK
Does the name of the network really need to be dependent on the service name? If you are trying to generate a docker-compose.yml, use a templating tool like mustache or handlebars, and generate #the docker-compose.yml file yourself from env vars.
Hi @jhrotko thanks for you help
Maybe i can do this in a different way, that im trying to do is to isolate the networks creating a template and filling the values with the .env, i need to isolate because i have multiple projects and multiple stages, like uat,dev, etc.,
is not possible also call the redis network with variables?, i trying to do this but i have this error "docker compose -f docker-compose.yml config service "redis" refers to undefined network dev-dbmx-network: invalid compose project"
services:
redis:
container_name: ${AMBIENTE}-${PROJECT_NAME}-redis
image: redis:alpine
restart: always
networks:
- ${NETWORK_NAME}
networks:
network:
name: ${NETWORK_NAME}
@elgordodevops use:
services:
redis:
networks:
- my_network
networks:
my_network:
name: ${NETWORK_NAME}
Anyway, IIRC we added support for interpolation on resources keys some time ago, I wonder this is a regression, need to give it a try
@elgordodevops did @ndeloof help you solve your issue? If so please close this issue or if you have any other related questions feel free to ask them!
Hi @jhrotko I'm not sure if naming my_network that way in all the compose files of different stages and then in name: differently completely separates the networks.
And according to @ndeloof , I understand there was an improvement for the "bug" I reported and it is not working, is that right?
@elgordodevops key used in networks doesn't matter, as long as you use a name attribute to set the actual network name. But services.xx.networks must refer to network by this key
Hi @ndeloof , thanks for yor help
I understand, thanks. I will investigate more about the functioning of mappings network in services and network names.
Leaving that aside, you mentioned that an improvement was added to interpolate the keys, right?.So, is there a bug?