1

This is how typically a docker-compose.yml file would be structured:

version: "3.7"
services:
    serv1:
        build: ./serv1
        ports:
            - 4040:40
        env_file:
            - ./docker.env
    serv2:
        build: ./serv2
        ports:
            - 3000:3000
        env_file:
            - ./docker.env
    serv3:
        build: ./serv3
        ports:
            - 5000:5000
        env_file:
            - ./docker.env

I have setup some env variables inside a docker.env file which I would like to use and I'm having two questions:

  1. Trying to use the port on the host, so
serv1:
   ports:
      - "${SERV1_PORT}:40"
   env_file:
      - ./docker.env
...

serv2:
   ports:
      - "${SERV2_PORT}:40"
   env_file:
      - ./docker.env

but when I build I'm getting the following:

WARN[0000] The "SERV1_PORT" variable is not set. Defaulting to a blank string.
  1. Instead of including on each service the env_file: is there a way to specify "globally" the env file on my docker-compose by including it only once? Mean something like this:
services:
  serv1: ...
  serv2: ...
  ...
env_file:
   - ./docker.env # these variables to be accessible in all

3 Answers 3

2

The env_file sets environment variables that are available inside the container, not inside your docker-compose.yml. For that, you want the .env file; read more about Environment variables in docker compose.

Substitute environment variables in Compose files

It’s possible to use environment variables in your shell to populate values inside a Compose file:

web:
   image: "webapp:${TAG}"

If you have multiple environment variables, you can substitute them by adding them to a default environment variable file named .env or by providing a path to your environment variables file using the --env-file command line option.

Sign up to request clarification or add additional context in comments.

4 Comments

Ah I see.. How about the env_file I mention on my second question? Do I only have to include it on each service?
I'm not sure I understand your question. env_file is never required, but if you've got a common set of environment variables you're using it's convenient.
Sorry, let me rephrase that. So if I have a list of env variables I would like to share with all my services I would normally put env_file: - docker/docker.env on each service. Instead of doing this, is there a way I can specify this env file in one place only within docker-compose.yml, but will be accessible through all the services? Just looking to see if there's a DRY pattern I could use and save some lines of code in docker-compose.yml
The linked document really has all the information there is on managing environment variables with compose. There is no configuration that will do exactly what you're asking, but by combining a .env file with appropriate environment sections, you can come to a "single source of truth" for environment variables.
1

On top of the previous valid answer, you can also issue a docker compose config command to see how any ENV has been (or not) substituted as you wished.

Comments

0

Adding to the correct answer I would highly suggest you include "sensible" defaults to the docker compose stack. This will allow your team and other developers to spin up the stack to develop with quicker, while also providing them with the opportunity to customize.

This can be done like:

    environment:
      NODE_ENV: ${NODE_ENV:-development}

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.