Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

DRY docker-compose

I'm trying to find a more DRY way to use docker-compose env.

docker-compose-base.yml

base:
    image: reactjs_web
    volumes:
        - src:/reactjs/src
        - bin/server:/reactjs/bin/server
        - config:/reactjs/config

docker-compose-prod.yml

svr:
  extends:
    file: docker-compose-base.yml
    service: base
  command: npm run prod:deploy
  ports:
    - "8081:8081"
  environment:
    NODE_ENV: production
    PORT: "8081"
    CLTPORT: "8082"

clt:
  extends:
    file: docker-compose-base.yml
    service: base
  command: npm run prod:deploy:clientside
  ports:
    - "8082:8082"
  environment:
    NODE_ENV: production
    PORT: "8082"
  • The ports and the env port are equals
  • Is there a way to reference the clt port to the svr container ?
like image 741
bdo334 Avatar asked Apr 11 '16 09:04

bdo334


2 Answers

Docker Environment File

Use a .env file and reference it in both containers. This will ensure you only need to store these settings in a single location.

Compose supports declaring default environment variables in an environment file named .env placed in the folder docker-compose command is executed from (current working directory).

Compose expects each line in an env file to be in VAR=VAL format. Lines beginning with # (i.e. comments) are ignored, as are blank lines.

Compose File Integration:

env_file: .env

env_file: 
  - ./common.env
  - ./apps/web.env
  - /opt/secrets.env

Docker Compose File Reference - env_file

Docker Compose Environment File Documentation

like image 180
DevOps Dan Avatar answered Sep 16 '22 11:09

DevOps Dan


You can use environment variable inside docker-compose.

  svr:
    extends:
      file: docker-compose-base.yml
      service: base
    command: npm run prod:deploy
    ports:
      - ${CLTPORT}:${PORT}
    environment:
      NODE_ENV: production

  clt:
    extends:
      file: docker-compose-base.yml
      service: base
    command: npm run prod:deploy:clientside
    ports:
      - ${CLTPORT2}:${PORT2}
    environment:
      NODE_ENV: production

run docker-compose like:

  CLTPORT=8082 PORT=8081 CLTPORT2=8081 PORT2=8082 docker-compose -f docker-compose-prod.yml up

Of course change your port variables as you need.

like image 33
maxo Avatar answered Sep 16 '22 11:09

maxo