docker 容器中未指定应用程序加密密钥

Posted

技术标签:

【中文标题】docker 容器中未指定应用程序加密密钥【英文标题】:No application encryption key has been specified in docker container 【发布时间】:2021-01-04 08:20:12 【问题描述】:

我想开始使用 docker。我已经启动了 nginx、laravel 和 mysql 容器。但是我有问题,当我想访问我的页面时,我得到:

No application encryption key has been specified

在我在 cointainer 中的 env 文件中,我有 APP_KEY=base64:isqaS8uLDmiO47hZLTTKmheHM13uBkeA6wTWG1nF5E4= 由命令 php artisan key:generate 生成,我得到了 Application key set successfully.

当我检查php artisan tinkerenv('APP_KEY') 时,我只得到""。更重要的是,我发现当.env 文件中没有APP_KEY 部分时,此命令不起作用。

对 env 文件的权限如下所示: -rwxr-xr-x 1 www-data www-data 829 Sep 17 12:22 .env

来自日志文件的错误

[2020-09-17 12:23:19] local.ERROR: No application encryption key has been specified. "exception":"[object] (RuntimeException(code: 0): No application encryption key has been specified. at /var /www/vendor/laravel/framework/src/Illuminate/Encryption/EncryptionServiceProvider.php:44) [stacktrace] #0 /var/www/vendor/laravel/framework/src/Illuminate/Support/helpers.php(422): Illuminate\\Encryption\\EncryptionServiceProvider->Illuminate\\Encryption\\closure('') #1 /var/www/vendor/laravel/framework/src/Illuminate/Encryption/EncryptionServiceProvider.php(48): tap('', Object(Closure)) #2 /var/www/vendor/laravel/framework/src/Illuminate/Encryption/EncryptionServiceProvider.php(24): Illuminate\\Encryption\\EncryptionServiceProvider->key(Array) #3 /var/www/vendor/laravel/framework/src/Illuminate/Container/Container.php(800): Illuminate\\Encryption\\EncryptionServiceProvider->Illuminate\\Encryption\\closure(Object(Illuminate\\Foundat ion\\Application), Array) #4 /var/www/vendor/laravel/framework/src/Illuminate/Container/Container.php(682): Illuminate\\Container\\Container->build(Object(Closure)) #5 /var/www/vendor/laravel/framework/src/Illuminate/Container/Container.php(630): Illuminate\\Container\\Container->resolve('encrypter', Array) #6 /var/www/vendor/laravel/framework/src/Illuminate/Foundation/Application.php(768): Illuminate\\Container\\Container->make('encrypter', Array) #7 /var/www/vendor/laravel/framework/src/Illuminate/Container/Container.php(946): Illuminate\\Foundation\\Application->make('encrypter') #8 /var/www/vendor/laravel/framework/src/Illuminate/Container/Container.php(874): Illuminate\\Container\\Container->resolveClass(Object(ReflectionParameter)) #9 /var/www/vendor/laravel/framework/src/Illuminate/Container/Container.php(835): Illuminate\\Container\\Container->resolveDependencies(Array) #10 /var/www/vendor/laravel/framework/src/Illuminate/Container/Container.php(682): Illuminate\\Container\\Container->build('App\\\\Http\\\\Middle...') #11 /var/www/vendor/laravel/framework/src/Illuminate/Container/Container.php(630): Illuminate\\Container\\Container->resolve('App\\\\Http\\\\Middle...', Array) #12 /var/www/vendor/laravel/framework/src/Illuminate/Foundation/Application.php(768): Illuminate\\Container\\Container->make('App\\\\Http\\\\Middle...', Array) #13 /var/www/vendor/laravel/framework/src/Illuminate/Foundation/Http/Kernel.php(215): Illuminate\\Foundation\\Application->make('App\\\\Http\\\\Middle...') #14 /var/www/vendor/laravel/framework/src/Illuminate/Foundation/Http/Kernel.php(189): Illuminate\\Foundation\\Http\\Kernel->terminateMiddleware(Object(Illuminate\\Http\\Request), Object(Illumin ate\\Http\\Response)) #15 /var/www/public/index.php(60): Illuminate\\Foundation\\Http\\Kernel->terminate(Object(Illuminate\\Http\\Request), Object(Illuminate\\Http\\Response)) #16 main "

我尝试重新启动服务器,清除缓存并删除和添加新密钥,但这没有帮助。

编辑: 这是我的 dockerfile

FROM php:7.4.0-fpm

COPY composer.lock composer.json /var/www/

COPY database /var/www/database

WORKDIR /var/www

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

RUN curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer

COPY . /var/www 

RUN chown -R www-data:www-data \
        /var/www/storage \
        /var/www/bootstrap/cache

RUN  apt-get install -y libmcrypt-dev \
        libmagickwand-dev --no-install-recommends \
        && pecl install mcrypt-1.0.3 \
        && docker-php-ext-install pdo_mysql \
        && docker-php-ext-enable mcrypt

RUN mv .env.prod .env

RUN composer install

RUN chown -R www-data:www-data /var/www

RUN chown -R $USER:www-data storage \
        && chown -R $USER:www-data bootstrap/cache\
        && chmod -R 775 storage \
        && chmod -R 775 bootstrap/cache

RUN php artisan key:generate

这是我的docker-compose.yml

version: '3.6'

services: 

    app:
        container_name: laravel_app
        build: 
            context: ./
            dockerfile: development/app.dockerfile
        volumes: 
            - ./storage:/var/www/storage
        env_file: '.env.prod'
        environment: 
            - "DB_HOST=database"
            - "REDIS_HOST=cache"

    web:
        container_name: nginx_server
        build: 
            context: ./
            dockerfile: development/web.dockerfile
        volumes: 
            - ./storage/logs/:/var/log/nginx_server
        ports: 
            - 8990:80
        
    database:
        container_name: mysql_database
        image: mysql:8.0
        volumes: 
            - dbdata:/var/lib/mysql
        environment: 
            - "MYSQL_DATABSE=Baly"
            - "MYSQL_USER=admin"
            - "MYSQL_PASSWORD=admin"
            - "MYSQL_ROOT_PASSWORD=root"
        ports: 
            - 8991:3006
    
    cache:
        image: redis:3.0-alpine

volumes: 
    dbdata:

【问题讨论】:

请添加您的 dockerfile 抱歉,我忘记了。添加到编辑部分。 【参考方案1】:

问题可能来自 .env.prod 的使用。

在这种情况下,.env.prod 将被 Docker 加载到 OS 容器 env 变量中。

而Dotnet主要使用操作系统的环境变量。

为了确定,请尝试在 .env.prod 中设置 APP_KEY 值并重新启动 docker :docker-compose restart

【讨论】:

以上是关于docker 容器中未指定应用程序加密密钥的主要内容,如果未能解决你的问题,请参考以下文章

Docker中未指定挂载点容器间volume卷的数据共享

尝试从 docker 容器中使用 SSH 密钥 [重复]

未指定应用程序加密密钥(反应)

随机“未指定应用程序加密密钥”

错误:未指定应用程序加密密钥。但它存在

加密webconfig中的连接字符串,利用RSA非对称加密,利用windows保存密钥容器