This is my docker compose file where I have redis service and volume declared dynamically
redis:
container_name: redis-${ENV}
image: redis
command: --port ${REDIS_PORT}
ports:
- "${REDIS_PORT}:6379"
volumes:
- redis-${ENV}:/data
networks:
- app
volumes:
redis:
name: redis-${ENV}
external: false
I get error: service "redis" refers to undefined volume redis-dev: invalid compose project
2
Answers
Once I make your
docker-compose.yml
workable, by removing the undefinedapp
network, which is not relevant, and adding the missingservices
section:I get your same output:
I think the problem is that you assumed that you could refer to this volume by the value of its
name
property. That is just a label, but its identifier is stillredis
, which cannot be interpolated with environment variables (no key can, only values).In other words, I don’t think what you wanted to do can be done, at least not just within the
docker-compose.yml
file. You can achieve this by creating the volume externally and then using it as an external volume.These two names need to match:
Note that Compose is able to construct volume and container names itself. It uses a project name as part of the name, and so the generated names wind up looking a lot like what you’ve specified, so long as you use a
docker-compose -p dev
option or similar. You can probably remove the manual name settings and the mentions of${ENV}
throughout the file.If you also let each Redis instance use its standard port 6379 inside its container (the container network isolation means there won’t be a conflict) and use the Compose-provided
default
network, you can reduce this to as little as