Cài đặt môi trường cho laravel dùng docker

1. Cài đặt laravel

1
composer create-project --prefer-dist laravel/laravel blog

2. Tạo file docker-compose.yml

Tạo file docker-compose.yml trong thư mục của laravel.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
version: '2'
services:

# The Application
app:
build:
context: ./
dockerfile: app.dockerfile
working_dir: /var/www
volumes:
- ./:/var/www
environment:
- "DB_PORT=3306"
- "DB_HOST=database"

# The Web Server
web:
build:
context: ./
dockerfile: web.dockerfile
working_dir: /var/www
volumes_from:
- app
ports:
- 8080:80

# The Database
database:
image: mysql
volumes:
- dbdata:/var/lib/mysql
environment:
- "MYSQL_DATABASE=homestead"
- "MYSQL_USER=homestead"
- "MYSQL_PASSWORD=secret"
- "MYSQL_ROOT_PASSWORD=secret"
ports:
- "33061:3306"

volumes:
dbdata:

Tạo file app.dockerfile

1
2
3
4
5
FROM php:7-fpm

RUN apt-get update && apt-get install -y libmcrypt-dev \
mysql-client libmagickwand-dev --no-install-recommends \
&& docker-php-ext-install mcrypt pdo_mysql

Tạo file web.dockerfile

1
2
3
FROM nginx

ADD vhost.conf /etc/nginx/conf.d/default.conf

Tạo file vhost.conf

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
server {
listen 80;
index index.php index.html;
root /var/www/public;

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

location ~ \.php$ {
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass app:9000;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param PATH_INFO $fastcgi_path_info;
}
}

3. Chạy docker

Chạy lệnh này để cài đặt tất cả các dịch vụ

1
docker-compose up

Chạy các lệnh artisan hay một số lệnh bên trong docker dùng

1
2
3
4
5
docker-compose exec app "__ Lệnh muốn chạy __"

Ví dụ:

docker-compose exec app php artisan migrate