简体   繁体   English

使用env_file在撰写文件中设置变量时发出警告

[英]Warning when setting variables in compose file with env_file

In my Docker/Laravel 5 project I'm trying to rename .env file of Docker(I have similar file in a Laravel project), so I added 2 lines to docker-compose.yml : 在我的Docker / Laravel 5项目中,我试图重命名Docker的.env文件(我在Laravel项目中有类似的文件),因此我在docker docker-compose.yml添加了两行:

version: '3.1'

services:

    web:
        env_file:
            ./docker_app.env

        build:
            context: ./web
            dockerfile: Dockerfile.yml

        environment:
            - APACHE_RUN_USER=www-data
        volumes:
            - ${APP_PATH_HOST}:${APP_PTH_CONTAINER}

        ports:
            - 8081:80
        working_dir: ${APP_PTH_CONTAINER}


    db:
        image: mysql:5.7.24
        restart: always
        environment: 
            MYSQL_ROOT_PASSWORD: 1
        volumes:
            - ${DB_PATH_HOST}:/var/lib/mysql

but when I'm running the build command I get an error: 但是当我运行构建命令时,出现错误:

$ docker-compose up -d --build
WARNING: The APP_PATH_HOST variable is not set. Defaulting to a blank string.
WARNING: The APP_PTH_CONTAINER variable is not set. Defaulting to a blank string.
WARNING: The DB_PATH_HOST variable is not set. Defaulting to a blank string.
...
Step 2/3 : RUN apt-get update &&     apt-get install -y     libfreetype6-dev     libwebp-dev     libjpeg62-turbo-dev     libpng-dev     nano     libgmp-dev     libldap2-dev     netcat     sqlite3     git     libsqlite3-dev &&     docker-php-ext-configure gd --with-freetype-dir=/usr/include/ --with-webp-dir=/usr/include/  --with-jpeg-dir=/usr/include/ &&     docker-php-ext-install gd pdo pdo_mysql pdo_sqlite zip gmp bcmath pcntl ldap sysvmsg exif && a2enmod rewrite
 ...

docker_app.env contents are: docker_app.env内容为:

DB_PATH_HOST=./databases

APP_PATH_HOST=./SiteApp

APP_PTH_CONTAINER=/var/www/html/

docker-compose.yml , docker_app.env and SiteApp subdirectory are in the same root directory docker-compose.ymldocker_app.envSiteApp子目录位于同一根目录中

If I set an invalid file for env_file parameter in docker-compose.yml will I get another error? 如果我在env_file docker-compose.ymlenv_file参数设置了无效文件, docker-compose.yml会出现另一个错误?

How to fix this error? 如何解决这个错误?

Thanks! 谢谢!

The variables in the docker-compose file is not the environment variables in the container , they are env from your working shell (on the host). docker-compose文件中的变量不是 容器中的环境变量,它们是从您的工作外壳(主机上)中得到的env。 Docker reads these variables from your host env and substitutes them in the docker-compose file. Docker从您的主机环境中读取这些变量,并将其替换为docker-compose文件。

You need to export your variables in the docker_app.env. 您需要将变量导出到docker_app.env中。

A quick way to do this is 一种快速的方法是

source docker_app.env
export $(cut -d= -f1 docker_app.env)

(credit to https://unix.stackexchange.com/questions/79064/how-to-export-variables-from-a-file ) (信用https://unix.stackexchange.com/questions/79064/how-to-export-variables-from-a-file

PS 聚苯乙烯

You can use docker-compose config to see substituted result. 您可以使用docker-compose config查看替代结果。

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

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