docker .env file reuse declared variables

When I run docker-compose build I have a .env file to be used for building environment variables on my docker-machine like this:

COMPOSE_PROJECT_NAME=radar
ENV=production
DB_NAME=postgres
DB_USER=postgres
DB_PASS=sho1c2ov3Phezaimah7eb2Tii4ohkah8k
DB_SERVICE=postgres
DB_PORT=5432
C_FORCE_ROOT="true"
PGHOST=postgres
PGDATABASE=postgres
PGUSER=postgres
PGPASSWORD=sho1c2ov3Phezaimah7eb2Tii4ohkah8k
PGPORT=5432

If you have noticed the redundancy like the ‘DB_NAME’ and ‘PGDATABASE’ is the same.. Is there a way to avoid this?

  • Dynamic DNS Resolution with HAProxy and Docker
  • How do apply Puppet class locally
  • Wildfly: AMQ214016: Failed to create netty connect ion java.nio.channels.UnresolvedAddressException from client on natted network
  • How can I attach VS Code to a node process running in a docker container
  • How to get infos about ourself in a compose cluster
  • Console App or Windows Service in Windows Container?
  • docker copy file from one container to another?
  • Deploy ASP.NET core to Azure Container Service (Swarm mode)
  • Docker-machine : How to exec an ssh command correctly?
  • boot2docker: port forwording to manage spark workers through web UI on Mac OS
  • Troubleshoot docker connectivity to external network
  • Why is Docker Secrets more secure than environment variables?
  • One Solution collect form web for “docker .env file reuse declared variables”

    What about doing something like this in your Dockerfile?:

    ARG DB_NAME
    ENV DB_NAME=${DB_NAME} PGDATABASE=${DB_NAME} ...
    

    Test

    $ cat Dockerfile
    FROM alpine:3.4
    
    ARG DB_NAME
    ENV DB_NAME=${DB_NAME} PGDATABASE=${DB_NAME}
    
    RUN env
    

    Compose File Version 2 or higher!

    $ cat docker-compose.yml
    version: '3' #'2' should work as well
    
    services:
      docker-arg-env:
        build:
          context: .
          args:
            - DB_NAME
        command: env
    

    $ cat .env
    DB_NAME=world
    

    $ docker-compose build
    Building docker-arg-env
    Step 1/4 : FROM alpine:3.4
     ---> 0766572b4bac
    Step 2/4 : ARG DB_NAME
     ---> Running in a56be8426dd5
     ---> 4b1009ba9fad
    Removing intermediate container a56be8426dd5
    Step 3/4 : ENV DB_NAME ${DB_NAME} PGDATABASE ${DB_NAME}
     ---> Running in 5bbdd40e640e
     ---> 593105981a2a
    Removing intermediate container 5bbdd40e640e
    Step 4/4 : RUN env
     ---> Running in dadf204c7497
    HOSTNAME=26ba10d264c2
    SHLVL=1
    HOME=/root
    PGDATABASE=world
    DB_NAME=world
    PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
    PWD=/
     ---> 9632ee1e0e37
    Removing intermediate container dadf204c7497
    Successfully built 9632ee1e0e37
    

    $ docker-compose up
    Creating network "dockerargenv_default" with the default driver
    Creating dockerargenv_docker-arg-env_1
    Attaching to dockerargenv_docker-arg-env_1
    docker-arg-env_1  | PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
    docker-arg-env_1  | HOSTNAME=5a6b51f4ecc7
    docker-arg-env_1  | DB_NAME=world
    docker-arg-env_1  | PGDATABASE=world
    docker-arg-env_1  | HOME=/root
    

    As you can see, PG_NAME and PGDATABASE are set during build-time and run-time.

    Without Compose:

    docker build --build-arg DB_NAME=world . and docker run docker-arg-env:latest env produce the same result.

    Docker will be the best open platform for developers and sysadmins to build, ship, and run distributed applications.