Docker Compose: Change env variables
docker, docker-compose
Solution
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:
- One of these methods is to use a `.env` file in the same folder that you execute `docker-compose` form (see https://docs.docker.com/compose/env-file/ for information on it).
- Another option is to interpolate environment variables from your shell. You could specify them such as `- 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.
Problem
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?