windows11 下搭建 Docker 开发环境记录(少踩坑版)

前置:

# 安装步骤:
# 1、打开 linux 子系统和虚拟机平台
# 2、store 安装 windows terminal
# 3、安装 vscode 
# 4、store 搜索 ubuntu 安装一个
# 5、点击启动设置用户名密码
# 6、安装 Docker Desktop (不要勾选代替 hyper-v,否则造成站点访问慢)(安装完也可在设置取消勾选 Use the WSL 2 based engine:若已编译完成需重新编译容器)
# 7、设置打开 wsl2 & Resources WSL integration 打开安装的版本

1、 克隆仓库:

git clone https://github.com/Laradock/laradock.git | git clone https://hub.fastgit.org/Laradock/laradock.git

2、复制 env

cp .env.example .env

3、修改 .env 配置

# 版本及路径根据实际需要修改
APP_CODE_PATH_HOST=../code
DATA_PATH_HOST=../data
PHP_VERSION=7.2.34
MYSQL_VERSION=5.7.34
CHANGE_SOURCE=true #(In China) 
WORKSPACE_TIMEZONE=Asia/Shanghai
WORKSPACE_NODE_VERSION=18.17.1 WORKSPACE_NVM_NODEJS_ORG_MIRROR=https://npm.taobao.org/mirrors/node WORKSPACE_NPM_REGISTRY=https://registry.npm.taobao.org
MYSQL_VERSION=5.7.42
#各项扩展(建议只打开用到的,防止编译错误) 
# 默认 mysql 及 redis 密码是否需要更改
修改 workspace/Dockerfile
每个 https://raw.githubusercontent.com 前面加上 https://ghproxy.com/ 如果需要在容器里执行 npm run dev 或 vite 要把端口映射出来

统一时区:修改 docker-compose.yml

nginx/redis/: port 后面增加: environment: - TZ=${WORKSPACE_TIMEZONE}

4、启动 docker 容器

docker compose up -d nginx mysql redis
# (php-fpm 和 workspace 为默认编译 docker-in-docker) 
# 可选 phpmyadmin(localhost:8081 mysql root root)

# 启动容器后如需修改端口映射或其它 .env 配置修改后停止容器,
# 然后执行 docker compose up -d xxx 就可以了

5、执行命令

docker ps # 找到 CONTAINER ID docker exec -it xxx bash

# 在主机修改项目配置:
DB_HOST=mysql
REDIS_HOST=redis
...

6、新增扩展重新编译:

docker compose build xxx #e.g: docker compose build workspace

7、多 PHP 版本

# .env 增加配置
PHP81_VERSION=8.1
# docker-compose.yml: 复制一份 PHP_FPM php-fpm81
LARADOCK_PHP_VERSION=${PHP81_VERSION}
volumes: - ./php-fpm/php${PHP81_VERSION}.ini:/usr/local/etc/php/php.ini
expose: - "9081" # 自定义端口

# 站点修改:
fastcgi_pass php-fpm82:9082;
nginx: depends_on: - php-fpm - php-fpm82
# 启动
docker compose up -d php-fpm81

8、多 workspace

# docker-compose.yml: 复制一份 workspace workspace81
LARADOCK_PHP_VERSION=${PHP81_VERSION}
# 不同版本 PHP 扩展不兼容的在复制的版本里直接指定

# 启动
docker compose up -d workspace81

重启所有容器:

docker restart $(docker ps -a -q)

设置开启自启:

docker update --restart=always <containerName>

附:

导入主机 sql 文件:
docker cp ./dir docker-id:/home/tmp/
docker exec -it docker-id bash
cd /home/tmp
mysql -uroot -p
use xxx;
source xxx.sql
迁移 Docker 存储位置(释放 C 盘空间):
# 停止 Docker 容器:
docker compose stop
# 关闭发行版:
wsl --shutdown
# 导出 docker-desktop-data 发行版:
wsl --export docker-desktop-data D:\wsl\docker\tmp\docker-desktop-data.tar 
# 注销 docker-desktop-data 发行版:
wsl --unregister docker-desktop-data
# 导入 docker-desktop-data 到期望迁移的目录:
wsl --import docker-desktop-data D:\wsl\docker\data\ D:\wsl\docker\tmp\docker-desktop-data.tar --version 2
# 删除导出临时文件、迁移另一个 Docker 发行版 docker-desktop (可选,占用空间很小) 
# 启动 docker destktop 启动容器:
docker compose start nginx mysql redis
编译错误:
# 打开 source_change 关闭代理 反之打开代理 多试几次
Docker 国内加速
# 网易:
https://hub-mirror.c.163.com
# 百度:
https://mirror.baidubce.com
GPK 错误:
# wsl 里执行
sudo apt-key adv --keyserver keyserver.ubuntu.com --recv-keys 23E7166788B63E1E

~ 下新增 .wslconfig

wsl 占用内存过高问题

# Settings apply across all Linux distros running on WSL 2
[wsl2]

# Limits VM memory to use no more than 4 GB, this can be set as whole numbers using GB or MB
memory=2GB 

# Sets the VM to use two virtual processors
processors=1

# Specify a custom Linux kernel to use with your installed distros. The default kernel used can be found at https://github.com/microsoft/WSL2-Linux-Kernel
# kernel=C:\\temp\\myCustomKernel

# Sets additional kernel parameters, in this case enabling older Linux base images such as Centos 6
# kernelCommandLine = vsyscall=emulate

# Sets amount of swap storage space to 8GB, default is 25% of available RAM
swap=2GB

# Sets swapfile path location, default is %USERPROFILE%\AppData\Local\Temp\swap.vhdx
# swapfile=C:\\temp\\wsl-swap.vhdx

# Disable page reporting so WSL retains all allocated memory claimed from Windows and releases none back when free
# pageReporting=false

# Turn off default connection to bind WSL 2 localhost to Windows localhost
localhostforwarding=true

# Disables nested virtualization
# nestedVirtualization=false

# Turns on output console showing contents of dmesg when opening a WSL 2 distro for debugging
# debugConsole=true
本作品采用《CC 协议》,转载必须注明作者和本文链接
《L05 电商实战》
从零开发一个电商项目,功能包括电商后台、商品 & SKU 管理、购物车、订单管理、支付宝支付、微信支付、订单退款流程、优惠券等
《G01 Go 实战入门》
从零开始带你一步步开发一个 Go 博客项目,让你在最短的时间内学会使用 Go 进行编码。项目结构很大程度上参考了 Laravel。
讨论数量: 9

hyper-v 确实会拖慢速度 :+1:

7个月前 评论
slowlyo

可以, 很全. 我最近也是把本地环境换成了 Docker 目前来看 很不错, laradock 很好用~

doge

7个月前 评论
666666 7个月前

真够繁琐的

7个月前 评论

我这边是直接拉个centos的镜像,然后镜像里面用宝塔,感觉还是很方便,操作没这么繁琐

7个月前 评论

我搭建起来的laradock 网页访问非常慢,怎么处理?

7个月前 评论
qingye (楼主) 6个月前

我想指导多个 php 版本在一个 workspace 中怎么调用 php 命令;另外如果一个 php版本对应一个 workspace 是不是映射端口也需要改变

6个月前 评论
qingye (楼主) 6个月前

讨论应以学习和精进为目的。请勿发布不友善或者负能量的内容,与人为善,比聪明更重要!