-
创建容器:
cd 工作目录
# 创建项目文件 mkdir tp8
cd tp8
# 初始化docker docker init
# 交互:start # 选择语言&服务器配置(选择php,回车即可) ? What application platform does your project use? PHP with Apache # 语言版本(回车为docker的默认php版本,这里为指定版本8.1) ? What version of PHP do you want to use? 8.1 # 设置当前目录作为docker容器app映射的工作目录(回车即可) ? Please enter the relative directory (with a leading .) for your app: ./ (current directory) # docker监听的本地端口,该端口会映射到docker的指定端口,默认80 ? What port do you want to use to access your app? 9000 # 交互:end
# 接下来会出现三个docker核心文件,以下是简单解释: # .dockerignore 将本地目录映射到docker容器时需要忽略的文件 # compose.yaml 使用yaml语法管理多容器的配置文件,用来管理容器用的 # Dockerfile 容器化构建时调用的核心配置文件
配置dockerfile # 使用 PHP 8.0 作为基础镜像 FROM php:8.0-fpm
# 更新软件包列表并安装必要的编译工具和依赖 # 更换软件源为阿里云源 RUN sed -i 's/deb.debian.org/mirrors.aliyun.com/g' /etc/apt/sources.list && \ sed -i 's/security.debian.org/mirrors.aliyun.com/g' /etc/apt/sources.list && \ apt-get update -qq && \ apt-get install -y --no-install-recommends libzip-dev zip libpng-dev libonig-dev libxml2-dev unzip && \ docker-php-ext-install pdo_mysql zip mbstring exif pcntl bcmath gd && \ # 清理安装包缓存 apt-get clean && \ rm -rf /var/lib/apt/lists/*
# 安装 Composer RUN curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer
# 更换 Composer 源为阿里云源 RUN composer config -g repo.packagist composer https://mirrors.aliyun.com/composer/
# 安装 git 工具 RUN apt-get update && apt-get install -y git
# 设置环境变量允许在 root 用户下使用 Composer 插件 ENV COMPOSER_ALLOW_SUPERUSER=1
# 设置工作目录 WORKDIR /var/www/html
# 复制项目文件到容器中 COPY . /var/www/html
# 切换用户为 www-data USER www-data
# 使用 Composer 安装 ThinkPHP 8.0 及其依赖 RUN composer create-project topthink/think thinkphp8
# 切换回 root 用户安装 Nginx USER root
# 安装 Nginx RUN apt-get update && apt-get install -y nginx
# 复制 Nginx 配置文件 COPY nginx.conf /etc/nginx/nginx.conf
# 暴露端口 EXPOSE 80
# 启动 Nginx 和 PHP-FPM 服务 CMD service nginx start && php-fpm 添加nginx配置文件:nginx.conf worker_processes 1;
events { worker_connections 1024; }
http { include mime.types; default_type application/octet-stream;
sendfile on; keepalive_timeout 65;
server { listen 80; server_name localhost;
root /var/www/html/thinkphp8/public; index index.php index.html index.htm;
location / { try_files $uri $uri/ /index.php?$query_string; }
location ~ \.php$ { fastcgi_pass 127.0.0.1:9000; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; include fastcgi_params; } } } 做完上述操作后,开始构建:docker compose up --build 构建成功会提示: [+] Running 2/2g to docker.io/library/tp8-server:latest 1.3s ✔ Service server Built 132.1s ✔ Container tp8-server-1 Recreated 0.3s Attaching to server-1 server-1 | Starting nginx: nginx. server-1 | [22-Jan-2025 09:27:07] NOTICE: fpm is running, pid 20 server-1 | [22-Jan-2025 09:27:07] NOTICE: ready to handle connections 打开终端,查看安装的服务 # 查看docker的所有服务 roy_chen@roy-chendeMacBook-Pro ~ % docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES f22203ee0e1d tp8-server "docker-php-entrypoi…" 34 seconds ago Up 34 seconds 9000/tcp, 0.0.0.0:9000->80/tcp tp8-server-1 # 进入bash roy_chen@roy-chendeMacBook-Pro ~ % docker exec -it tp8-server-1 bash # 检查composer root@f22203ee0e1d:/var/www/html# composer -v ______ / ____/___ ____ ___ ____ ____ ________ _____ / / / __ \/ __ `__ \/ __ \/ __ \/ ___/ _ \/ ___/ / /___/ /_/ / / / / / / /_/ / /_/ (__ ) __/ / \____/\____/_/ /_/ /_/ .___/\____/____/\___/_/ /_/ Composer version 2.8.5 2025-01-21 15:23:40
# composer安装成功
打开浏览器,请求:http://localhost:9000/,就可以看到TP8的欢迎页面,框架安装成功。 因为TP8框架是直接在docker中安装的,所以需要把docker的框架文件同步到本地 # 首先获取容器名称: docker ps -a # echo CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 96c5077a3b37 tp8-server "docker-php-entrypoi…" 9 seconds ago Up 8 seconds 9000/tcp, 0.0.0.0:9000->80/tcp tp8-server-1 # 复制容器里的文件到本地 # docker cp your_container_name:/container/path/to/file ./local/path/to/file docker cp tp8-server-1:/var/www/html/thinkphp8 /Users/roy_chen/Desktop/private/code/docker/tp8
在本地修改文件内容实时同步到docker(使用卷:volumes,来做同步) # 停止容器 ctrl + c # 修改compose.yaml services: server: build: context: . ports: - 9000:80 volumes: - .:/var/www/html
# 注意:再次构建时,dockerfile可以屏蔽composer的thinkphp8的框架安装了。
# 重新构建容器 docker compose up --build 再次访问localhost:9000,修改本地的代码,docker容器里的代码也会被修改。
结束。
注意事项: 之所以要在docker安装tp8框架,而不是直接从本地同步过去,是因为tp8要求php必须在8.0以上,我本地的是7.4,不具备框架安装条件。 docker版本为:4.37.2 (179585) Docker Engine配置(尽可能使用国内镜像源:https://www.coderjia.cn/archives/dba3f94c-a021-468a-8ac6-e840f85867ea) { "builder": { "gc": { "defaultKeepStorage": "20GB", "enabled": true } }, "experimental": false, "registry-mirrors": [ "https://hub.littlediary.cn", "https://docker.rainbond.cc", "https://docker.unsee.tech", "https://docker.m.daocloud.io", "https://hub.crdz.gq", "https://docker.nastool.de", "https://hub.firefly.store", "https://registry.dockermirror.com", "https://docker.1panelproxy.com", "https://hub.rat.dev", "https://docker.udayun.com", "https://docker.kejilion.pro", "https://dhub.kubesre.xyz", "https://docker.1panel.live", "https://dockerpull.org" ] }