I am using docker compose to set up application environments. There are two distinct environments, test and production.
In a test environment, I need to expose additional ports (for debugging). These ports should remain closed in a production environment.
I would also like to use the same image and docker-compose.yml
file. Using the same image is no problem but I am struggeling with the compose file. In it, I would like to open or close a port based on an environment variable.
The current setup is pretty much the standard, like this:
# ...
ports:
- "8080:8080" # HTTP Server port
- "9301:9301" # debug port
# ...
In this example, both ports are always exposed. Is it possible to expose the port 9301
only if a certain environment variable, say EXPOSE_DEBUG
, is set?
3
Answers
The solution I usually use in my projects is to make a bash script that writes the
docker-compose.yml
based on the value of the environment variable. But you could write it with any other programming language as well.You can use profiles or a second compose file.
Then you can use the below command or an environment variable to set the profile,
COMPOSE_PROFILES
.Alternatively, you can use a second compose file and override the ports.
Then you can use the file after the main file to patch it:
The file(s) to use can also be controls with an environment variable,
COMPOSE_FILE
.If you name the file
compose.override.yaml
, docker will automatically use it, so you don’t have to point to it with the -f flag. Be careful that you don’t add this file to your production system, if you choose to do this.You could also bind the debug port to the loopback interface so that you can only access it locally.
Conditional statements (
if else
) are not supported in docker compose.docker compose -f
)docker-compose.yml
programmatically by yourselfTo just maintain dev/prod environments in my opinion solution 2 is the most efficient in terms of effort.
To follow your approach:
You can set port mapping by envs like:
.env
-File or add them indocker compose up -e
commanddocker-compse.yml
But afaik there is no way to omit one of them