简体   繁体   中英

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM