I want to start work with docker. I have started nginx, laravel, and mysql containers. But I have a problem. When I want to visit my page I get:
No application encryption key has been specified
In my env file in cointainer I have APP_KEY=base64:isqaS8uLDmiO47hZLTTKmheHM13uBkeA6wTWG1nF5E4=
generated by command php artisan key:generate
from whitch i get Application key set successfully.
.
When I check by php artisan tinker
value env('APP_KEY')
i get only ""
. What’s more, I found that when in .env
file is no APP_KEY
section, this command doesn’t work.
Permissions to env file looks like this:
-rwxr-xr-x 1 www-data www-data 829 Sep 17 12:22 .env
Error from log file
[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} "}
I tried to restart the server, clear the cache and remove and add a new key, but this didn’t help.
EDIT:
This is my 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
And this is my 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:
2
Answers
The problem probably comes from the .env.prod usage.
In this case, .env.prod will be loaded by Docker in the OS container env variables.
And Dotnet primarily uses the environment variables of the OS.
Just to make sure, please try to set the APP_KEY value in .env.prod and restart docker :
docker-compose restart
The package
phpdotenv
looks at your environment variables and in some versions it decides that those variables are to be used instead of the ones in your .env file.List your environment variables to see if
APP_KEY
is set:Unset
APP_KEY
:The source.
If the problem persists, try restarting
php-fpm
.