Docker-撰写环境文件无法正常工作 [英] Docker-compose env file not working
问题描述
我正在将docker-compose
文件写到MySQL实例上,并希望使用env文件中的一些变量:这实际上是以下文件:
I'm writing as docker-compose
file to up MySQL instance and want to use few variable from env file: here are the files actually look like:
docker-compose.yml
docker-compose.yml
version: '3.3'
services:
db:
image: mysql
restart: always
env_file:
- ./imran.env
environment:
MYSQL_ROOT_PASSWORD: ${PASS}
ports:
- ${PORT1}: ${PORT2}
imran.env
imran.env
PASS=imran123
PORT1=3306
PORT2=3306
不是正常工作,而是出现以下错误:
Instead of working correct i'm getting following errors:
WARNING: The PASS variable is not set. Defaulting to a blank string.
WARNING: The PORT2 variable is not set. Defaulting to a blank string.
ERROR: The Compose file './docker-compose.yml' is invalid because:
services.db.ports contains unsupported option: '${PORT1}
请帮助
推荐答案
您的docker-compose.yaml
文件中存在一些问题:
You have some issues in your docker-compose.yaml
file:
A:
端口值之间的space
符号.应该没有空格:
A space
symbol between ports values. It should be without a space:
ports:
- ${PORT1}:${PORT2}
B:
您需要在docker-compose.yaml
所在的文件夹中使用 .env 文件为了声明docker-compose.yaml
文件和docker容器的默认环境变量. env_file
部分仅用于将值放入容器.
You need to use .env file in folder where docker-compose.yaml
is in order to declaring default environment variables for both docker-compose.yaml
file and docker container. env_file
section is used to put values into container only.
因此,您应该执行以下操作:
So, you should do the following:
1.
使用ENV变量将文件重命名为.env
:
Re-name file with ENV variables to .env
:
mv imran.env .env
2.
之后使用以下docker-compose.yaml
:
version: '3.3'
services:
db:
image: mysql
restart: always
environment:
MYSQL_ROOT_PASSWORD: ${PASS}
ports:
- ${PORT1}:${PORT2}
这篇关于Docker-撰写环境文件无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!