1

I am trying to setup an existing project with Docker. The project is in codeigniter and uses memcached as the driver to set the session.

I created a Docker environment with php:7.1.8-apache and installed all the neccesary packages.

I am now getting this error on the page

Warning Message: session_start(): Failed to read session data: user (path: localhost:11211:128)

I searched this on the internet a lot and tried out many things but nothing works.

I have a file in application/libraries/Session/drivers/Session_memcached_driver.php

This is the the piece of code from where the error is generating but i am not sure what exactly it is.

public function read($session_id)
    {
        if (isset($this->_memcached) && $this->_get_lock($session_id))
        {
            // Needed by write() to detect session_regenerate_id() calls
            $this->_session_id = $session_id;

            $session_data = (string) $this->_memcached->get($this->_key_prefix.$session_id);
            $this->_fingerprint = md5($session_data);
            return $session_data;
        }

        return $this->_fail();
    }

Here are my Docker Configurations:

FROM php:7.1.8-apache

# AUTHOR
MAINTAINER Development

# INSTALLING PACKAGES
RUN apt-get update && apt-get install -y \
    git \
    pkg-config \
    build-essential \
    libmemcached-dev \
    libmemcached-tools \
    libicu-dev \
    libmcrypt-dev \
    libfreetype6-dev \
    libjpeg62-turbo-dev \
    libfreetype6 \
    libfontconfig \
    libxml2-dev \
    vim \
    nano \
    mysql-client \
    libldap2-dev \
    wget

RUN apt-get clean

# Extension PHP
RUN docker-php-ext-install pdo pdo_mysql
RUN docker-php-ext-install intl
RUN docker-php-ext-install mbstring
RUN docker-php-ext-install bcmath
RUN docker-php-ext-install mcrypt
RUN docker-php-ext-install zip
RUN docker-php-ext-install exif
RUN docker-php-ext-install soap
RUN docker-php-ext-install opcache
RUN docker-php-ext-install iconv
RUN docker-php-ext-configure gd --with-freetype-dir=/usr/include/ --with-jpeg-dir=/usr/include/
RUN docker-php-ext-install gd
RUN docker-php-ext-install mysqli && docker-php-ext-enable mysqli

# APCu
RUN pecl channel-update pecl.php.net
RUN pecl install apcu
RUN echo "extension=apcu.so" > /usr/local/etc/php/conf.d/apcu.ini

# Enable PHP 7
RUN a2enmod rewrite && a2enmod headers && a2enmod expires
RUN a2enmod php7
RUN echo "ServerName localhost" >> /etc/apache2/apache2.conf

# SETUP VIRTUAL HOST
COPY .docker/sites-available/default-vhost.conf /etc/apache2/sites-available/default-vhost.conf
COPY .docker/sites-available/my-vhost.conf /etc/apache2/sites-available/my-vhost.conf
RUN a2dissite 000-default.conf
RUN a2ensite default-vhost.conf
RUN a2ensite my-vhost.conf

# Composer
RUN curl -sS https://getcomposer.org/installer | php
RUN mv composer.phar /usr/local/bin/composer

# Memcached
RUN apt-get update
RUN apt-get install -y pkg-config build-essential libmemcached-dev
RUN git clone https://github.com/php-memcached-dev/php-memcached.git
RUN cd php-memcached && git checkout php7 && phpize && ./configure --disable-memcached-sasl && make && make install
RUN echo 'extension = memcached.so' > /usr/local/etc/php/conf.d/memcached.ini

# ALIASES
RUN echo "alias ll='ls -l'" >> /root/.bashrc
RUN echo "alias la='ls -l'" >> /root/.bashrc

# COPY THE WORKING DIRECTORY
COPY . /var/www/myproject

# MANAGE DIRECTORY PERMISSIONS
RUN chown -R www-data:www-data /var/www/myproject

# SERVER RESTART
RUN service apache2 restart

And this is docker-compose.yml settings

version: '3'

services:
  app:
    build:
      context: .
      dockerfile: .docker/Dockerfile
    container_name: gaz-container
    image: gaz-docker
    ports:
      - "80:80"
    volumes:
      - .:/var/www/gaz
    links:
      - memcached:memcached
      - mysql:mysql

  memcached:
    image: memcached
    ports:
      - "11211:11211"

  mysql:
    container_name: mysql
    image: mysql:5.6
    ports:
      - "3306:3306"
    environment:
      - MYSQL_ROOT_PASSWORD=password

Can anyone please direct me what could be the issue here?

CodeThing
  • 2,580
  • 2
  • 12
  • 25
  • What's the permissions on the file? Also, check the permissions on the Session folder. – Niall Lonergan Oct 18 '18 at 13:14
  • The sessions are not stored inside file or folder on server. This is inside codeignitor and the sess_driver is set to memcached – CodeThing Oct 18 '18 at 13:17
  • Have you seen this? https://stackoverflow.com/questions/48245296/php-custom-session-handler-problems-php-7-1?rq=1 – Niall Lonergan Oct 18 '18 at 13:18
  • @NiallLonergan Yes, i have already gone through with that. Basically they are using database to store the session. In my case its memcached. – CodeThing Oct 18 '18 at 13:26
  • You don't seem to be using CI's internal session management through `$this->session` and using native PHP session calls. This causes CI code to act up sometimes... have you tried using CI's sessions? (you can use them with memcached) – Javier Larroulet Oct 18 '18 at 13:43
  • @JavierLarroulet I am indeed using CI's session management. For instance check this screenshot which checks the session after login https://www.screencast.com/t/2Sx4AGakwH6 – CodeThing Oct 18 '18 at 14:02

1 Answers1

3

OMG.

After doing lot of searching i finally managed to get this working.

Thanks to dukebody. This post helped me to get hints about what i was doing wrong. https://stackoverflow.com/a/27657937/4342075

The thing is i am actually creating multiple docker containers as you can see in the question in docker-compose.yml file. One of which is memcached. And i was trying to use memcached in the main container which is app.

What i was doing wrong is, i was using localhost:11211 to access memcached.

I just changed

$config['sess_save_path'] = 'localhost:11211:128'

To

$config['sess_save_path'] = 'memcached:11211';

And guess what it worked!!!

CodeThing
  • 2,580
  • 2
  • 12
  • 25