Help us understand the problem. What is going on with this article?

Laravel+MySQL+NginxでさくっとDocker開発立ち上げる

More than 1 year has passed since last update.

Laravel環境をDockerで作りました。
よく使われている環境はありますが、シンプルにやりたかったので自前でdocker-composeしています。

ファイル構成イメージ

.
├── Dockerfile
├── docker-compose.yml
├── etc
│   └── nginx
│       └── conf.d
│           └── default.conf
└── laravel

docker-compose.yml

docker-compose.yml
version: "2"

services:
  nginx:
    image: nginx:alpine
    volumes:
      - ./etc/nginx/conf.d/:/etc/nginx/conf.d/
      - ./laravel/public:/var/www/laravel/public
    ports:
      - 80:80
    links:
      - phpfpm
    depends_on:
      - phpfpm
  phpfpm:
    build:
      context: .
      dockerfile: ./Dockerfile
    volumes:
      - ./laravel:/var/www/laravel
    links:
      - mysql
    depends_on:
      - mysql
    environment:
      - DB_HOST=mysql
      - DB_DATABASE=wakana
      - DB_USERNAME=root
      - DB_PASSWORD=pass
  mysql:
    image: mysql:5.7
    environment:
      - MYSQL_ROOT_PASSWORD=pass
      - MYSQL_DATABASE=wakana

Mysqlのバージョンが5.7なのは、8だとめんどくさいエラーでるからです。

めんどくさいエラー
   Illuminate\Database\QueryException  : SQLSTATE[HY000] [2054] The server requested authentication method unknown to the client

Dockerfile

Dockerfile
FROM php:7.2-fpm-alpine

RUN docker-php-ext-install pdo_mysql

PHPはメンテコストを考え現在(2018/05/09現在)latestの7.2に固定していますがお好きなので大丈夫です。
phpfpmにpdo_mysql入ってないので別にDockerfile作成しインストールしてあげます。

Nginx

nginxコンテナに共有する./etc/nginx/conf.dに置くファイルです。
普通にららべる向けの設定を書けばOKです。

default.conf
server {
    listen       0.0.0.0:80;
    server_name  localhost;
    charset      utf-8;

    root /var/www/laravel/public;

    index index.php;

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

    location ~ \.php$ {
        fastcgi_pass  phpfpm:9000;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME  $document_root$fastcgi_script_name;
        include       fastcgi_params;
    }
}

Docker立ち上げ

docker-compose up -d

どーん!

アプリケーションキー発行、マイグレーションなど

docker-compose exec phpfpm /var/www/laravel/artisan key:generate
docker-compose exec phpfpm /var/www/laravel/artisan migrate

もしくは

docker-compose exec phpfpm /bin/sh

このコマンドで中入れるのでlaravelフォルダ内で作業します。

cd /var/www/laravel/

php artisan key:generate
php artisan migrate

アクセス

こんにちわんこ!
http://localhost/

スクリーンショット 2018-05-09 13.28.49.png

Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
No comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
ユーザーは見つかりませんでした