7
5

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 ComposeでLaravelのバージョンも指定した開発環境構築

Last updated at Posted at 2021-03-18

はじめに

Docker Composeを使ってとりあえず思考停止でLaravelの開発環境を作りたいって人向けの記事です。
この記事を読めばLaravelの環境構築がすぐに出来ます。

環境

Docker version 19.03.13
docker-compose version 1.27.4
Mac

1.ディレクトリの作成

はじめにlaravelディレクトリを作成し、以下のように構成して下さい。

 laravel
   ├── docker/
   │   ├── app/
   │   │   ├── Dockerfile
   │   │   └── php.ini
   │   ├── db/
   │   │   ├── Dockerfile
   │   │   └── my.cnf
   │   └── nginx/
   │       └── default.conf
   ├── .env
   ├── .gitignore
   ├── docker-compose.yml
   └── server/

スクリーンショット 2021-03-18 7.51.45.png

2.ファイルの中身の作成

app/Dockerfile

FROM php:7.3-fpm
COPY php.ini /usr/local/etc/php/

RUN apt-get update && apt-get install -y zlib1g-dev libzip-dev mysql-common wget locales npm \
  && docker-php-ext-install zip pdo_mysql \
  && locale-gen ja_JP.UTF-8 \
  && echo "export LANG=ja_JP.UTF-8" >> ~/.bashrc \
  && wget https://dev.mysql.com/get/Downloads/MySQL-8.0/mysql-community-client-core_8.0.17-1debian10_amd64.deb \
  && dpkg -i mysql-community-client-core_8.0.17-1debian10_amd64.deb

ENV LANG ja_JP.UTF-8
ENV LC_CTYPE ja_JP.UTF-8
RUN localedef -f UTF-8 -i ja_JP ja_JP.utf8
ENV LC_ALL ja_JP.UTF-8

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

RUN composer config -g repos.packagist composer https://packagist.jp
RUN composer global require hirak/prestissimo

WORKDIR /var/www

RUN composer global require "laravel/installer"
app/php.ini
[Date]
date.timezone = "Asia/Tokyo"
[mbstring]
mbstring.internal_encoding = "UTF-8"
mbstring.language = "Japanese"

display_errors = ON
db/Dockerfile
FROM mysql:8.0.17

COPY my.cnf /etc/mysql/conf.d/

RUN apt-get update -qq ;\
    apt-get install -y locales ;\
    locale-gen ja_JP.UTF-8
ENV LANG ja_JP.UTF-8
ENV LC_CTYPE ja_JP.UTF-8
RUN localedef -f UTF-8 -i ja_JP ja_JP.utf8
ENV LC_ALL ja_JP.UTF-8
RUN cp /usr/share/zoneinfo/Asia/Tokyo /etc/localtime
db/my.cnf
[mysqld]
character-set-server=utf8mb4
collation-server=utf8mb4_unicode_ci
default_authentication_plugin= mysql_native_password

[client]
default-character-set=utf8mb4

nginx/default.conf
server {
    listen 80;
    index index.php index.html;
    root /var/www/public/;

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

    location ~ \.php$ {
        try_files $uri =404;
        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;
    }
}
.gitignore
mysql-data
.env
.env
MYSQL_ROOT_PASSWORD=password
docker-compose.yml
version: "3.6"

services:
  app:
    build: ./docker/app
    ports:
      - 8000:8000
    volumes:
      - ./server:/var/www
    working_dir: /var/www

  nginx:
    image: nginx
    ports:
      - 80:80
    volumes:
      - ./server:/var/www
      - ./docker/nginx/default.conf:/etc/nginx/conf.d/default.conf
    depends_on:
      - app

  db:
    build: ./docker/db
    image: mysql8.0.17
    environment:
      MYSQL_ROOT_PASSWORD: ${MYSQL_ROOT_PASSWORD}
    volumes:
      - "./mysql-data:/var/lib/mysql"
    ports:
      - 3306:3306

3.docker-composeで起動

# laravelディレクトリで
$ docker-compose up -d
Creating laravel_app_1 ... done
Creating laravel_db_1  ... done
Creating laravel_nginx_1 ... done

次にappコンテナに入ります。

$ docker-compose exec app bash

そして

composer create-project laravel/laravel ./laravel_project "5.5.*" --prefer-dist

を実行するとserver直下にlaravel_projectのフォルダがインストールされてきます。
なお、laravel_project "5.5.*"の部分は好きなプロジェクト名とバージョンを指定出来ます。
今回私は初学者のため、情報量が多い、5.5を指定しました。

4.laravelの起動

以下のコマンドでlaravel_projectディレクトリに移動します。

cd laravel_project

そして以下のコマンドを実行し

php artisan serve --host 0.0.0.0

以下へアクセスすると
http://localhost:8000/

スクリーンショット 2021-03-18 8.51.20.png

これで無事Laravelが立ち上がりました。

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?