🗂

[ Docker ] Laravel × MongoDB × Nginx の環境構築を行う

2022/03/30に公開

はじめに

実際に開発現場で使用しているdockerの設定を元に環境構築手順をまとめる。

ディレクトリ構成

.
├── app
├── bootstrap
├── config
├── database
├── docker
|   ├── php
|   |    ├── Dockerfile
|   |    └── php.ini
|   └── nginx
|        └── default.conf
├── public
├── resources
├── routes
├── tests
├── docker-compose.yml
|
... 以下省略

各ファイルの詳細

docker/php/Dockerfile
FROM php:7.4-fpm

RUN apt-get update \
    && apt-get -y install libzip-dev wget git unzip libpq-dev libfreetype6-dev libjpeg62-turbo-dev libpng-dev vim \

RUN docker-php-ext-install zip

RUN pecl install mongodb
RUN docker-php-ext-enable mongodb

RUN chown -R www-data:www-data /var/www

COPY --from=composer:latest /usr/bin/composer /usr/bin/composer
COPY ./docker/php/php.ini /usr/local/etc/php/php.ini

docker/nginx/default.conf

server {
    listen 80;
    root  /var/www/html/app/public;
    index index.html index.php;
    access_log /var/log/nginx/access.log;
    error_log  /var/log/nginx/error.log;
    location / {
        root /var/www/html/app/public;
        index  index.php index.html index.htm;
        try_files $uri $uri/ /index.php?$query_string;
    }
    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.yml
version: "3.2"
services:

  web:
    image: nginx:latest
    volumes:
      - ./docker/nginx/default.conf:/etc/nginx/conf.d/default.conf
      - .:/var/www/html/app
    ports:
      - "8000:80"
    depends_on:
      - app

  app:
    build:
      context: .
      dockerfile: docker/php/Dockerfile
    volumes:
      - .:/var/www/html/app
    depends_on:
      - db

  db:
    image: mongo:latest
    volumes:
      - "./docker/data/mongo:/data/db"
      - "./docker/data/dump:/root/dump"
    ports:
      - "27017:27017"

開発環境を立ち上げる

1. docker-compose build
2. docker-compose up -d
3. docker-compose exec app /bin/bash (ここからappコンテナに入る)
4. compose intall 
5. cp .env.example .env

Discussion