LoginSignup
3
4

More than 5 years have passed since last update.

Docker laravel redisをつなげてみる

Posted at

はじめに

lumenでRedisをつなげようとした時、うまくいかなかったので、
段階を分けて接続を試みようとした時のメモ

laravelでやってみる

構成

laravel
 |_docker-compose.yml
 |_app.docker
 |_vhost.conf
 |_web.docker
 |_www

yml

docker-compose.yml
version: '2'
services:
    web:
        build:
            context: ./
            dockerfile: web.docker
        volumes:
            - ./www:/var/www
        ports:
            - "8000:80"
        links:
            - app
    app:
        build:
            context: ./
            dockerfile: app.docker
        volumes:
            - ./www:/var/www
        links:
            - redis
    redis:
        image: redis:latest
        ports:
            - "6379:6379"
        command: redis-server --appendonly yes

appサーバー

Dockerfile

app.docker
FROM php:7-fpm

RUN apt-get update && apt-get install -y libmcrypt-dev mysql-client \
    && docker-php-ext-install mcrypt pdo_mysql && \

    #zip,unzip
    apt-get install -y zip unzip && \

    #vim
    apt-get install -y vim && \

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

    #user追加
    useradd -m -s /bin/bash -u 1000 meidaimae && \

    #www.conf修正
    sed -i 's/user\ \=\ www-data/user\ \=\ meidaimae/g' /usr/local/etc/php-fpm.d/www.conf && \
    sed -i 's/group\ \=\ www-data/group\ \=\ meidaimae/g' /usr/local/etc/php-fpm.d/www.conf
WORKDIR /var/www

共有ディレクトリ

mkdir www

webサーバー

Dockerfile

FROM nginx:latest

RUN useradd -m -s /bin/bash -u 1000 meidaimae && \
#nginx.conf
sed -i 's/user\ \ nginx\;/user\ \ meidaimae\;/g' /etc/nginx/nginx.conf

ADD ./vhost.conf /etc/nginx/conf.d/default.conf
WORKDIR /var/www

confファイル

vhost.conf
server {
    listen 80;
    index index.php index.html;
    root /var/www/laravel/public;

    location / {
        try_files $uri /index.php?$args;
    }

    location ~ \.php$ {
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        fastcgi_pass app:9000;
        fastcgi_index index.php;
        include fastcgi_params;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        fastcgi_param PATH_INFO $fastcgi_path_info;
    }
}

起動

docker-compose up -d

プロジェクト作成

#とりま、appサーバーに入る
docker exec -it laravel_app_1 bash

#んで、meidaimaeになってlaravelプロジェクト作成 

chown -R meidaimae:meidaimae /var/www
su meidaimae
composer create-project --prefer-dist laravel/laravel laravel

#redis
cd laravel
composer require predis/predis

#/var/www以下ののディレクトリとファイルのアクセス権を変更
find /var/www -type d -exec chmod 770 {} \;
find /var/www -type f -exec chmod 770 {} \;

redis疎通確認

ping laravel_redis_1 #ここでhostを確認

続けて以下の箇所を.env編集

REDIS_HOST=172.20.0.2#上記で確認したhostを入力
SESSION_DRIVER=redis
CACHE_DRIVER=redis

routesにとりあえず

Route::get('/testCache', function() {
    Cache::put('name', 'aaa',100);

    return Cache::get('name');
});

ブラウザで確認

おわりに

ここまでたどり着くのにめちゃくちゃ長かった。。
ふぅ〜

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