I have writte a docker-compose.yml
file to create a multi-container app with nodejs and mongodb (so 2 containers), and I want to make some options configurable, as the choice of server address and port.
To do that, I have written what follows in docker-compose.yml to set them as env variables:
..
web:
environment:
- PORT=3000
- ADDRESS=xxx.xxx.xxx.xxx
..
Within the source code of my application, I use process.env.PORT
and process.env.ADDRESS
to refer to these variables.
But how can I do if I want to change those values, and for example set PORT=3001
?
I have to use docker-compose build
and docker-compose up
again and build all the application, included mongodb container?
I have to use docker-compose build and docker-compose up again and build all the application, included mongodb container?
Not build
, just up
. They are runtime options, not build options. Changing them in the docker-compose.yml
file and then doing a docker-compose up
again should recreate the containers with the new environment variables.
Alternatively, you can specify environment variables outside the docker-compose.yml
file to make for easier changes:
.env
file in the same folder that you execute docker-compose
form (see https://docs.docker.com/compose/env-file/ for information on it).- PORT=${APP_PORT}
and then export APP_PORT=3000
on your shell before running docker-compose
. See https://docs.docker.com/compose/environment-variables/ for more information.If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With