5
8

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

Docker(docker-compose)で、Laravelの環境を作成

Last updated at Posted at 2021-01-10

初めに

もともと作られた方のものを参考(パクって)展開
こちらの方のページを参考(パク…)

ソース

https://github.com/naozo-se/Docker-Laravel
にあります。ダウンロードしたら、実行コマンドから

こんな感じで作成

・php7.4 / mysql8 / nginx1.19 のイメージで作成
・sampleprojectというプロジェクト名で作成(名称変更する場合は、適当に置換を)
・確認はDocker Desktop for Windows で確認
・Ubuntuでも確認(Dockerのバージョンは以下)
 ⇛Docker version 20.10.1, build 831ebea
 ⇛docker-compose version 1.26.0, build d4451659

フォルダ構成

laravel
 ├ docker-compose.yml
 ├ docker
 │  ├ php
 │  │  ├ php.ini
 │  │  └ Dockerfile
 │  └ nginx
 │    └ default.conf
 └ server

各ファイル内容

docker-compose.yml
version: '3'

services:
  laravel_php:
    container_name: laravel_php
    build: ./docker/php
    volumes:
    - ./server:/var/www

  laravel_nginx:
    image: nginx:1.19
    container_name: laravel_nginx
    ports:
    - 80:80
    volumes:
    - ./server:/var/www
    - ./docker/nginx/default.conf:/etc/nginx/conf.d/default.conf
    depends_on:
    - laravel_php

  laravel_db:
    image: mysql:8.0
    container_name: laravel_db
    environment:
      MYSQL_ROOT_PASSWORD: rootpass
      MYSQL_DATABASE: sampleproject
      MYSQL_USER: dbuser
      MYSQL_PASSWORD: dbpass
      TZ: 'Asia/Tokyo'
    command: mysqld --character-set-server=utf8mb4 --collation-server=utf8mb4_unicode_ci
    volumes:
    - ./docker/db/data:/var/lib/mysql
    - ./docker/db/my.cnf:/etc/mysql/conf.d/my.cnf
    - ./docker/db/sql:/docker-entrypoint-initdb.d
    ports:
    - 3306:3306
../docker/php/php.ini
[Date]
date.timezone = "Asia/Tokyo"
[mbstring]
mbstring.internal_encoding = "UTF-8"
mbstring.language = "Japanese"
../docker/php/Dockerfile
FROM php:7.4-fpm
COPY php.ini /usr/local/etc/php/

RUN apt-get update && apt-get -y upgrade
RUN apt-get install -y zlib1g-dev && apt-get install -y libzip-dev
RUN docker-php-ext-install pdo_mysql zip

COPY --from=composer /usr/bin/composer /usr/bin/composer
ENV COMPOSER_ALLOW_SUPERUSER 1
ENV COMPOSER_HOME /composer
ENV PATH $PATH:/composer/vendor/bin

WORKDIR /var/www

RUN composer global require "laravel/installer"
../docker/nginx/default.conf
server {
    index index.php index.html;
    server_name localhost;
    root /var/www/sampleproject/public;

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

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

実行コマンド

1.コンテナ構築

docker-compose up -d

2.Laravelプロジェクト作成

コンテナ内に入る

docker exec -ti laravel_php bash

バージョン6の作成コマンド(コンテナ内)

composer create-project "laravel/laravel=6.*" sampleproject

プロジェクト作成後(そのままコンテナ内で実行)

cd sampleproject
chmod -R 777 storage
chmod -R 777 bootstrap/cache

サーバー起動

php artisan serve

※localhostのみで接続できるはず

3.DB関連

.env の編集(プロジェクトフォルダ以下)

..env
DB_CONNECTION=mysql
DB_HOST=laravel_db
DB_PORT=3306
DB_DATABASE=sampleproject
DB_USERNAME=dbuser
DB_PASSWORD=dbpass

※composeのymlで記述した参考に

接続確認

php artisan migrate

修正履歴

2021.01.25
・タイトル等の記載修正
・ファイル名、コマンドの誤記載修正
・ubuntuでの確認を追記
2021.02.06
・GitHubのリンクを追加

5
8
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
5
8

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?