LoginSignup
3
4

More than 3 years have passed since last update.

Docker PHP memcached追加

Posted at

DockerのPHPでmemcachedを利用できるようにする方法

構成

---docker
|--docker-compose.yml
|--docker
   |--nginx
   |    |--nginx.conf
   |
   |--phpfpm
   |    |--Dockerfile
   |    |--php.ini
   |
   |--memcached
        |--Dockerfile

docker-compose.yml

version: '3'

services:
  memcached:
    container_name: memcached
    build: ./docker/memcached
    #image: memcached:latest
    ports:
        - 11211:11211

  nginx:
    container_name: nginx
    build: ./docker/nginx
    #image: nginx:alpine
    restart: always
    ports:
        - 80:80
    volumes:
        - ./docker/nginx/conf.d/default.conf:/etc/nginx/conf.d/default.conf
        - ./docker/nginx/nginx.conf:/etc/nginx/nginx.conf
        - ./volumes/nginx/:/var/log/nginx
    depends_on:
        - phpfpm

  phpfpm:
    container_name: phpfpm
    build: ./docker/phpfpm
    #image: php:7.3-fpm-alpine
    restart: always
    volumes:
        - ./volumes/www/html:/var/www/html
        - ./docker/phpfpm/php.ini:/usr/local/etc/php/php.ini
        - ./docker/phpfpm/php-fpm.conf:/usr/local/etc/php-fpm.conf

phpfpm/Dockerfile

FROM php:7.3-fpm-alpine

RUN apk update \
&& apk add \
    autoconf \
    libmemcached-dev \
    zlib-dev \
    gcc \
    g++ \
    make \
&& pecl install memcached

phpfpm/php.ini

extension=memcached.so

memcached/Dockerfile

FROM memcached:latest

/volumes/www/html/index.php

<?php

//phpinfo();exit;
$results = array();
$mc = new Memcached();

"memcached"はdocker-compose.ymlのcontainer_nameを指定する
$mc->addServer("memcached", 11211);
$mc->set("test1", "hoge");
$mc->get("test1");

?>
3
4
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
4