Docker-撰写环境文件无法正常工作 [英] Docker-compose env file not working

查看:248
本文介绍了Docker-撰写环境文件无法正常工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在将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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆