How to pass environment variable to docker-compose up

DockerDocker ComposeDockerfile

Docker Problem Overview


I am trying to run a container. I already have the image uploaded to private Docker registry. I want to write a compose file to download and deploy the image. But I want to pass the TAG name as a variable from the docker-compose run command.My compose file looks like below. How can I pass the value for KB_DB_TAG_VERSION as part of docker-compose up command?

version: '3'
services:
   db:
    #build: k-db
    user: "1000:50"
    volumes:
      - /data/mysql:/var/lib/mysql
    container_name: k-db
    environment:
      - MYSQL_ALLOW_EMPTY_PASSWORD=yes
    image:  XX:$KB_DB_TAG_VERSION
    image: k-db
    ports:
      - "3307:3306"

Docker Solutions


Solution 1 - Docker

You have two options:

  1. Create the .env file as already suggested in another answer.

  2. Prepend KEY=VALUE pair(s) to your docker-compose command, e.g:

    KB_DB_TAG_VERSION=kb-1.3.20-v1.0.0 docker-compose up
    

Exporting it earlier in a script should also work, e.g.:

    export KB_DB_TAG_VERSION=kb-1.3.20-v1.0.0
    docker-compose up

Solution 2 - Docker

You can create a .env file on the directory where you execute the docker-compose up command (and your docker-compose.yml file is located) with the following content:

KB_DB_TAG_VERSION=kb-1.3.20-v1.0.0

Your docker-compose.yml file should look like the following (added { and }):

version: '3'
services:
   db:
     user: "1000:50"
     volumes:
       - /data/mysql:/var/lib/mysql
     container_name: k-db
     environment:
       - MYSQL_ALLOW_EMPTY_PASSWORD=yes
     image: XX:${KB_DB_TAG_VERSION}
     image: k-db
     ports:
       - "3307:3306"

After making the above changes , check whether the changes are reflected or not using the command docker-compose config. The variable will be replaced by the variable value. Please refer to the page here to understand more about variable replacement.

Solution 3 - Docker

Just to supplement what has been outlined by others, in particular by @JakubKukul

For security purposes you probably wouldn't want to keep vulnerable information such as username/password in your docker-compose files if they're under version control. You can map environment variables that you have on your host to environment variables inside container as well. In this case it could be something like the following:

version: '3'
services:
   db:
    #build: k-db
    user: "1000:50"
    volumes:
      - /data/mysql:/var/lib/mysql
    container_name: k-db
    environment:
      - MYSQL_ALLOW_EMPTY_PASSWORD=yes
      - MYSQL_PASSWORD=${MYSQL_PASSWORD}
    image:  XX:$KB_DB_TAG_VERSION
    image: k-db
    ports:
      - "3307:3306"

where MYSQL_PASSWORD would be both:

  1. An environment variable on your host (maybe just in the current shell session)
  2. An environment variable inside the containers from the db service

Solution 4 - Docker

In your docker-compose.yml file add

env_file:
  - .env_file

to your db service where .env_file is your .env file (change its name accordingly).

version: '3'
services:
   db:
    #build: k-db
    user: "1000:50"
    volumes:
      - /data/mysql:/var/lib/mysql
    container_name: k-db
    env_file:
      - .env_file
    environment:
      - MYSQL_ALLOW_EMPTY_PASSWORD=yes
    image:  XX:$KB_DB_TAG_VERSION
    image: k-db
    ports:
      - "3307:3306"

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionAbhi.GView Question on Stackoverflow
Solution 1 - DockerJakub KukulView Answer on Stackoverflow
Solution 2 - DockerSebastian BroschView Answer on Stackoverflow
Solution 3 - DockervasigorcView Answer on Stackoverflow
Solution 4 - Dockeruser1734227View Answer on Stackoverflow