Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to specify multiple "env_files" for a docker compose service?

Currently I have setup my service like the following.

version: '3'
services:
  gateway:
    container_name: gateway
    image: node:lts-alpine
    working_dir: /
    volumes:
      - ./package.json:/package.json
      - ./tsconfig.json:/tsconfig.json
      - ./services/gateway:/services/gateway
      - ./packages:/packages
      - ./node_modules:/node_modules
    env_file: .env
    command: yarn run ts-node-dev services/gateway --colors
    ports:
      - 3000:3000

So I have specified one env_file. But now I want to pass multiple .env files.

Unfortunately, the following is not possible:

    env_files:
       - .env.secrets
       - .env.development

Is there any way to pass multiple .env files to one service in docker-compsoe?

like image 825
Flo Avatar asked Jul 04 '20 10:07

Flo


People also ask

How do I handle multiple files in Docker compose?

To use multiple override files, or an override file with a different name, you can pass the -f option to the docker-compose up command. The base Compose file has to be passed on the first position of the command.

Does Docker compose run multiple containers?

With Docker compose, you can configure and start multiple containers with a single yaml file.

What is Depends_on in Docker compose?

depends_on is a Docker Compose keyword to set the order in which services must start and stop. For example, suppose we want our web application, which we'll build as a web-app image, to start after our Postgres container.


Video Answer


2 Answers

You can specify multiple env files on the env_file option (without s).

For instance:

version: '3'

services:
  hello:
    image: alpine
    entrypoint: ["sh"]
    command: ["-c", "env"]
    env_file:
      - a.env
      - b.env
like image 114
conradkleinespel Avatar answered Jan 02 '23 09:01

conradkleinespel


Note that, complementary to @conradkleineespel's answer, if an environment variable is defined in multiple .env files listed under env_file, the value found in the last environment file in the list overwrites all prior (tested in a docker-compose file with version: '3.7'.

like image 42
Ahmet Polat Avatar answered Jan 02 '23 10:01

Ahmet Polat