Docker-Compose不在.env文件中创建不存在的变量

pprl5pva  于 2022-09-19  发布在  Docker
关注(0)|答案(1)|浏览(182)

这是LARAVEL项目的设置:

文档文件:

FROM php:8.1.2-cli-buster

COPY --from=composer /usr/bin/composer /usr/bin/composer

RUN apt-get update && apt install git -y

WORKDIR /var/www/html

COPY . .

RUN composer install --no-dev

RUN mv ./.env.example .env

RUN php artisan key:generate

CMD ["/bin/bash", "-c", "php artisan serve --host 0.0.0.0 --port 8000"]

查看docker-Compse.yml中的这个变量

version: "3.8"

services:
  artisan:
    build: .
    environment:
        CUSTOM_VAR: custom-value-from-compse

    ports:
      - '8000:8000'

Api.php

Route::get('test', function () {
    dump(env('CUSTOM_VAR')); //CUSTOM_VAR is null
});

上面的路由应该转储custom-value-from-compse值,但它转储了1d1d1e,这是什么问题?它只覆盖.env文件中的现有变量,我的意思是如果我在.env文件中将CUSTOM_VAR设置为‘Some-Value’,它不会覆盖它为docker Compose内部的值

注意:CUSTOM_VAR将为空,即使我将其放入Dockerfile中...

von4xj4u

von4xj4u1#

或许可以试试这个:

version: "3.8"

services:
  artisan:
    build: .
    environment:
      # laravel uses Dotenv to set and load environment variables,by default it will not
      # overwrite existing environment variables. So we set env variables for app container here
      # then laravel will use these env values instead of the same env variables  defined in .env file.
      - "CUSTOM_VAR=custom-value-from-compse"

    ports:
      - '8000:8000'

消息来源:https://gist.github.com/kevinyan815/fa0760902d29f19a4213b4a16fe0501b#file-docker-compose-yml-for-laravel-L11

相关问题