スポンサーリンク

Laravel+MySQL+Nginx with Docker and docker-compose

Development Environment
スポンサーリンク

Development tools to be prepared

  • Docker 20.10
  • docker-compose 1.27.4
  • git 2.25

If you have not installed Docker and docker-compose

MacOS

Install Docker from the official website.

Docker Desktop for Mac

Windows
Setting Up Docker and docker-compose on WSL2
Setting Up Docker and docker-compose on WSL2.Installing WSL2. Installing Docker 20.10.1 . Installing docker-compose 1.27.4

Containers to be created with docker-compose

  • PHP 7.4 / composer 2.0.14
  • MySQL 8.0
  • Nginx 1.18
スポンサーリンク

Clone the Dockerfile repository

The Dockerfile for Laravel development is available on github, so please clone it!

GitHub - E-handson/docker-laravel
Contribute to E-handson/docker-laravel development by creating an account on GitHub.

Start a terminal and run the clone command.

$ git clone https://github.com/E-handson/docker-laravel.git

After the clone is complete, the “docker-laravel” directory will be created.
Execute the cd command.

$ cd docker-laravel

You will find docker-compose.yml under the docker-laravel directory.
The docker-compose command can be executed directly under the directory where docker-compose.yml exists.

Building Docker

First, execute the following command to build the Docker service.

$ docker-compose build

Next, docker-compose up

$ docker-compose up

Let’s run the “docker-compose ps” command to see if Docker is running.

$ docker-compose ps
      Name                         Command            State           Ports 
------------------------------------------------------------------------------------
docker-laravel_app_1     docker-php-entrypoint php-fpm   Up     9000/tcp 
docker-laravel_db_1      docker-entrypoint.sh mysqld     Up     3306/tcp, 33060/tcp 
docker-laravel_web_1    /docker-entrypoint.sh ngin ...   Up     0.0.0.0:8000->80/tcp

docker-laravel_app_1 → PHP and Composer Container
docker-laravel_db_1 → MySQL Container
docker-laravel_web_1 → Nginx Container

If the “State” column is “Up”, the container is up and running.

スポンサーリンク

Installing Laravel

Install Laravel in the “app” container

Execute the following command.

$ docker-compose exec app composer create-project --prefer-dist "laravel/laravel=6.*" .

When you are done, the “src” directory will have been created.

$ ll
drwxrwxr-x 8 user user 4096 Dec 24 01:43 .git/
-rw-rw-r-- 1 user user 486 Dec 24 01:43 README.md
drwxrwxr-x 5 user user 4096 Dec 24 01:43 docker/
-rw-rw-r-- 1 user user 719 Dec 24 01:43 docker-compose.yml
drwxr-xr-x 3 root root 4096 Dec 24 01:46 src/

The Laravel source files have been created under the “src” directory.

Accessing localhost:8000 will bring up the Laravel welcome page.

MySQL configuration

Edit the file “./src/.env”.

Before

DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=laravel
DB_USERNAME=root
DB_PASSWORD=

After

DB_CONNECTION=mysql
DB_HOST=db
DB_PORT=3306
DB_DATABASE=laravel_local
DB_USERNAME=phper
DB_PASSWORD=secret
Let’s try to run the migration to see if it connects to MySQL. Execute the following command.
$ docker-compose exec app php artisan migrate
Migration table created successfully.
Migrating: 2014_10_12_000000_create_users_table
Migrated: 2014_10_12_000000_create_users_table (0.11 seconds)
Migrating: 2014_10_12_100000_create_password_resets_table
Migrated: 2014_10_12_100000_create_password_resets_table (0.08 seconds)
Migrating: 2019_08_19_000000_create_failed_jobs_table
Migrated: 2019_08_19_000000_create_failed_jobs_table (0.04 seconds)

If the connection is successful, migrate will be executed and the authentication and other tables will be created in MySQL.

This completes the development environment setup!

スポンサーリンク

Docker Container Configuration

Repository configuration

docker-laravel
├── docker-compose.yml
├── docker
│    ├── mysql
│    │    ├── Dockerfile
│    │    └── my.cnf
│    ├── nginx
│    │    ├── default.conf
│    └── php
│       ├── Dockerfile
│       └── php.ini
└── src

“src” directory does not exist when you do a git clone.
It will be created after Laravel is installed.

Container configuration

As you can see in “docker-compose ps”, it consists of three containers.

  1. app container – PHP7.4 installed
  2. db container – MySQL8.0 installed
  3. web container – Nginx1.18 installed
app container

This is the container where Laravel is installed.
The Laravel source installed in the “/workspace” of the app container is mounted under the local src/ distribution.

db container

The user and database are created at build time.

DB name:laravel_local
DB user:phper
password:secret
web container

The configuration is written in “default.cnf” on the local side.
The file is mounted on the container side.

スポンサーリンク

コメント