Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to pass environment variable to docker-compose up

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" 
like image 341
Abhi.G Avatar asked Mar 15 '18 07:03

Abhi.G


People also ask

Does Docker compose override environment variables?

But docker-compose does not stop at the . env and the host's current environment variables. It's cool that you can simply override values of your . env file, but this flexibility is can also be the source of nasty bugs.

Can you use variables in Docker compose file?

Docker Compose allows us to pass environment variables in via command line or to define them in our shell. However, it's best to keep these values inside the actual Compose file and out of the command line.

How do I set an environment variable in docker?

Use -e or --env value to set environment variables (default []). If you want to use multiple environments from the command line then before every environment variable use the -e flag. Note: Make sure put the container name after the environment variable, not before that.

How do I access docker environment variables?

Fetch Using docker exec Command Here, we are executing the /usr/bin/env utility inside the Docker container. Using this utility, you can view all the environment variables set inside Docker containers.


2 Answers

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 
like image 112
Jakub Kukul Avatar answered Sep 18 '22 12:09

Jakub Kukul


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.

like image 20
Sebastian Brosch Avatar answered Sep 16 '22 12:09

Sebastian Brosch