1
0

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 1 year has passed since last update.

# Docker + laravel + nginx + mysqlでローカル環境構築

Last updated at Posted at 2022-05-11

docker-compose.ymlファイルの作成

docker-compose.yml
version: "3.9"

services:
    app:
        # php コンテナ
        build:
            context: .
            dockerfile: ./app/Dockerfile
        volumes:
            - ./src/:/app
    web:
        # nginx コンテナ
        build:
            context: .
            dockerfile: ./docker/web/Dockerfile
        ports:
            - 8080:80
        depends_on:
            - app
        volumes:
            - ./src/:/app
    db:
        # mysql コンテナ
        build:
            context: .
            dockerfile: ./docker/db/Dockerfile
        ports:
            - 3306:3306
        environment:
            MYSQL_DATABASE: database
            MYSQL_USER: user
            MYSQL_PASSWORD: password
            MYSQL_ROOT_PASSWORD: password
            TZ: 'Asia/Tokyo'
        volumes:
            - mysql-volume:/var/lib/mysql
volumes:
    mysql-volume:

各Dockerfile

app(php)

FROM php:8.0-fpm

ENV TZ Asia/Tokyo

RUN apt-get update && \
    apt-get install -y git unzip libzip-dev libicu-dev libonig-dev && \
    docker-php-ext-install intl pdo_mysql zip bcmath

COPY ./app/php.ini /usr/local/etc/php/php.ini

COPY --from=composer:2.0 /usr/bin/composer /usr/bin/composer

WORKDIR /app

web(nginx)

FROM nginx:1.20-alpine

ENV TZ Asia/Tokyo

COPY ./docker/web/default.conf /etc/nginx/conf.d/default.conf

db(mysql)

FROM mysql:8.0

COPY ./docker/db/my.conf /etc/my.conf
  • 3つのコンテナが作成される
    docker-compose up -d --build
laravel % docker container ps -a
CONTAINER ID   IMAGE         COMMAND                  CREATED             STATUS             PORTS                               NAMES
b8e098958ac0   laravel_db    "docker-entrypoint.s…"   9 seconds ago       Up 7 seconds       0.0.0.0:3306->3306/tcp, 33060/tcp   laravel-db-1
c569c60a1811   laravel_web   "/docker-entrypoint.…"   About an hour ago   Up About an hour   0.0.0.0:8080->80/tcp                laravel-web-1
06ad6812158d   laravel_app   "docker-php-entrypoi…"   About an hour ago   Up About an hour   9000/tcp                            laravel-app-1

mysql接続確認

//  dbコンテナに入る
laravel % docker-compose exec db bash

root@b8e098958ac0:/# mysql -u root -p
// この段階では、ymlファイル内のpassword入力
Enter password:

Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 15
Server version: 8.0.29 MySQL Community Server - GPL

Copyright (c) 2000, 2022, Oracle and/or its affiliates.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql>

appコンテナからdbコンテナへ接続

laravel installでできたsrc/.envファイルにymlファイルに定義した内容を指定
DB_HOSTにはMySQLコンテナのサービス名

src/.env
DB_CONNECTION=mysql
DB_HOST=db
DB_PORT=3306
DB_DATABASE=database
DB_USERNAME=root
DB_PASSWORD=password

マイグレーションを実行して、接続確認

laravel % docker-compose exec app bash
root@0b8668e3911e:/app# php artisan migrate
Migration table created successfully.
Migrating: 2014_10_12_000000_create_users_table
Migrated:  2014_10_12_000000_create_users_table (63.47ms)
Migrating: 2014_10_12_100000_create_password_resets_table
Migrated:  2014_10_12_100000_create_password_resets_table (51.16ms)
Migrating: 2019_08_19_000000_create_failed_jobs_table
Migrated:  2019_08_19_000000_create_failed_jobs_table (49.98ms)
Migrating: 2019_12_14_000001_create_personal_access_tokens_table
Migrated:  2019_12_14_000001_create_personal_access_tokens_table (59.13ms)
root@0b8668e3911e:/app#
root@b8e098958ac0:/# mysql -u root -p
mysql> use database;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed
mysql> show tables;
+------------------------+
| Tables_in_database     |
+------------------------+
| failed_jobs            |
| migrations             |
| password_resets        |
| personal_access_tokens |
| users                  |
+------------------------+
5 rows in set (0.00 sec)

mysql>

参考

1
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
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?