4
4

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 5 years have passed since last update.

laravel6のDockerfileとdocker-compose.ymlを書く

Last updated at Posted at 2019-12-26

シンプルなphp:7.4のイメージをベースにしています。
ローカルでの開発環境を想定しています。

Dockerfile
FROM php:7.4

RUN apt-get update -y && \
    apt-get install default-mysql-client nodejs npm git unzip -y && \
    // これがないとmysqlにつながらなくてphp artisan migrationなどのコマンドでエラーになる
    docker-php-ext-install pdo_mysql

COPY --from=composer /usr/bin/composer /usr/bin/composer
docker-compose.yml
version: '3'
services:
  mysql:
    // このイメージを元にする
    image: mysql:8.0.18
    // パスワードの認証方法を変更
    command: --default-authentication-plugin=mysql_native_password
    // password設定
    environment:
      MYSQL_ROOT_PASSWORD: xxxx
    // データを保存するための設定
    volumes:
      - "./mysql-data:/var/lib/mysql"
    ports:
      - "3306:3306"
  web:
    // buildするDockerfileを指定する
    build:
      context: .
      dockerfile: Dockerfile
    // マウントする場所のマッピング
    volumes:
      - ".:/app"
    // portのマッピング
    ports:
      - "8000:8000"
    // コンテナを起動しつづける
    tty: true
    // mysqlが起動してからwebを起動させる
    depends_on:
      - mysql
    // コンテナの中に入ったときに/appにいるようにする
    working_dir: "/app"
// --buildは初回だけでよくて、うまくいったら次回はなくてもよい
docker-compose up --build

// 次でコンテナ内に入れる
docker exec -it コンテナ名 bash

composer create-project --prefer-dist laravel/laravel laravel_app

php artisan serve --host 0.0.0.0

// mysqlに入る
mysql -uroot -ppass -h mysql

.envでは
hostはmysqlを設定する。

参考サイト
Docker超入門という動画を作りました。 - Qiita

Docker超入門 Part1 - Qiita

laraveldev/php Dockerfile - Docker Hub

Docker に Composer をインストールするベストプラクティス(と解説) - Qiita

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?