简体   繁体   English

无法在 docker 组合中插入环境变量

[英]Cannot interpolate environment variable in docker compose

I have a simple docker compose file to create a Mysql database for my app.我有一个简单的 docker 撰写文件来为我的应用程序创建一个 Mysql 数据库。 But I cannot interpolate the environment variable MYSQL_PORT to set a custom port.但是我无法插入环境变量MYSQL_PORT来设置自定义端口。 Running docker compose up with the configuration below results in a random port being assigned to mysql.运行docker compose up下面的配置会导致一个随机端口被分配给 mysql。

The path to the env file does work, since I have env variables configuring the database. env 文件的路径确实有效,因为我有配置数据库的 env 变量。

docker-compose.yml docker-compose.yml

version: '3'
services:
  mysql:
    image: mysql
    command: --default-authentication-plugin=mysql_native_password
    restart: always
    volumes:
      - mysql_data:/var/lib/mysql
    env_file:
      - ../../.env
    ports:
      - ${MYSQL_PORT}:3306

volumes:
  mysql_data:

.env .env

MYSQL_PORT=3306
MYSQL_ROOT_PASSWORD=root
MYSQL_DATABASE=final_project_database
MYSQL_USER=db_user
MYSQL_PASSWORD=some_db_user_password

Use --env-file option with docker-compose up command.--env-file选项与docker-compose up命令一起使用。 env_file declared in your MySQL service applies only for container env在您的 MySQL 服务中声明的 env_file 仅适用于容器环境

Move your.env file to the same directory as the docker-compose file and change the env_file to point to it.将 your.env 文件移动到与 docker-compose 文件相同的目录,并将env_file更改为指向它。 That way both docker-compose and the container will use the same environment file.这样 docker-compose 和容器都将使用相同的环境文件。

Right now it's only the container that's using it.现在只有容器在使用它。

version: '3'
services:
  mysql:
    image: mysql
    command: --default-authentication-plugin=mysql_native_password
    restart: always
    volumes:
      - mysql_data:/var/lib/mysql
    env_file:
      - ./.env
    ports:
      - ${MYSQL_PORT}:3306

volumes:
  mysql_data:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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