memcached not working in docker-compose

Viewed 13593

I have problem with memcached in docker-compose. This is docker-compose.yml:

nginx:
    container_name: nginx
    image: nginx:latest
    ports:
        - 127.0.0.2:8000:80
    volumes:
        - ./htdocs:/htdocs
        - ./nginx.conf:/etc/nginx/conf.d/nginx.conf
    links:
        - php
php:
    container_name: php
    build: ./php
    volumes:
        - ./htdocs:/htdocs
        - ./php/php.ini:/usr/local/etc/php/php.ini  
    links:
        - mysql
        - memcached
mysql:
    container_name: mysql
    image: mysql:latest
    ports:
        - 127.0.0.2:8001:3306
    volumes:
        - ./my.cnf:/etc/mysql/my.cnf
        - ./db:/var/lib/mysql
    environment:
        - MYSQL_ROOT_PASSWORD=root
        - MYSQL_DATABASE=db
        - MYSQL_USER=root
        - MYSQL_PASSWORD=root
memcached:
    container_name: memcached
    image: memcached:latest
    ports:
        - "11211:11211"

and this is my php code:

error_reporting(E_ALL & ~E_NOTICE);

$memcached = new Memcached; 

$memcached->addServer('0.0.0.0', 11211);

echo '<pre>'; print_r($memcached->getServerList()); echo '</pre>';

if($memcached->getStats() === false) {
    echo 'returned false';
} else {
    echo '<pre>'; print_r($memcached->getStats()); echo '</pre>';
}

and result is:

Array
(
     [0] => Array
        (
            [host] => 0.0.0.0
            [port] => 11211
            [type] => TCP
        )
)

returned false

Why memcached cant see server? (getStats returned nothing) Command "docker ps" return list where it`s running docker memcached:lastest as port "0.0.0.0:11211->11211/tcp". Sorry for my english.

1 Answers

Just replace host 0.0.0.0 to memcached

Host memcached link to IP of memcached container.

Related