简体   繁体   中英

How volume user session in php-fpm container?

My site based on code inside docker container with docker file like:

FROM php:7.1-fpm

RUN apt-get update && apt-get install -y libmcrypt-dev \
    mysql-client libmagickwand-dev --no-install-recommends \
    && pecl install imagick \
    && docker-php-ext-enable imagick \
    && docker-php-ext-install mcrypt pdo_mysql opcache gd

#ADD php.conf/opcache.ini    /etc/php/7.1/mods-available/opcache.ini
ADD build/prod/php.conf/www.conf       /usr/local/etc/php-fpm.d/
ADD build/prod/php.conf/php.ini        /usr/local/etc/php/

COPY .  /app

WORKDIR /app

RUN chmod 777 -R storage/
RUN php artisan cache:clear && php artisan key:generate
RUN nohup php artisan queue:work &

I build CI process just creating new app container with code and kill old. But after each such "recreation", users who were login are logged out. I'm sure that this is due to fact that old container contained PHP sessions, i wanted to add them to volume, but i cloud not found them. My php.ini:

session.save_handler = files
session.use_cookies = 1
session.cookie_path = /

i use Laravel framework, my sessions.php config has:

'driver' => 'file',
'files' => storage_path('framework/sessions'),
'path' => '/',

volume to path framework/sessions not helped

When you run php artisan key:generate , it generates new value for APP_KEY . You can see it's value in .env file (project root)

APP_KEY is used to encrypt and decrypt the session data (and other stuff), so when you generate key again, laravel can't access previous session data.

If you use docker volumes for session files, but APP_KEY changes, sessions files from storage/framework/sessions become useless

This is easy to test locally by logging into your local website, running the command php artisan key:generate , and refreshing the page. You will be logged out.

To solve your problem with sessions use the same APP_KEY every time when you create a container.

You could pass key as environment variable to docker run -e APP_KEY='<key-here>' ... , but you need to enable environment variables

Note: even if you use different session driver (redis, database, memcached, ...), key should stay the same.

A bit more info about the Laravel's Application Key - what it is and how it works?

Laravel installation documentation Application Key

More about the laravel sessions

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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