1

We have something like this in our docker-compose file:

services:
   myapp:
      build: .
      image: my-app-image:uat
      ports:
      - "8080:8080"
      environment:
        - SERVER_SERVLET-PATH=/
        - SERVER_PORT=80
        - more variables.....

What comes after environment are lots and lots of variables that our application needs. This makes in quite a hassle to look and examine the file due to the high number of variables. Also makes it look kinda dirty. Is there a way to place all environment variables in another file and just reference it?

Sorry i'm just new to docker. Tried looking for this in the docker site but couldn't find any.

Help would be appreciated. Thanks

2 Answers2

2

Well, one solution is having an .env file in the same folder where your docker compose is: https://docs.docker.com/compose/env-file/

PassionateDeveloper
  • 14,558
  • 34
  • 107
  • 176
1

You can use YAML anchors (I think that's the term) to define shared hunks for reference within a single file.

# common pieces
x-common-params
environment:
  - SERVER_SERVLET-PATH=/
  - SERVER_PORT=80
  ... more ...


services:
myapp:
   build: .
   image: my-app-image:uat
   ports:
   - "8080:8080"
   <<: *common-params

If you need to share across files it isn't directly supported but can still be done a number of different ways.

In my case I have a single file that contains all the shared pieces. Then in the stack file (compose file in your case) I put jinja2 tags to be filled in. Then I use the "j2" command line utility to fill them in during my build and deploy the generated file.

The podman project has added a c preprocessor step so you can use "#include". I know it's not straight docker, but seems pretty effective.

fishyjoes
  • 143
  • 1
  • 7