# Docker 本地环境操作流程
## 一、安装 Docker Desktop
### 1.1 下载
官网:https://www.docker.com/products/docker-desktop/
选择 **Windows x86_64** 版本下载。
### 1.2 安装
双击安装程序,**勾选** `Use WSL 2 instead of Hyper-V`,其他默认。
安装完成后**重启电脑**。
### 1.3 启动
重启后,双击桌面 **Docker Desktop** 图标,等待右下角鲸鱼图标稳定(不转圈)。
### 1.4 验证安装
打开 CMD 或 PowerShell,执行:
docker --version
预期输出:`Docker version 26.1.4, build 5650f9b`
---
## 二、配置镜像加速器(国内必做)
Docker Desktop → **Settings** → **Docker Engine**,替换为:
```json
{
"registry-mirrors": [
"https://docker.1ms.run",
"https://docker.xuanyuan.me"
]
}
```
点击 **Apply & Restart**。
---
## 三、准备项目文件
### 3.1 确保项目目录结构
```
D:\laragon\www\engine-api\
├── Dockerfile
├── docker/
│ ├── nginx.conf
│ └── supervisord.conf
├── 其他项目文件...
```
### 3.2 创建必要目录
cd D:\laragon\www\engine-api
mkdir docker
---
## 四、编写配置文件
### 4.1 Dockerfile
**文件位置**:`D:\laragon\www\engine-api\Dockerfile`
//dockerfile
FROM php:8.3-fpm-alpine
RUN apk add --no-cache nginx supervisor
RUN docker-php-ext-install pdo_mysql
COPY docker/nginx.conf /etc/nginx/nginx.conf
COPY docker/supervisord.conf /etc/supervisor/conf.d/supervisord.conf
WORKDIR /var/www/html
COPY . .
RUN chown -R www-data:www-data storage bootstrap/cache \
&& chmod -R 775 storage bootstrap/cache
RUN composer install --no-interaction --no-dev --optimize-autoload
EXPOSE 80
CMD ["/usr/bin/supervisord", "-n", "-c", "/etc/supervisor/conf.d/supervisord.conf"]
### 4.2 Nginx 配置
**文件位置**:`D:\laragon\www\engine-api\docker\nginx.conf`
```nginx
user www-data;
worker_processes auto;
pid /run/nginx.pid;
events {
worker_connections 1024;
}
http {
sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 65;
include /etc/nginx/mime.types;
default_type application/octet-stream;
server {
listen 80;
server_name _;
root /var/www/html/public;
index index.php;
location / {
try_files $uri $uri/ /index.php?$query_string;
}
location ~ \.php$ {
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}
}
}
```
### 4.3 Supervisor 配置
**文件位置**:`D:\laragon\www\engine-api\docker\supervisord.conf`
```conf
[supervisord]
nodaemon=true
logfile=/var/log/supervisor/supervisord.log
pidfile=/run/supervisord.pid
[program:php-fpm]
command=php-fpm
autostart=true
autorestart=true
stdout_logfile=/var/log/supervisor/php-fpm.log
stderr_logfile=/var/log/supervisor/php-fpm-error.log
[program:nginx]
command=nginx -g "daemon off;"
autostart=true
autorestart=true
stdout_logfile=/var/log/supervisor/nginx.log
stderr_logfile=/var/log/supervisor/nginx-error.log
```
---
## 五、构建镜像
cd D:\laragon\www\engine-api
docker build -t codeyoursmile/haozhanzhan:latest .
等待构建完成(首次构建较慢,需要下载基础镜像)。
---
## 六、运行容器
docker run -d -p 8080:80 --name haozhanzhan codeyoursmile/haozhanzhan:latest
参数说明:
- `-d`:后台运行
- `-p 8080:80`:宿主机 8080 端口映射到容器 80 端口
- `--name haozhanzhan`:容器名称
## 七、验证
```bash
# 查看容器状态
docker ps
# 测试访问
curl http://localhost:8080
浏览器打开 `http://localhost:8080`,看到好站站安装页面即成功。
## 八、常用命令
| 操作 | 命令 |
|------|------|
| 查看镜像 | `docker images` |
| 查看运行中容器 | `docker ps` |
| 停止容器 | `docker stop haozhanzhan` |
| 启动容器 | `docker start haozhanzhan` |
| 删除容器 | `docker rm haozhanzhan` |
| 删除镜像 | `docker rmi haozhanzhan` |
| 查看容器日志 | `docker logs haozhanzhan` |
| 进入容器内部 | `docker exec -it haozhanzhan sh` |