LoginSignup
0
0

More than 3 years have passed since last update.

Dockerを使用してLaravelプロジェクトを作成および実行する

Posted at

Using Docker to Create & Run a Laravel Project

このチュートリアルで使用したコンピューター:

The computer used for this tutorial:

Ubuntu 19.04 clean install

Docker version 19.03.3, build a872fc2f86

docker-compose version 1.25.0, build 0a186604

一時的な php composer コンテナを使用してプロジェクトを作成します

Use temporary php composer container to create project

sudo docker run --rm -v $(pwd):/app composer create-project --prefer-dist laravel/laravel <mylaravelproject>

終了したら、プロジェクトフォルダーの所有権を変更します

Once finished, change the ownership of the project folder

sudo chown -R myuser:user <mylaravelproject>

プロジェクトフォルダー内に、 .cloud /という新しいフォルダーを作成します。このフォルダーには、2つのフォルダー docker /nginxが含まれます。

Inside the project folder, create a new folder called .cloud/, which in turn, will have 2 folders docker/ & nginx

mylaravelproject/
    .cloud/
        docker/
        nginx/

docker /フォルダー内で、php container Dockerfileを作成します

inside the docker/ folder, create the php container Dockerfile

この記事の時点では、PHP 7.4がリリースされていますが、私はそれを試していないため、すべてのプラグインが互換性を持っているかどうかわかりません。したがって、このチュートリアルはPHP 7.3で行います。

As of the time of this article, PHP 7.4 has been released, but I haven´t experimented with it, and don´t know if all plugins have been made compatible, hence, we will be doing this tutorial with PHP 7.3

Dockerfile
FROM php:7.3-fpm

# Installing dependencies
RUN apt-get update && apt-get install -y \
    build-essential \
    default-mysql-client \
    libpng-dev \
    libjpeg62-turbo-dev \
    libfreetype6-dev \
    libzip-dev \
    locales \
    zip \
    jpegoptim optipng pngquant gifsicle

# Clear cache
RUN apt-get clean && rm -rf /var/lib/apt/lists/*

# Installing extensions
RUN docker-php-ext-install pdo_mysql mbstring zip exif pcntl bcmath opcache
RUN docker-php-ext-configure gd --with-gd --with-freetype-dir=/usr/include/ --with-jpeg-dir=/usr/include/ --with-png-dir=/usr/include/
RUN docker-php-ext-install gd

# Changing Workdir
WORKDIR /application

nginxフォルダー内に、設定ファイルを作成します

inside the nginx folder, we will create a config file

ngingx.conf
server {
  listen 80;
  index index.php index.html index.htm;
  root /application/public; # default Laravel's entry point for all requests

  access_log /var/log/nginx/access.log;
  error_log /var/log/nginx/error.log;

  location / {
    # try to serve file directly, fallback to index.php
    try_files $uri /index.php?$args;
  }

  location ~ \.php$ {
    fastcgi_index index.php;
    fastcgi_pass api-server:9000; # address of a fastCGI server, must be the same name used for the container in the docker-compose.yml
    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    fastcgi_param PATH_INFO $fastcgi_path_info;
    include fastcgi_params;
  }
}

プロジェクトのルートフォルダに戻って、 docker-compose.ymlファイルを作成できます

Going back to the project root folder, we can now create the docker-compose.ymlfile

docker-compose.yml
version: '3'

services:
  app-server: #name of the container, must be the same used on the nginx.conf
    build: .cloud/docker
    image: larave-app #name of the image
    depends_on:
      - mysql
      - mysql-test
      - redis
    volumes:
      - ./:/application:cached

  horizon:
    build: .cloud/docker
    image: laravel-app #name of the image
    command: php artisan horizon
    depends_on:
      - mysql
    volumes:
      - ./:/application:cached

  mysql:
    image: mysql:5.7
    ports:
      - "3306:3306"
    environment:
      - MYSQL_ROOT_PASSWORD=secret
      - MYSQL_DATABASE=laraveldb
    volumes:
      - db-data:/var/lib/mysql:cached

  mysql-test:
    image: mysql:5.7
    ports:
      - "3307:3306"
    environment:
      - MYSQL_ROOT_PASSWORD=secret
      - MYSQL_DATABASE=testing

  nginx:
    image: nginx:alpine
    ports:
      - "80:80"
    volumes:
      - .cloud/nginx/nginx.conf:/etc/nginx/conf.d/default.conf:cached
      - ./:/application:cached
    depends_on:
      - app-server #must be the same name of the laravel container used above

  redis:
    image: redis:alpine
    ports:
      - "6379:6379"

volumes:
  db-data:

コンテナーを実行する前に、mysqlのプロジェクトの .envファイルを変更します

Before running the container, modify the projects .env file, for mysql

DB_CONNECTION=mysql
DB_HOST=mysql
DB_PORT=3306
DB_DATABASE=laraveldb
DB_USERNAME=root
DB_PASSWORD=secret

私はdevにいるので、 storage /パーミッションを変更することを忘れないでください、私は通常777パーミッションで行きます

Remember to change the storage/ permissions, since I´m on dev, I usually go with 777 permissions

sudo chmod -R 777 storage/

最後に、docker-composeを実行します

Finally, run the docker-compose

sudo docker-compose up --build

http:// localhostlaravel ウェルカムページを見ることができます。

You can see the laravel welcome page, on http://localhost

移行または職人のコマンドを実行する場合は、*app-server * コンテナを参照することで実行できます

If you want to run migrations, or any artisan command, you can do so, by referencing the app-server container

sudo docker-compose exec app-server php artisan migrate
0
0
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
0
0