Dry docker

I am trying to find a more severe 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" 
  • Ports and env port are equal
  • Is there any way to refer to clt port on svr container?
+6
source share
3 answers

Docker Environment File

Use the .env file and specify it in both containers. This ensures that you only need to save these settings in one place.

Generate alerts declaring default environment variables in an environment file named .env located in a folder. The docker-compose command is executed from (the current working directory).

Compose expects each line in the env file to be in the format VAR = VAL. Lines beginning with C # (i.e. Comments) are ignored, as are empty lines.

Compile file integration:

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

Docker Compage File Link - env_file

Docker Compage Documentation Documentation

+2
source

You can reference an environment variable to solve this problem. https://github.com/docker/compose/issues/1377

0
source

You can use the 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 the port variables as you need.

0
source

All Articles