使用 Docker 容器化 Laravel 專案

建立專案

建立專案。

1
laravel new laravel

修改 .env 檔:

1
2
3
4
5
6
DB_CONNECTION=mysql
DB_HOST=mysql
DB_PORT=3306
DB_DATABASE=laravel
DB_USERNAME=root
DB_PASSWORD=root

容器化

新增 docker-compose.yaml 檔:

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
42
43
44
45
46
47
48
version: "3"

services:
app:
container_name: laravel
build:
context: .
dockerfile: Dockerfile
volumes:
- ./:/var/www
env_file: .env
depends_on:
- database
networks:
- backend

web:
image: nginx:alpine
container_name: nginx
volumes:
- ./nginx/conf.d:/etc/nginx/conf.d
depends_on:
- app
ports:
- "8990:80"
networks:
- backend

database:
image: mysql:latest
container_name: mysql
volumes:
- mysql:/var/lib/mysql
environment:
- MYSQL_DATABASE=${DB_DATABASE}
- MYSQL_USER=${DB_USERNAME}
- MYSQL_PASSWORD=${DB_PASSWORD}
- MYSQL_ROOT_PASSWORD=${DB_PASSWORD}
ports:
- "3306:3306"
networks:
- backend

networks:
backend:

volumes:
mysql:

新增 Dockerfile 檔:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
FROM php:7.2-fpm

RUN apt-get update \
&& apt-get -y install zip

WORKDIR /var/www

COPY . /var/www

RUN curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer

RUN composer install --optimize-autoloader --no-dev --no-scripts

RUN chown -R www-data:www-data \
/var/www/storage \
/var/www/bootstrap/cache

RUN apt-get install -y libmcrypt-dev \
libmagickwand-dev --no-install-recommends \
&& pecl install mcrypt-1.0.2 \
&& docker-php-ext-install pdo_mysql \
&& docker-php-ext-enable mcrypt

新增 .dockerignore 檔:

1
2
3
4
5
.git
.gitignore
.env.*
node_modules
vendor

新增一個 nginx 資料夾,並新增 default.conf 檔:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
server {
listen 80;

index index.php index.html;

root /var/www/public;

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

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;
}
}

編譯並啟動容器:

1
docker-compose up -d --build

前往 http://localhost:8990/ 瀏覽。

程式碼

參考資料