Reputation: 150624
I have a docker-compose.yml
file in one of my projects where I link multiple containers to build one application. The main entry point (i.e. a container with a web server) is run on host port 8080
.
Now this port is hard-coded into the docker-compose.yml
file.
Is there a way to dynamically set this port using runtime parameters? I could not find anything on this in the documentation, but on the other hand I can not imagine that this should not be possible.
Is it?
Upvotes: 13
Views: 6799
Reputation: 743
If you want to expose a port dynamically from compose ports
directive, you can use environment variables in said directive, making sure you actually have those variables loaded for docker engine, example:
# docker-compose.yml
version: '3'
services:
abcservice:
ports:
- "${ENV_API_PORT}:3000"
# .env
ENV_API_PORT=8082
To load those variables for docker engine, pass the .env file reference via --env-file
option instead of loading them into your shell:
docker-compose --env-file .env up
And so make sure to always pass that option when calling docker-compose
commands.
Upvotes: 0
Reputation: 114
This question is super old, but it's been viewed a bunch of times so I'll post the link to the docs for using environment variables:
https://docs.docker.com/compose/environment-variables/
Upvotes: 2