Reputation: 14866
I have the following compose file:
services:
myproject:
environment:
- ASPNETCORE_ENVIRONMENT=Development
- ASPNETCORE_URLS=https://+:443;http://+:80
- ASPNETCORE_HTTPS_PORT=44308
- PROJECT_NAME=MyProject
volumes:
- ${APPDATA}/ASP.NET/Https:/root/.aspnet/https:ro
- ${APPDATA}/Microsoft/UserSecrets:/root/.microsoft/usersecrets:ro
- ${APPDATA}/Turma/${PROJECT_NAME}/Logs:/var/logs/${PROJECT_NAME}
On the line:
- ${APPDATA}/Turma/${PROJECT_NAME}/Logs:/var/logs/${PROJECT_NAME}
It recognises ${APPDATA}
but for ${PROJECT_NAME}
it uses the literal string not the environment variable value.
Is there a way to make this work so the actual project name is used in path?
Upvotes: 3
Views: 5595
Reputation: 121
As far as I am aware, you cannot reference your env variables defined within the compose file later in the same compose file and have them interpreted. Your definition of $APPDATA works since that's set in the host's environment, not the compose file.
I tested both using the env variable and a .env file with compose 2.3 and 3, and neither worked.
I recommend wrapping your compose file in a run script where you can set the variables needed in your host shell, so you can have those interpreted properly. If you're deploying with a standard tool such as ansible, jenkins, etc. those can all set variables for you. This can look like the following:
#!/bin/bash
export PROJECT_NAME=foo
docker-compose up -d
unset PROJECT_NAME
Although it may not work for creating volumes, if you just need the variable to do something during the container's runtime (such as setting another environment variable), that can be put into an entrypoint script as well.
Upvotes: 1