docker-compose 命令行设置的env文件
docker-compose env file set by command line
所以我想使用自定义 env 文件取决于通过命令行传递的自定义环境变量。
假设我使用 docker-compose 运行 命令开始撰写:
docker-compose run -e ENVIROMENT=local spring-app
然后我想根据环境变量
在我的docker-compose.yml文件中使用我的自定义.env
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
我的docker文件:
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
当我 运行 这个命令时,我的自定义 ENVIROMENT 变量似乎没有被设置并且 docker 仍然想使用默认的 .env 文件。
这是我的错误:
❯ 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
-e
标志是为了将环境变量传递给容器。在你的docker run
命令之前添加你的环境,将环境变量分配给docker引擎,这样环境变量插值就可以完成了
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
顺便说一句,希望您知道 docker-compose run
是为了 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
调出整套容器
所以我想使用自定义 env 文件取决于通过命令行传递的自定义环境变量。
假设我使用 docker-compose 运行 命令开始撰写:
docker-compose run -e ENVIROMENT=local spring-app
然后我想根据环境变量
在我的docker-compose.yml文件中使用我的自定义.envversion: '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
我的docker文件:
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
当我 运行 这个命令时,我的自定义 ENVIROMENT 变量似乎没有被设置并且 docker 仍然想使用默认的 .env 文件。 这是我的错误:
❯ 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
-e
标志是为了将环境变量传递给容器。在你的docker run
命令之前添加你的环境,将环境变量分配给docker引擎,这样环境变量插值就可以完成了
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
顺便说一句,希望您知道 docker-compose run
是为了 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
调出整套容器