Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

docker-compose env file set by command line

So i want to use a custom env file depends on a custom enviroment variable passed via command line.

Let's say that i start my compose with docker-compose run command:

docker-compose run -e ENVIROMENT=local spring-app

then i want to use my custom .env in my docker-compose.yml file based on ENVIROMENT variable

version: '2.1'
services:

  spring-app:
    build: .
      depends_on:
        docker-mariadb:
          condition: service_healthy
    links:
      - docker-mariadb
    ports:
      - 8080:8080
    environment:
      - SPRING_PROFILES_ACTIVE
      - DATABASE_HOST
      - DATABASE_USER
      - DATABASE_PASSWORD
      - DATABASE_NAME
      - DATABASE_PORT
    env_file:
      - ${ENVIROMENT}.env

  docker-mariadb:
    image: mariadb:latest
    restart: always
    environment:
      - MYSQL_ROOT_PASSWORD=${DATABASE_PASSWORD}
      - MYSQL_DATABASE=${DATABASE_NAME}
      - MYSQL_PASSWORD=${DATABASE_PASSWORD}
    healthcheck:
      test: "/usr/bin/mysql --user=${DATABASE_USER} --password=${DATABASE_PASSWORD} --execute \"SHOW DATABASES;\""
      interval: 30s
      timeout: 10s
      retries: 5
    env_file:
      - ${ENVIROMENT}.env

My dockerfile:

FROM openjdk:10-jre-slim
VOLUME /tmp
ARG JAR_FILE="build/libs/spring-app-0.0.1-SNAPSHOT.jar"
COPY ${JAR_FILE} spring-app.jar
ENTRYPOINT ["java","-Djava.security.egd=file:/dev/./urandom","-jar","/spring-app.jar"]

local.env:

SPRING_PROFILES_ACTIVE=prod
DATABASE_HOST=docker-mariadb
DATABASE_USER=root
DATABASE_PASSWORD=xxx
DATABASE_NAME=xxx
DATABASE_PORT=3306

When i run this command my custom ENVIROMENT variable seems not to be set and docker still want to use default .env file. This is my error:

❯ docker-compose run -e ENVIROMENT=local spring-app
WARNING: The ENVIROMENT variable is not set. Defaulting to a blank string.
WARNING: The DATABASE_PASSWORD variable is not set. Defaulting to a blank string.
WARNING: The DATABASE_NAME variable is not set. Defaulting to a blank string.
WARNING: The DATABASE_USER variable is not set. Defaulting to a blank string.
ERROR: Couldn't find env file: /Users/user/Desktop/docker-example/.env
like image 631
user3361149 Avatar asked Aug 22 '18 19:08

user3361149


People also ask

How can you set environment variables from the command line?

To set an environment variable, use the command " export varname=value ", which sets the variable and exports it to the global environment (available to other processes). Enclosed the value with double quotes if it contains spaces. To set a local variable, use the command " varname =value " (or " set varname =value ").

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.

Can I use environment variables in Docker compose file?

If you need to define various configuration values, environment variables are your best friend. Like many tools, Docker and, more specifically, Docker Compose can both define and read environment variables to help keep your configurations clean and modular.


1 Answers

The -e flag is meant to pass the environment variables to the container. Add your environment before your docker run command to assign the environment variable to the docker engine so that the environment variable interpolation can be done

docker-compose run -e ENVIROMENT=local spring-app
[...]
ERROR: Couldn't find env file: /Users/sabhat/code/scratch/.env

ENVIROMENT=local docker-compose run spring-app
[...]
Starting scratch_docker-mariadb_1

As an aside, hope you are aware that docker-compose run is meant to run a one-time command for a service - and it doesn't map the ports and also overrides the run commands defined in the service. You should be using docker-compose up to bring up the entire set of containers

like image 128
Sathyajith Bhat Avatar answered Nov 15 '22 03:11

Sathyajith Bhat