[docker] 多容器项目 - PHP+MySQL+Nginx+utility containers
这个项目总共会配置 6 个容器,主要还是学习一下 docker 的使用和配置,目标是:
-
本机不安装 PHP、Nginx
安装部分全都交给 docker 容器实现
-
可以运行一个 Laravel 网页项目
-
修改本机代码可以即时更新网页
大概流程是这样的:
主机只负责保存必要的源码,PHP 容器会读取本机变动的源码,随后进行必要的更新
这个项目也是把之前所有学的知识点全都整合一下,本项目没有任何的 PHP 实现
service 实现
按照模块分
nginx
YAML 部分配置如下:
是 laravel 的默认数据库名称,配置方面的就不多赘述了
这时候项目结构如下
❯ tree
修改 laravel 配置
这里主要修改的是 src/.env
,之前提到了现在 laravel 已经使用 sqlite 了,所以需要修改一下 mysql 的配置:
# 大概是 22-27 行这里
DB_CONNECTION=mysql
DB_HOST=mysql
DB_PORT=3306
DB_DATABASE=homestead
DB_USERNAME=homestead
DB_PASSWORD=secret
运行 laravel 项目
这里使用 docker compose up
,使用方式有两种:
# 1. 这里会运行指定的3个容器
❯ docker compose up server php mysql
# 2. 或者配置 depends_on,之前没有配置,不过在我的 docker compose 里已经补上了
❯ docker compose up server
# 省略掉一些 download 和 build
[+] Running 0/3
⠹ Container section08-php-1 Created 0.2s
⠹ Container section08-mysql-1 Created 0.2s
⠋ Container section08-server-1 Created 0.1s
Attaching to server-1
server-1 | /docker-entrypoint.sh: /docker-entrypoint.d/ is not empty, will attempt to perform configuration
server-1 | /docker-entrypoint.sh: Looking for shell scripts in /docker-entrypoint.d/
server-1 | /docker-entrypoint.sh: Launching /docker-entrypoint.d/10-listen-on-ipv6-by-default.sh
server-1 | 10-listen-on-ipv6-by-default.sh: info: can not modify /etc/nginx/conf.d/default.conf (read-only file system?)
server-1 | /docker-entrypoint.sh: Launching /docker-entrypoint.d/20-envsubst-on-templates.sh
server-1 | /docker-entrypoint.sh: Launching /docker-entrypoint.d/30-tune-worker-processes.sh
server-1 | /docker-entrypoint.sh: Configuration complete; ready for start up
server-1 | 2024/04/25 03:45:52 [notice] 1#1: using the "epoll" event method
server-1 | 2024/04/25 03:45:52 [notice] 1#1: nginx/1.24.0
server-1 | 2024/04/25 03:45:52 [notice] 1#1: built by gcc 12.2.1 20220924 (Alpine 12.2.1_git20220924-r4)
server-1 | 2024/04/25 03:45:52 [notice] 1#1: OS: Linux 6.6.22-linuxkit
server-1 | 2024/04/25 03:45:52 [notice] 1#1: getrlimit(RLIMIT_NOFILE): 1048576:1048576
server-1 | 2024/04/25 03:45:52 [notice] 1#1: start worker processes
server-1 | 2024/04/25 03:45:52 [notice] 1#1: start worker process 21
server-1 | 2024/04/25 03:45:52 [notice] 1#1: start worker process 22
server-1 | 2024/04/25 03:45:52 [notice] 1#1: start worker process 23
server-1 | 2024/04/25 03:45:52 [notice] 1#1: start worker process 24
server-1 | 2024/04/25 03:45:52 [notice] 1#1: start worker process 25
server-1 | 2024/04/25 03:45:52 [notice] 1#1: start worker process 26
server-1 | 2024/04/25 03:45:52 [notice] 1#1: start worker process 27
server-1 | 2024/04/25 03:45:52 [notice] 1#1: start worker process 28
看起来服务器已经成功启动了:
⚠️:这里的报错是因为 mysql 没有成功配置好的关系,现在 laravel 默认使用的是 sqlite,所以配置都是按照 sqlite 进行的,等到下一步配置完 artisan 就好了
artisan & npm
compose 配置如下:
artisan:
build:
context: "./dockerfiles"
dockerfile: "php.dockerfile"
volumes:
- ./src:/var/www/html:delegated
entrypoint: ["php", "/var/www/html/artisan", "migrate"]
npm:
image: node:20-alpine
working_dir: /var/www/html
entrypoint: ["npm"]
volumes:
- ./src:/var/www/html:delegated
这两个没有新创建一个 dockerfile——也不是不可以,不过这里展现一下不用新建 dockerfile 也可以实现 entrypoint 之类的功能。
同样,这两个也是 util container
artisan 运行结果如下:
❯ docker compose run --rm artisan
WARN[0000] /Users/user/study/docker/section08/docker-compose.yaml: `version` is obsolete
INFO Preparing database.
Creating migration table ...................................... 25.68ms DONE
INFO Running migrations.
0001_01_01_000000_create_users_table .......................... 79.47ms DONE
0001_01_01_000001_create_cache_table .......................... 27.05ms DONE
0001_01_01_000002_create_jobs_table ........................... 82.80ms DONE
这代表数据库已经 migrate 成功了:
npm 运行结果如下:
❯ docker compose run --rm npm
WARN[0000] /Users/user/study/docker/section08/docker-compose.yaml: `version` is obsolete
npm <command>
Usage:
npm install install all the dependencies in your project
npm install <foo> add the <foo> dependency to your project
npm test run this project's tests
npm run <foo> run the script named <foo>
npm <command> -h quick help on <command>
npm -l display usage info for all commands
npm help <term> search for help on <term>
npm help npm more involved overview
All commands:
access, adduser, audit, bugs, cache, ci, completion,
config, dedupe, deprecate, diff, dist-tag, docs, doctor,
edit, exec, explain, explore, find-dupes, fund, get, help,
help-search, hook, init, install, install-ci-test,
install-test, link, ll, login, logout, ls, org, outdated,
owner, pack, ping, pkg, prefix, profile, prune, publish,
query, rebuild, repo, restart, root, run-script, sbom,
search, set, shrinkwrap, star, stars, start, stop, team,
test, token, uninstall, unpublish, unstar, update, version,
view, whoami
Specify configs in the ini-formatted file:
/root/.npmrc
or on the command line via: npm <command> --key=value
More configuration info: npm help config
Configuration fields: npm help 7 config
npm@10.5.0 /usr/local/lib/node_modules/npm
至此,就完成了不需要在本机上安装这些功能,但是依旧可以启动一个 laravel 项目的 docker 案例了