win11下使用wsl2 + docker 打造前端开发环境

1,254 阅读28分钟

环境

  • 操作系统:win11家庭版 版本号: 24H2 (版本:26100.3476)
  • CPU类型: 英特尔CPU

安装wsl2

启用Linux子系统

控制面板(查看方式:小图标) > 程序和功能 > 启用或关闭Windows功能

image.png

更新wsl版本

# 更新wsl为最新版本
wsl --update

如果上面的命令下载速度过慢,则可以采用离线方式更新(使用下面的地址,下载最新的稳定版msi安装包,然后双击安装)

Releases · microsoft/WSL · GitHub

image.png

开启虚拟机平台的功能

管理员角色打开PowerShell, 执行如下命令

dism.exe /Online /Enable-Feature /FeatureName:VirtualMachinePlatform /All

此时,会需要重启

查看当前版本

# 查看当前版本
wsl --version
# 将wsl设置为2(win11默认是2,可以不设置)
wsl --set-default-version 2

image.png

wsl2常用命令

  • 列出已安装发行版和发行版运行状态: wsl -l -v
  • 运行某个发行版: wsl -d Ubuntu-24.04(此时会以默认用户登录) wsl -d Ubuntu-20.04 -u yourusername (此时会以特定用户登录)
  • 终止(关闭)指定发行版: wsl --terminate <Distribution Name>
  • 从发行版的命令行中退出(并让发行版继续运行): 先执行sync, 再执行logout
  • 查看默认发行版: wsl -l
  • 设置默认发行版: wsl --set-default Ubuntu-24.04
  • 查看wsl状态: wsl --status
  • 查看wsl版本: wsl --version
  • 关闭wsl: wsl --shutdown
  • 导出发行版: wsl --export <Distribution Name> <FileName>
  • 导入发行版: wsl --import <Distribution Name> <InstallLocation> <FileName>
  • 注销或卸载 Linux 发行版: wsl --unregister <DistributionName>
  • 返回通过 WSL 2 安装的 Linux 发行版 IP 地址(WSL 2 VM 地址): wsl hostname -I
  • 返回从 WSL 2 (WSL 2 VM) 看到的 Windows 计算机的 IP 地址: ip route show | grep -i default | awk '{ print $3}'

安装一个 WSL Linux 发行版

从微软应用商店安装(选择一个发行版): ubuntu - Microsoft Apps

我选的是Ubuntu 24.04.1 LTS

安装完毕之后,第一次打开会弹出窗口进行初始化操作,并让你设置账户和密码 (请务必记住该账号和密码)。账户密码设置完后就会自动进入发行版系统,接下来就可以探索咱们安装的Linux系统了

将更新源设置为国内源

注意:新版本的ubuntu更新源设置与旧版本的不同,我这个是新版本更新源设置

备份源配置

sudo cp /etc/apt/sources.list.d/ubuntu.sources  /etc/apt/sources.list.d/ubuntu.sources.bak

编辑源配置

sudo vim /etc/apt/sources.list.d/ubuntu.sources

将文件内容替换为:

Types: deb
URIs: http://mirrors.aliyun.com/ubuntu/
Suites: noble noble-updates noble-security
Components: main restricted universe multiverse
Signed-By: /usr/share/keyrings/ubuntu-archive-keyring.gpg

更新

# 更新软件源
sudo apt-get update
# 升级所有软件
sudo apt-get upgrade

美化命令行 (可选)

sudo apt install zsh git curl -y

sh -c "$(curl -fsSL https://gitee.com/pocmon/ohmyzsh/raw/master/tools/install.sh)"

# 更换主题
git clone --depth=1 https://gitee.com/romkatv/powerlevel10k.git ${ZSH_CUSTOM:-$HOME/.oh-my-zsh/custom}/themes/powerlevel10k

vim ~/.zshrc

# 在~/.zshrc文件中,将主题设置为 powerlevel10k/powerlevel10k
ZSH_THEME="powerlevel10k/powerlevel10k"

因为这个主题的自由度比较高,因此会需要你进行一系列的选择

安装zsh插件

命令补全插件

git clone https://github.moeyy.xyz/https://github.com/zsh-users/zsh-autosuggestions ${ZSH_CUSTOM:-~/.oh-my-zsh/custom}/plugins/zsh-autosuggestions

命令高亮插件

git clone https://github.moeyy.xyz/https://github.com/zsh-users/zsh-syntax-highlighting.git ${ZSH_CUSTOM:-~/.oh-my-zsh/custom}/plugins/zsh-syntax-highlighting

启用插件

vim ~/.zshrc

plugins=(git zsh-autosuggestions zsh-syntax-highlighting z extract web-search)
source ~/.zshrc

zsh设置为默认shell (配置好之后下次打开默认就是 zsh 了,也可以输入zsh进入 zsh 环境)

chsh -s /bin/zsh

image.png

安装git

sudo apt install git -y

# 启用大小写敏感
git config --global core.ignorecase false

# 配置用户名和密码
git config --global user.name "Your Name"  
git config --global user.email "youremail@domain.com"

# 生成 ssh key  
# 用 Github 的话,可以拷贝生成的公钥到 https://github.com/settings/keys  
ssh-keygen

新增用户

sudo adduser username

username 替换为你想创建的用户名

该命令将提示你输入并确认新用户的密码,并提供一些可选的用户信息(如全名、房间号等)。你可以直接按 Enter 跳过这些可选项。

为新用户赋予管理员权限(可选)

sudo usermod -aG sudo username

# 这里是新建用户组,并将用户加入docker用户组。
# 因为: Docker 守护进程的 Unix 套接字文件 `/var/run/docker.sock`,
# 默认仅允许 `root` 用户或 `docker` 用户组成员操作,后续我需要使用该用户操作docker,
# 因此,我需要将该用户添加到docker用户组
sudo groupadd docker
sudo usermod -aG docker username

# 重新加载用户组权限(需重新登录或执行以下命令)
newgrp docker

# 查看当前用户所属组是否包含 docker
groups

这样,新用户将能够使用 sudo 命令来执行需要管理员权限的操作。

切换到新用户

su - username

修改发行版位置,避免C盘空间被占用

# 停止发行版运行
wsl --terminate Ubuntu-24.04
# 查看发行版运行状态
wsl -l -v
# 导出指定发行版
wsl --export Ubuntu-24.04 D:\wsl2\bak\Ubuntu-24.tar
# 注销发行版运行
wsl --unregister Ubuntu-24.04
# 查看是否注销成功
wsl -l -v
# 重新导入发行版
wsl --import Ubuntu-2404 D:\wsl2\Ubuntu-24 D:\wsl2\bak\Ubuntu-24.tar
# 重新运行发行版
wsl -d  Ubuntu-2404

永久修改发行版的默认登录用户

# 进入发行版,查看是否存在该用户
cat /etc/passwd | grep xxx
sudo vim /etc/wsl.conf
# 加入如下内容
[user]
default = <用户名>

退出重启linux发行版

# 退出(这个在linux命令行执行)
logout
# 关闭特定发行版(这个在win命令行执行)
wsl --terminate Ubuntu-2404
# 重启
wsl -d  Ubuntu-2404

ubuntu安装docker

将当前用户授予管理员权限

查看当前用户是否有管理员权限

groups

若输出中包含 sudo 或 admin 组,则用户拥有管理员权限

测试是否真的有 sudo 权限

sudo -l
  • 若显示 (ALL : ALL) ALL 或类似内容,表示用户拥有完整 sudo 权限
  • 若提示输入密码后成功执行命令,则用户具有临时管理员权限

如果没有sudo权限,则将当前用户添加到sudo

sudo usermod -aG <组名> <用户名> 

卸载docker

sudo apt purge docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin docker-ce-rootless-extras
sudo rm -rf /var/lib/docker
sudo rm -rf /var/lib/containerd

安装docker

安装需要的依赖包

sudo apt install apt-transport-https ca-certificates curl gnupg2 software-properties-common

添加阿里云的Docker CE仓库的GPG密钥

curl -fsSL https://mirrors.aliyun.com/docker-ce/linux/ubuntu/gpg | sudo apt-key add -

添加阿里云的仓库

sudo add-apt-repository "deb [arch=amd64] https://mirrors.aliyun.com/docker-ce/linux/ubuntu $(lsb_release -cs) stable"

再次更新软件源

sudo apt update 
sudo apt upgrade

安装Docker

sudo apt-get install docker-ce docker-ce-cli containerd.io

# 查看docker版本
docker --version

配置国内的镜像加速源

sudo vim /etc/docker/daemon.json
{
   "registry-mirrors": [
   "https://mirror.baidubce.com",
   "https://mirror.ccs.tencentyun.com",
   "https://docker.mirrors.ustc.edu.cn",
   "https://docker.mirrors.ustc.edu.cn",
   "https://docker.1panel.live",
   "https://b9pmyelo.mirror.aliyuncs.com",
   "https://gmbtuhv4.mirror.aliyuncs.com",
   "https://docker.nju.edu.cn"
  ]
}

安装vieux/sshfs插件

查看所有已安装插件

docker plugin ls

作用: 让容器可以使用远程数据卷

docker plugin install --grant-all-permissions vieux/sshfs

创建远程数据卷示例:

docker volume create \
  --driver vieux/sshfs \
  -o sshcmd=user@remote_host:/远程路径 \
  -o port=2222 \
  -o password=your_password \
  my_remote_volume

启动容器时挂载远程数据卷:

docker run -v my_remote_volume:/容器路径 --rm your_image

查看所有数据卷

docker volume ls

查看数据卷详情

docker volume inspect <volume_name>

清理未使用的数据卷

docker volume prune

删除单个数据卷

docker volume rm <volume_name>

删除容器时,同时删除关联的数据卷

docker rm -v <container_name>

加载配置文件并重启

# 查看运行状态
sudo systemctl status docker.service
# 重新加载配置文件
sudo systemctl reload docker
# 查看docker配置信息是否更新成功
sudo docker info
# 重启
sudo systemctl restart docker
# 查看运行状态
sudo systemctl status docker.service
# 启动docker
sudo systemctl start docker

# 停止docker运行
sudo systemctl stop docker
sudo systemctl stop docker.socket

# 查看docker运行状态
sudo systemctl status docker.service docker.socket

运行测试容器

sudo docker run hello-world

docker run 命令的参数

后台运行

docker run -d -p 8080:80 nginx

-d: 就表示后台运行

设置容器名称

docker run -d -p 8080:80 --name web nginx

--name web:将容器名称设置成了web

挂载数据卷实现动态配置

适用场景:需持久化配置或频繁修改,且需配置与容器解耦。适合开发调试环境

操作步骤

  1. 启动容器时挂载宿主机目录
docker run -d -v /宿主机/配置目录:/容器/配置目录 <镜像名>

3. 在宿主机直接编辑配置文件

vi /宿主机/配置目录/config.conf  # 修改后自动同步至容器

5. 触发容器内服务重载配置(无需重启容器)

docker exec <容器名> nginx -s reload  # 例如 Nginx 重载配置
启动容器时指定网络

docker run --network my_custom_network -d nginx

端口映射
  • 映射单个端口: docker run -d -p 8080:80 nginx
  • 映射多个端口: docker run -d -p 8080:80 -p 8443:443 nginx
  • 动态端口映射(Docker自动分配宿主机端口而不是手动指定): docker run -d -P nginx
docker run -d -p 0.0.0.0:8080:80 --name my_nginx nginx

docker常用命令

  • 查看docker版本: docker --version
  • 列出所有本地镜像: sudo docker image ls
  • 列出所有使用中的容器: sudo docker container ls
  • 列出所有本地容器: sudo docker ps -a
  • 将容器提交为新的镜像: docker commit <CONTAINER_ID> <NEW_IMAGE_NAME>:<TAG>
  • 导出镜像: docker save -o my-custom-ubuntu-v1.tar my-custom-ubuntu:v1
  • 导入镜像: docker load -i my-custom-ubuntu-v1.tar
  • 删除指定容器: docker rm id/名称
  • 删除所有容器: docker rm $(docker ps -a -q)
  • 删除所有镜像: docker rmi $(docker images -q)
  • 删除指定镜像(如果镜像被引用会删除失败): docker rmi id/名称
  • 查看容器详细信息: docker inspect <容器名称或 ID>
  • 清理所有未使用资源(包括镜像、容器、网络和构建缓存):docker system prune
  • 查看容器的ip地址: sudo docker inspect -f '{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' <容器id>
  • 查看容器端口映射: sudo docker inspect -f '{{json .NetworkSettings.Ports}}' <container_id>
  • 查看docker的所有网络: sudo docker network ls
  • 查看docker网络的详细信息: sudo docker network inspect <网络id>
  • 创建自定义网络: docker network create my_custom_network
  • 在容器外执行容器内命令(这里的5ca578c91df9是容器id, nginx -v是容器内部命令): sudo docker exec 5ca578c91df9 nginx -v
  • 进入运行中的容器的终端: docker exec -it <容器名或ID> /bin/bash
  • 重启容器: docker restart <容器名>
  • 将宿主机文件复制到容器: docker cp <宿主机路径> <容器名>:<容器内文件路径>
  • 将容器中文件复制到宿主机: docker cp <容器名>:<容器内文件路径> <宿主机路径>
  • 恢复已停止容器的运行: docker start <容器名或ID>
  • docker整个重启: sudo systemctl restart docker
  • docker在外部运行容器内部命令并删除容器: docker run --rm <镜像名> <要运行的命令> 如: docker run --rm ubuntu ls /app
  • 查看所有数据卷: docker volume ls
  • 查看数据卷详情: docker volume inspect <volume_name>
  • 清理未使用的数据卷: docker volume prune
  • 删除单个数据卷: docker volume rm <volume_name>
  • 删除容器时,同时删除关联的数据卷: docker rm -v <container_name>
  • 查看所有已安装插件: docker plugin ls
  • 禁用插件: docker plugin disable <PLUGIN_ID_OR_NAME>
  • 删除插件(插件必须先禁用再删除): docker plugin rm <PLUGIN_ID_OR_NAME>
  • 通过docker compose停止容器运行: docker compose stop
  • 通过docker compose重启服务: docker compose start
  • 通过docker compose完全重新创建并启动容器: docker compose up -d
  • 通过docker compose停止容器运行,并删除容器和网络: docker compose down
  • 通过docker compose停止容器运行,并删除容器和网络, 卷: docker compose down --volumes --rmi all
  • 通过docker compose查看容器状态: docker compose ps

安装docker-compose

# 添加 Docker Compose 软件源
sudo apt-get update
sudo apt-get install ca-certificates curl
sudo install -m 0755 -d /etc/apt/keyrings
sudo curl -fsSL https://mirrors.tuna.tsinghua.edu.cn/docker-ce/linux/ubuntu/gpg -o /etc/apt/keyrings/docker.asc
sudo chmod a+r /etc/apt/keyrings/docker.asc

# 添加清华镜像源
echo \
  "deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.asc] https://mirrors.tuna.tsinghua.edu.cn/docker-ce/linux/ubuntu \
  $(. /etc/os-release && echo "$VERSION_CODENAME") stable" | \
  sudo tee /etc/apt/sources.list.d/docker.list > /dev/null
sudo apt-get update
sudo apt-get install docker-compose-plugin
docker compose version
# 输出示例:Docker Compose version v2.27.1

安装verdaccio

注意: verdaccio的web界面只能查看和搜索自己发布到私服的包,对于其他第三方包,是无法查看和搜索的 部分npm包在verdaccio容器中存在也可以安装,但是web界面不显示也搜不到这个npm包 · verdaccio · Discussion #4725 · GitHub

创建verdaccio, 并在verdaccio目录下创建如下目录和文件

config/config.yaml  
docker-compose.yml  
logs
plugins
storage
# 修改文件夹权限,不修改文件夹权限可能造成verdaccio无法在这些文件中创建文件或目录,从而导致包无法缓存
chown -R 777 logs plugins storage

docker-compose.yml内容

services:
  verdaccio:
    image: verdaccio/verdaccio:latest
    container_name: verdaccio
    environment:
      - VERDACCIO_PORT=4873
      - VERDACCIO_UID=1000    # 设置与宿主机用户相同的 UID/GID(避免权限问题)
      - VERDACCIO_GID=1000
    ports:
      - "4873:4873"
    volumes:
      - ./storage:/verdaccio/storage    # 持久化存储
      - ./config/config.yaml:/verdaccio/conf/config.yaml
      - ./plugins:/verdaccio/plugins    # 插件目录(可选)
      - ./logs:/verdaccio/logs # 日志目录
    restart: unless-stopped

config/config.yaml内容

配置详见: Verdaccio配置

listen:
  - 0.0.0.0:4873 # listen on all addresses (INADDR_ANY)

server:
  keepAliveTimeout: 60

web:
  title: My Private npm (verdaccio)
  logo: /verdaccio/logo.png

auth:
  htpasswd:
    # 用户信息存放的文件
    file: /verdaccio/conf/htpasswd
    # 允许用户注册的数量,默认1000,设为-1时,不允许用户通过npm adduser注册。但是可以通过直接编写htpasswd file内容的方式添加用户
    max_users: 20 # 最多可以注册 20 个用户
    # hash 算法,选项可以是 "bcrypt", "md5", "sha1", "crypt",默认 crypt
    algorithm: bcrypt
    # “bcrypt”的轮数,对于其他算法将被忽略,值设的越高,验证所需时间越久,大于 10 时 CPU 使用率显著增加,并在处理请求时产生额外的延迟
    rounds: 6
    # 如果密码的验证时间超过此持续时间(以毫秒为单位),则记录警告
    slow_verify_ms: 1000

uplinks:
  npmjs:
    url: https://registry.npmmirror.com/  # 阿里源地址
    agent_options:
      keepAlive: true
      maxSockets: 40
      maxFreeSockets: 10
  npmjs2:
    url: https://registry.npmjs.org/  # 官方源
    agent_options:
      keepAlive: true
      maxSockets: 40
      maxFreeSockets: 10

packages:
  '@*/*':
    access: $all
    publish: $authenticated
    unpublish: $authenticated
    proxy: npmjs npmjs2

  '**':
    access: $all
    publish: $authenticated
    unpublish: $authenticated
    proxy: npmjs npmjs2

middlewares:
  audit:
    enabled: true

i18n:
  web: zh-CN

# 日志层级可选: info, http, debug
# /verdaccio/logs/verdaccio.log 这个是指容器中的目录
log:
  - {type: stdout, format: pretty, level: debug}

获取verdacio的用户token

什么场景需要使用用户token?

答: 自动化CI/CD场景需要使用用户token, 达到免密操作

# 先登录
npm login

# 再查看.npmrc配置文件
cat ~/.npmrc

image.png

上图红线部分就是我们需要的用户token

启动服务:

docker compose up -d 

查看容器运行状态

docker compose ps -a

停止容器运行:

docker compose stop

查看日志:

docker compose logs -f

删除容器

docker compose down

安装gitea和postgresql

新建docker-compose.yml文件,内容如下

networks:
  gitea:
    external: false

services:
  server:
    image: gitea/gitea:1.21.1
    container_name: gitea
    environment:
      - USER_UID=1000
      - USER_GID=1000
      - GITEA__database__DB_TYPE=postgres
      - GITEA__database__HOST=db:5432
      - GITEA__database__NAME=gitea
      - GITEA__database__USER=gitea
      - GITEA__database__PASSWD=gitea
    restart: always
    networks:
      - gitea
    volumes:
      - ./gitea:/data
      - /etc/timezone:/etc/timezone:ro
      - /etc/localtime:/etc/localtime:ro
    ports:
      - "3100:3000"
      - "222:22"
    depends_on:
      - db

  db:
    image: postgres:17
    container_name: postgres-17
    restart: always
    ports:
      - "5432:5432"
    environment:
      - POSTGRES_USER=gitea
      - POSTGRES_PASSWORD=gitea
      - POSTGRES_DB=gitea
    networks:
      - gitea
    volumes:
      - ./postgres:/var/lib/postgresql/data

安装并启动容器

docker compose up -d

ubuntu中安装postgresql客户端

sudo apt-get install postgresql-client

连接容器中的数据库

psql -h localhost -p 5432 -U gitea -d gitea

查看所有表

# 方式一(列出所有表)
\dt

# 方式二(显示大小和描述)
\dt+

# 方式三(列出所有非系统表及其所属模式)
SELECT table_schema, table_name, table_type
FROM information_schema.tables
WHERE table_schema NOT IN ('pg_catalog', 'information_schema')
  AND table_type = 'BASE TABLE';
  
# 方式四
SELECT schemaname, tablename, tableowner
FROM pg_catalog.pg_tables
WHERE schemaname NOT IN ('pg_catalog', 'information_schema');

# 查看特定模式下的表
-- 查看模式 `public` 下的所有表
\dt public.*
-- 或
SELECT table_name
FROM information_schema.tables
WHERE table_schema = 'public';

# 查看表详细信息
-- 列出表名、行数、大小(需安装扩展)
CREATE EXTENSION IF NOT EXISTS pgstattuple;
SELECT 
  relname AS table_name,
  pg_size_pretty(pg_total_relation_size(relid)) AS total_size,
  n_live_tup AS row_count
FROM pg_catalog.pg_statio_user_tables;

让局域网用户,通过win11的ip访问wsl2中ubuntu中docker容器的服务

通过镜像网络实现

%UserProfile%\.wslconfig

[wsl2]
networkingMode=mirrored # 启用镜像网络模式,使 WSL2 与 Windows 共享网络
dnsTunneling=true # 启用 DNS 隧道,提高与 VPN 等复杂网络的兼容性
autoProxy=true # 同步 Windows 的代理设置到 WSL2
firewall=true # 启用 Windows 防火墙集成

[experimental]
autoMemoryReclaim=gradual
sparseVhd=true
hostAddressLoopback=true # 允许通过主机的局域网 IP 访问 WSL2 中的服务

wsl --shutdown

验证镜像网络模式

在 WSL2 中运行以下命令查看 IP 地址

ip addr

如果 IP 地址与 Windows 主机的 IP 地址一致,说明镜像网络模式已启用。

让win11能够在仅安装docker cli的情况下,操作远程docker

进入远程服务器,查看远程服务器上的docker版本

我们这里的远程服务器就是指wsl2中的ubuntu

docker version

可以看到我们的docker版本是: 28.0.1

Client: Docker Engine - Community
 Version:           28.0.1
 API version:       1.48
 Go version:        go1.23.6
 Git commit:        068a01e
 Built:             Wed Feb 26 10:41:12 2025
 OS/Arch:           linux/amd64
 Context:           default

下载docker命令行工具

这里下载 docker 命令行工具,选择对应版本即可

若是无法打开上面的网址,那就按照这个格式拼接下载地址,再通过迅雷下载

download.docker.com/win/static/…

将压缩包解压,并将文件放入一个文件夹,如: d:\app\docker-cli-28.0.1文件夹

这里下载 docker-compose 命令行工具

若是无法打开上面的网址,那就按照这个格式拼接下载地址,再通过迅雷下载

github.com/docker/comp…

同样将docker-compose-windows-x86_64.exe重名为docker-compose.exe文件后,放入,如: d:\app\docker-cli-28.0.1文件夹

image.png

image.png

找到 docker.service 文件位置

find -name "docker.service"

./multi-user.target.wants/docker.service

编辑docker.service文件

找到如下内容,并注释掉

ExecStart=/usr/bin/dockerd -H fd:// --containerd=/run/containerd/containerd.sock

替换为, 增加如下内容

ExecStart=/usr/bin/dockerd -H tcp://0.0.0.0:2375 -H unix:///var/run/docker.sock

image.png

重启docker服务

sudo systemctl daemon-reload && sudo systemctl restart docker

在你本地通过浏览器访问http://{服务器IP}:2375/version, 当看到页面显示一串JSON时表示已开放远程访问。

访问wsl2中的docker则是: http://localhost:2375/version, 当看到页面显示一串JSON时表示已开放远程访问。

{"Platform":{"Name":"Docker Engine - Community"},"Components":[{"Name":"Engine","Version":"28.0.1","Details":{"ApiVersion":"1.48","Arch":"amd64","BuildTime":"2025-02-26T10:41:12.000000000+00:00","Experimental":"false","GitCommit":"bbd0a17","GoVersion":"go1.23.6","KernelVersion":"5.15.167.4-microsoft-standard-WSL2","MinAPIVersion":"1.24","Os":"linux"}},{"Name":"containerd","Version":"1.7.25","Details":{"GitCommit":"bcc810d6b9066471b0b6fa75f557a15a1cbf31bb"}},{"Name":"runc","Version":"1.2.4","Details":{"GitCommit":"v1.2.4-0-g6c52b3f"}},{"Name":"docker-init","Version":"0.19.0","Details":{"GitCommit":"de40ad0"}}],"Version":"28.0.1","ApiVersion":"1.48","MinAPIVersion":"1.24","GitCommit":"bbd0a17","GoVersion":"go1.23.6","Os":"linux","Arch":"amd64","KernelVersion":"5.15.167.4-microsoft-standard-WSL2","BuildTime":"2025-02-26T10:41:12.000000000+00:00"}

配置win环境变量

DOCKER_HOST: tcp://localhost:2375
DOCKER_CLIENT: D:\app\docker-cli-28.0.1

并将:DOCKER_HOST设置到 PATH环境变量中,使得在所有地方,都可以调用docker-cli的命令

%DOCKER_CLIENT%

查看path环境变量中是否包含D:\app\docker-cli-28.0.1这个路径

# powershell 中查看path环境变量值
$env:Path

# cmd 中查看path环境变量值
echo %PATH%

验证是否成功

打开windows的CMD,在任意文件夹下执行docker ps查看是否显示服务器上的容器。

打开windows的CMD,在任意文件夹下执行docker-compose version看是否有效

让ubuntu在仅安装docker-ce-cli的情况下访问远程docker

ubuntu仅安装docker-ce-cli

安装需要的依赖包

sudo apt install apt-transport-https ca-certificates curl gnupg2 software-properties-common

添加阿里云的docker仓库

curl -fsSL https://mirrors.aliyun.com/docker-ce/linux/ubuntu/gpg | sudo apt-key add -
sudo add-apt-repository "deb [arch=amd64] https://mirrors.aliyun.com/docker-ce/linux/ubuntu $(lsb_release -cs) stable"

安装 docker-ce-cli

sudo apt update 
sudo apt upgrade
sudo apt-get install docker-ce-cli

# 查看docker版本 
docker --version

配置连接远程docker

# 尝试连接远程Docker守护进程(需提前配置)
export DOCKER_HOST="tcp://<远程主机IP>:2375"
docker ps  # 应返回远程主机的容器列表

安装ssh

安装

sudo apt install openssh-server

修改ssh配置

sudo vim /etc/ssh/sshd_config

# 修改默认端口(例如 2222)
Port 2222

# 禁止 root 用户登录
PermitRootLogin no

# 仅允许特定用户登录(例如允许用户 ubuntu)
AllowUsers ubuntu

# 禁用密码登录,仅允许密钥认证
PasswordAuthentication no

检查ssh配置

sudo sshd -t

配置修改后,需要重启ssh服务

sudo systemctl restart ssh

启动 SSH 服务

# 启动服务
sudo systemctl start ssh

# 停止服务
sudo systemctl stop ssh

# 重启服务
sudo systemctl restart ssh

# 查看服务状态
sudo systemctl status ssh

# 检查是否监听 2222 端口(替换为你的实际端口)
sudo ss -tuln | grep 2222

# 连接测试
ssh -p 2222 pan@localhost 

设置 SSH 开机自启

sudo systemctl enable ssh

创建远程数据卷,并在容器启动时挂载远程数据卷

创建远程数据卷示例:

docker volume create \
  --driver vieux/sshfs \
  -o sshcmd=user@remote_host:/远程路径 \
  -o password=your_password \
  my_remote_volume

启动容器时挂载远程数据卷:

docker run -v my_remote_volume:/容器路径 --rm your_image
  • 清理未使用的数据卷: docker volume prune
  • 删除单个数据卷: docker volume rm <volume_name>
  • 删除容器时,同时删除关联的数据卷: docker rm -v <container_name>

在ubuntu中,基于nexus3搭建私服

nexus-3网盘下载地址

安装jdk 8

sudo apt install -y wget openjdk-8-jdk unzip
java -version

解压与安装服务

tar -zxvf nexus-3.70.1-02-java8-unix.tar.gz
# 必须保持这个目录结构
sudo mv nexus-3.* /opt/nexus 
# 必须保持这个目录结构
sudo mv sonatype-work /opt/sonatype-work

编辑 Nexus 启动文件,设置非 root 用户运行

sudo nano /opt/nexus/bin/nexus.rc

不一定要设置为nexus, 只要是非root用户即可

run_as_user="nexus"

配置 Nexus 服务

sudo nano /etc/systemd/system/nexus.service
[Unit]
Description=Nexus Repository Manager
After=network.target

[Service]
Type=forking
LimitNOFILE=65536
# 用户必须与 /opt/nexus/bin/nexus.rc 中配置的一至
User=nexus
# 用户组必须是 /opt/nexus/bin/nexus.rc 中配置的用户,所在的用户组
Group=nexus
# 这里的nexus启动文件地址一定要写正确(这里只是示例)
ExecStart=/opt/nexus/bin/nexus start
# 这里的nexus启动文件地址一定要写正确(这里只是示例)
ExecStop=/opt/nexus/bin/nexus stop
Restart=on-abort

[Install]
WantedBy=multi-user.target

启动并启用 Nexus 服务

重新加载 Systemd 配置:

sudo systemctl daemon-reload

启动 Nexus 服务:

sudo systemctl start nexus

设置开机启动:

sudo systemctl enable nexus

检查服务状态:

sudo systemctl status nexus

访问 Nexus Web 界面

http://<你的服务器IP>:8081

首次登录时:

  • 默认用户名:admin
  • 默认密码:在 /opt/sonatype-work/nexus3/admin.password 文件中。
    查看密码命令:
cat /opt/sonatype-work/nexus3/admin.password

限制nexus内存占用

sudo nano /opt/nexus/bin/nexus.vmoptions
-Xms1G   # 设置 JVM 最小内存为 1GB
-Xmx1G   # 设置 JVM 最大内存为 1GB
-XX:MaxDirectMemorySize=1G  # 设置直接内存为 1GB

重启nexus服务

sudo systemctl restart nexus

查看 Nexus 的内存使用情况

ps aux | grep nexus

通过Nexus搭建Npm私库

【Nexus】通过Nexus搭建Npm私库_nexus npm-hosted-CSDN博客

通过nexus搭建docker私服

Nexus3中搭建Docker私服_如何通过nexus代理拉取外部镜像源-CSDN博客

将本地开发环境完全迁移到docker容器中

创建一个自定义镜像,该镜像做如下几个事情

  • 基于node18的镜像,将linux的源设置为国内源
  • 安装git,并将时区设置为中国的时区
  • 将npm源设置为国内镜像
  • 安装pnpm,并设置为国内镜像
  • 设置工作目录
  • 对外暴露4000端口(用于外部和内部端口映射)
  • 最后让容器无限休眠(作用:如果不无限休眠,则容器会在执行一次之后立刻退出)

Dockerfile

# 使用官方 Node.js 18 镜像作为基础(当前镜像将命名为: node18-pnpm8-cn:latest)
FROM node:18-bullseye

# 替换系统级软件包源为国内源(Debian 11 "bullseye")
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

# 更新软件包索引并安装工具链(包含 Git)
RUN apt-get update && \
    apt-get install -y --no-install-recommends \
    curl \
    wget \
    vim \
    git \
    tzdata \
    ca-certificates && \
    apt-get clean && \
    rm -rf /var/lib/apt/lists/*

# 设置时区
RUN ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime

# 设置 npm 国内镜像源
RUN npm config set registry https://registry.npmmirror.com

# 设置 pnpm 环境变量(在用户空间之前定义)
ENV PNPM_HOME=/pnpm
ENV PNPM_STORE_DIR=/pnpm/store
ENV PATH="$PNPM_HOME:$PATH"

# 创建目录
RUN mkdir -p $PNPM_STORE_DIR

# 可选:安装 pnpm
ENV PNPM_VERSION=8.15.3
RUN corepack enable && \
    corepack prepare pnpm@${PNPM_VERSION} --activate 

# 设置 pnpm 镜像地址
RUN pnpm config set registry https://registry.npmmirror.com
# 设置 pnpm 依赖存储目录
RUN pnpm config set store-dir /pnpm/store

# 设置工作目录
WORKDIR /workspace

# 对外暴露4000端口
EXPOSE 4000

# 无限休眠,使容器一直保持运行状态
CMD ["sleep", "infinity"] 

查看pnpm的存储目录

pnpm config get store-dir

构建镜像

docker build -t node18-pnpm8-cn .

创建一个vue3项目,将vue3项目挂载到容器中

创建远程数据卷

docker volume create \
  --driver vieux/sshfs \
  -o sshcmd=pan@localhost:/home/pan/project/demo-vue3-project \
  -o port=2222 \
  -o password=pan \
  demo-vue3-project

创建并运行容器

 docker run -itd -p 4000:4000 -v demo-vue3-project:/workspace node18-pnpm8-cn
  • -p 8100:4000: 将外部的4000端口,映射到容器的4000端口
  • -v demo-vue3-project:/workspace": 将项目目录以远程数据卷的形式,整个挂载到容器的工作目录

docker ps -a: 查看容器是否正常运行

此时,你的这个docker容器就是你本地的开发环境了

通过VSCode编辑与运行容器中的这个项目

通过VSCode连接WSL2中的Linux实例

VSCode安装好Remote Development开发工具

在WSL目标界面,连接到对应的wsl2的Linux实例

image.png

通过VSCode连接Linux上的docker容器

再在开发容器界面,连接到之前运行起来的容器,此时就能看到项目代码了

image.png

image.png

通过vscode的终端,就是容器的命令行

image.png

简化

  • 在项目根目录下创建 .devcontainer目录
  • .devcontainer目录创建devcontainer.json文件
{
    "name": "Vue3 Dev",
    "image": "node18-pnpm8-cn",
    "forwardPorts": [4000, 4000],
    "mounts": [
        "type=volume,source=demo-vue3-project,target=/workspace"
    ],
    "postCreateCommand": "pnpm install",
    "workspaceFolder": "/workspace"
}
  • name: 指定开发容器名,会显示在VSCode的左下角
  • image: 指定容器使用的镜像
  • forwardPorts: 配置端口映射
  • mounts: 数据卷挂载配置(type指定卷类型, source指定数据卷名, target指定容器的工作空间目录)
  • postCreateCommand: 指定容器首次启动后执行的命令
  • workspaceFolder: 指定容器的工作目录

有了这个.devcontainer/devcontainer.json配置文件后,VSCode会自动识别里面的内容,从而自动创建容器,且自动在容器中打开该项目

启动项目

通过VSCode命令行启动项目

注意点:

  • vite的honst要设置为0.0.0.0, 端口要设置为镜像中定义的4000端口

项目启动成功

image.png

这样就能在win的浏览器中访问了

http://localhost:4000

P.S. 修改项目代码之后,VITE能够实时热更新哈

通过docker安装redis, rabbit-mq, xxl-job, nginx

redis

docker-compose.yml

services:
  redis:
    image: redis:7.2  # 推荐稳定版本(可选 6.2/5.0)
    container_name: redis
    restart: unless-stopped
    environment:
      - REDIS_PLUGINS_DIR=/plugins  # 指定插件目录
    ports:
      - "6379:6379"
    volumes:
      - ./data:/data  # 数据持久化目录
      - ./conf/redis.conf:/usr/local/etc/redis/redis.conf  # 挂载配置文件
      - ./plugins:/plugins  # 插件目录
    command: redis-server /usr/local/etc/redis/redis.conf  # 指定配置文件启动

新建如下目录:

conf
data
plugins

conf/redis.conf

# 允许远程访问
bind 0.0.0.0
# 关闭保护模式
protected-mode no
# 访问端口
port 6379
# 设置访问密码
requirepass your_password
# 启用 AOF 持久化
appendonly yes
# 内存限制(按需调整)
maxmemory 100mb
# 内存淘汰策略
maxmemory-policy allkeys-lru

# 内存使用阈值(70%),要使该参数生效 maxmemory 至少要设置为 2gb
# vm-memory-high-watermark 0.7
# 客户端闲置超时(秒)
timeout 300
# RDB 持久化策略(900秒内至少1次修改)
save 900 1

启动

docker compose up -d 

查看日志

docker compose logs -f

验证redis服务是否启动成功

docker exec -it redis redis-cli -a your_password
> SET test_key "Hello Docker Redis"
> GET test_key

rabbit-mq

docker-compose.yml

services:
  rabbitmq:
    container_name: rabbitmq
    image: rabbitmq:management
    restart: always
    ports:
      - "5672:5672"     # AMQP 协议端口
      - "15672:15672"   # Web 管理界面端口
    environment:
      RABBITMQ_DEFAULT_USER: admin # 定义用户名
      RABBITMQ_DEFAULT_PASS: admin # 自定义密码
      RABBITMQ_DEFAULT_VHOST: /  # 默认虚拟主机
      RABBITMQ_PLUGINS_DIR: '/plugins:/myplugins'  # 指定多个插件目录
      TZ: Asia/Shanghai
    volumes:
      - ./data:/var/lib/rabbitmq  # 数据持久化目录
      - ./plugins:/myplugins  # 插件挂载目录
      - ./conf:/etc/rabbitmq  # 配置文件目录

新建如下目录

conf
data
plugins
默认的配置目录是空的,如果自己纯粹的从头开始手写MQ的配置,那有点费时也麻烦。

解决方法是:先通过空的配置目录启动,此时程序会自动将配置文件生成到这个目录,但会报错,配置文件生成之后,再删除容器,再重启,此时因为已经有配置文件了,所以就能正常启动了

启动

docker compose up -d 

查看日志

docker compose logs -f

mq启动之后,他的web管理服务并未启动,需要通过如下命令启动:

docker exec -it rabbitmq rabbitmq-plugins enable rabbitmq_management

其中:

  • docker exec -it: 是在外部执行,容器内部命令
  • rabbitmq: 是容器名
  • rabbitmq-plugins enable rabbitmq_management: 是容器中的内部命令

打开浏览器访问:http://localhost:15672/

image.png

xxl-job

services:
  xxl-job-admin:
    image: xuxueli/xxl-job-admin:2.5.0
    container_name: xxl-job-admin
    restart: always
    ports:
      - "8080:8080" # Web 管理界面端口
    environment:
      PARAMS: "--spring.datasource.url=jdbc:mysql://mysql:3306/xxl_job?useUnicode=true&characterEncoding=UTF-8&autoReconnect=true&serverTimezone=Asia/Shanghai --spring.datasource.username=root --spring.datasource.password=123456"
    volumes:
      - ./logs:/data/applogs # 日志目录
      - ./data:/data/appdata # 数据目录
    depends_on:
      mysql:
        condition: service_healthy

  mysql:
    image: mysql:lts
    container_name: xxl-job-mysql
    restart: always
    ports:
      - "3306:3306"
    environment:
      MYSQL_ROOT_PASSWORD: 123456
      MYSQL_DATABASE: xxl_job
      MYSQL_USER: xxl_job
      MYSQL_PASSWORD: xxl_job
    volumes:
      - ./mysql-init/init.sql:/docker-entrypoint-initdb.d/init.sql # 挂载SQL脚本
      - ./mysql/data:/var/lib/mysql # 数据持久化
    healthcheck:
      test: [ "CMD", "mysqladmin", "ping", "-h", "localhost" ]
      interval: 5s
      timeout: 3s
      retries: 3

docker-compose 中 depends_on 的作用_docker compose depends on-CSDN博客

depends_on: 
    db: 
        condition: service_healthy

含义:
xxl-job-admin的启动,依赖于db,且必须是db的健康检查通过之后,才启动


healthcheck:
      test: [ "CMD", "mysqladmin", "ping", "-h", "localhost" ]
      interval: 5s
      timeout: 3s
      retries: 3
      
含义:
test: [ "CMD", "mysqladmin", "ping", "-h", "localhost" ]: 健康检查执行的命令
interval: 5s: 每次执行命令的间隔时间
timeout: 3s: 命令执行的超时时间
retries: 3: 命令执行的重试次数

mysql-init/init.sql

#
# XXL-JOB
# Copyright (c) 2015-present, xuxueli.

CREATE database if NOT EXISTS `xxl_job` default character set utf8mb4 collate utf8mb4_unicode_ci;
use `xxl_job`;

SET NAMES utf8mb4;

CREATE TABLE `xxl_job_info`
(
    `id`                        int(11)      NOT NULL AUTO_INCREMENT,
    `job_group`                 int(11)      NOT NULL COMMENT '执行器主键ID',
    `job_desc`                  varchar(255) NOT NULL,
    `add_time`                  datetime              DEFAULT NULL,
    `update_time`               datetime              DEFAULT NULL,
    `author`                    varchar(64)           DEFAULT NULL COMMENT '作者',
    `alarm_email`               varchar(255)          DEFAULT NULL COMMENT '报警邮件',
    `schedule_type`             varchar(50)  NOT NULL DEFAULT 'NONE' COMMENT '调度类型',
    `schedule_conf`             varchar(128)          DEFAULT NULL COMMENT '调度配置,值含义取决于调度类型',
    `misfire_strategy`          varchar(50)  NOT NULL DEFAULT 'DO_NOTHING' COMMENT '调度过期策略',
    `executor_route_strategy`   varchar(50)           DEFAULT NULL COMMENT '执行器路由策略',
    `executor_handler`          varchar(255)          DEFAULT NULL COMMENT '执行器任务handler',
    `executor_param`            varchar(512)          DEFAULT NULL COMMENT '执行器任务参数',
    `executor_block_strategy`   varchar(50)           DEFAULT NULL COMMENT '阻塞处理策略',
    `executor_timeout`          int(11)      NOT NULL DEFAULT '0' COMMENT '任务执行超时时间,单位秒',
    `executor_fail_retry_count` int(11)      NOT NULL DEFAULT '0' COMMENT '失败重试次数',
    `glue_type`                 varchar(50)  NOT NULL COMMENT 'GLUE类型',
    `glue_source`               mediumtext COMMENT 'GLUE源代码',
    `glue_remark`               varchar(128)          DEFAULT NULL COMMENT 'GLUE备注',
    `glue_updatetime`           datetime              DEFAULT NULL COMMENT 'GLUE更新时间',
    `child_jobid`               varchar(255)          DEFAULT NULL COMMENT '子任务ID,多个逗号分隔',
    `trigger_status`            tinyint(4)   NOT NULL DEFAULT '0' COMMENT '调度状态:0-停止,1-运行',
    `trigger_last_time`         bigint(13)   NOT NULL DEFAULT '0' COMMENT '上次调度时间',
    `trigger_next_time`         bigint(13)   NOT NULL DEFAULT '0' COMMENT '下次调度时间',
    PRIMARY KEY (`id`)
) ENGINE = InnoDB
  DEFAULT CHARSET = utf8mb4;

CREATE TABLE `xxl_job_log`
(
    `id`                        bigint(20) NOT NULL AUTO_INCREMENT,
    `job_group`                 int(11)    NOT NULL COMMENT '执行器主键ID',
    `job_id`                    int(11)    NOT NULL COMMENT '任务,主键ID',
    `executor_address`          varchar(255)        DEFAULT NULL COMMENT '执行器地址,本次执行的地址',
    `executor_handler`          varchar(255)        DEFAULT NULL COMMENT '执行器任务handler',
    `executor_param`            varchar(512)        DEFAULT NULL COMMENT '执行器任务参数',
    `executor_sharding_param`   varchar(20)         DEFAULT NULL COMMENT '执行器任务分片参数,格式如 1/2',
    `executor_fail_retry_count` int(11)    NOT NULL DEFAULT '0' COMMENT '失败重试次数',
    `trigger_time`              datetime            DEFAULT NULL COMMENT '调度-时间',
    `trigger_code`              int(11)    NOT NULL COMMENT '调度-结果',
    `trigger_msg`               text COMMENT '调度-日志',
    `handle_time`               datetime            DEFAULT NULL COMMENT '执行-时间',
    `handle_code`               int(11)    NOT NULL COMMENT '执行-状态',
    `handle_msg`                text COMMENT '执行-日志',
    `alarm_status`              tinyint(4) NOT NULL DEFAULT '0' COMMENT '告警状态:0-默认、1-无需告警、2-告警成功、3-告警失败',
    PRIMARY KEY (`id`),
    KEY `I_trigger_time` (`trigger_time`),
    KEY `I_handle_code` (`handle_code`),
    KEY `I_jobid_jobgroup` (`job_id`,`job_group`),
    KEY `I_job_id` (`job_id`)
) ENGINE = InnoDB
  DEFAULT CHARSET = utf8mb4;

CREATE TABLE `xxl_job_log_report`
(
    `id`            int(11) NOT NULL AUTO_INCREMENT,
    `trigger_day`   datetime         DEFAULT NULL COMMENT '调度-时间',
    `running_count` int(11) NOT NULL DEFAULT '0' COMMENT '运行中-日志数量',
    `suc_count`     int(11) NOT NULL DEFAULT '0' COMMENT '执行成功-日志数量',
    `fail_count`    int(11) NOT NULL DEFAULT '0' COMMENT '执行失败-日志数量',
    `update_time`   datetime         DEFAULT NULL,
    PRIMARY KEY (`id`),
    UNIQUE KEY `i_trigger_day` (`trigger_day`) USING BTREE
) ENGINE = InnoDB
  DEFAULT CHARSET = utf8mb4;

CREATE TABLE `xxl_job_logglue`
(
    `id`          int(11)      NOT NULL AUTO_INCREMENT,
    `job_id`      int(11)      NOT NULL COMMENT '任务,主键ID',
    `glue_type`   varchar(50) DEFAULT NULL COMMENT 'GLUE类型',
    `glue_source` mediumtext COMMENT 'GLUE源代码',
    `glue_remark` varchar(128) NOT NULL COMMENT 'GLUE备注',
    `add_time`    datetime    DEFAULT NULL,
    `update_time` datetime    DEFAULT NULL,
    PRIMARY KEY (`id`)
) ENGINE = InnoDB
  DEFAULT CHARSET = utf8mb4;

CREATE TABLE `xxl_job_registry`
(
    `id`             int(11)      NOT NULL AUTO_INCREMENT,
    `registry_group` varchar(50)  NOT NULL,
    `registry_key`   varchar(255) NOT NULL,
    `registry_value` varchar(255) NOT NULL,
    `update_time`    datetime DEFAULT NULL,
    PRIMARY KEY (`id`),
    UNIQUE KEY `i_g_k_v` (`registry_group`, `registry_key`, `registry_value`) USING BTREE
) ENGINE = InnoDB
  DEFAULT CHARSET = utf8mb4;

CREATE TABLE `xxl_job_group`
(
    `id`           int(11)     NOT NULL AUTO_INCREMENT,
    `app_name`     varchar(64) NOT NULL COMMENT '执行器AppName',
    `title`        varchar(12) NOT NULL COMMENT '执行器名称',
    `address_type` tinyint(4)  NOT NULL DEFAULT '0' COMMENT '执行器地址类型:0=自动注册、1=手动录入',
    `address_list` text COMMENT '执行器地址列表,多地址逗号分隔',
    `update_time`  datetime             DEFAULT NULL,
    PRIMARY KEY (`id`)
) ENGINE = InnoDB
  DEFAULT CHARSET = utf8mb4;

CREATE TABLE `xxl_job_user`
(
    `id`         int(11)     NOT NULL AUTO_INCREMENT,
    `username`   varchar(50) NOT NULL COMMENT '账号',
    `password`   varchar(50) NOT NULL COMMENT '密码',
    `role`       tinyint(4)  NOT NULL COMMENT '角色:0-普通用户、1-管理员',
    `permission` varchar(255) DEFAULT NULL COMMENT '权限:执行器ID列表,多个逗号分割',
    PRIMARY KEY (`id`),
    UNIQUE KEY `i_username` (`username`) USING BTREE
) ENGINE = InnoDB
  DEFAULT CHARSET = utf8mb4;

CREATE TABLE `xxl_job_lock`
(
    `lock_name` varchar(50) NOT NULL COMMENT '锁名称',
    PRIMARY KEY (`lock_name`)
) ENGINE = InnoDB
  DEFAULT CHARSET = utf8mb4;


## —————————————————————— init data ——————————————————

INSERT INTO `xxl_job_group`(`id`, `app_name`, `title`, `address_type`, `address_list`, `update_time`)
VALUES (1, 'xxl-job-executor-sample', '示例执行器', 0, NULL, '2018-11-03 22:21:31');

INSERT INTO `xxl_job_info`(`id`, `job_group`, `job_desc`, `add_time`, `update_time`, `author`, `alarm_email`,
                           `schedule_type`, `schedule_conf`, `misfire_strategy`, `executor_route_strategy`,
                           `executor_handler`, `executor_param`, `executor_block_strategy`, `executor_timeout`,
                           `executor_fail_retry_count`, `glue_type`, `glue_source`, `glue_remark`, `glue_updatetime`,
                           `child_jobid`)
VALUES (1, 1, '测试任务1', '2018-11-03 22:21:31', '2018-11-03 22:21:31', 'XXL', '', 'CRON', '0 0 0 * * ? *',
        'DO_NOTHING', 'FIRST', 'demoJobHandler', '', 'SERIAL_EXECUTION', 0, 0, 'BEAN', '', 'GLUE代码初始化',
        '2018-11-03 22:21:31', '');

INSERT INTO `xxl_job_user`(`id`, `username`, `password`, `role`, `permission`)
VALUES (1, 'admin', 'e10adc3949ba59abbe56e057f20f883e', 1, NULL);

INSERT INTO `xxl_job_lock` (`lock_name`)
VALUES ('schedule_lock');

commit;

启动

docker compose up -d 

查看日志

docker compose logs -f

服务启动成功之后,打开浏览器访问: http://localhost:8080/xxl-job-admin/

admin / 123456 初始账户密码

image.png

nginx

docker-compose.yml

services:
  nginx:
    image: nginx:latest
    container_name: nginx-doc-site
    ports:
      - "9000:9000"
      - "9001:9001"
      - "9002:9002"
      - "9003:9003"
      - "9004:9004"
      - "9005:9005"
    volumes:
      # 挂载自定义配置文件(覆盖默认配置)
      - ./nginx.conf:/etc/nginx/nginx.conf
      # 挂载静态站点目录
      - ./site:/usr/share/nginx/html
    restart: unless-stopped

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 9000;
        server_name localhost;

        location / {
            root /usr/share/nginx/html;
            index index.html;
        }
    }

    server {
        listen 9001;
        server_name localhost;

        location / {
            root /usr/share/nginx/html/echarts-offline-doc-cli-gh-pages;
            index index.html;
        }
    }

    server {
        listen 9002; server_name local server_name localhost;
        location / {
            root /usr/share/nginx/html/element-plus;
            index index.html;
        }
    }

    server {
        listen 9003;
        server_name localhost;

        location / {
            root /usr/share/nginx/html/ts/docs;
            index index.html;
        }
    }

    server {
        listen 9004;
        server_name localhost;
        location / {
            root /usr/share/nginx/html/vue3;
            index index.html;
        }
    }

    server {
        listen 9005;
        server_name localhost;
        location / {
            root /usr/share/nginx/html/ant-designv-vue3.x;
            index index.html;
            try_files $uri $uri/ /index.html;
        }
    }
}

docker network

docker有4种网络模式: bridge模式, host模式, container模式, nome模式

bridge模式是默认模式(即: 你不指定网络模式的情况下, docker容器都是处于bridge网络模式)

作用:

  • 可以划分不同的网络
  • 处于同一网络名下的服务, 可以通过容器名进行连接, 从而避免ip变化导致的链接问题

查看当前所有的网络: docker network ls

创建一个名为local-docker-network的网络, 且指定网络模式为bridge模式

docker network create --driver bridge local-docker-network

创建容器,并加入local-docker-network网络

docker run --name service1 --network local-docker-network -d my_image

docker compose创建容器, 并加入local-docker-network网络, 且这个网络是外部已经创建好的网络

services:
  app:
    image: node:18
    networks:
      - local-docker-network
      - backend
  redis:
    image: redis
    networks:
      - backend
          
networks:
  local-docker-network:
    external: true # 声明网络为外部已存在

app容器, 加入了两个网络: local-docker-network, backend

redis容器, 仅加入了backend网络

docker compose创建容器, 并加入local-docker-network网络, 且这个网络随着容器一起创建

services:
  app:
    image: node:18
    networks:
      - local-docker-network
      - backend
  redis:
    image: redis
    networks:
      - backend
     
# 关键:在此处定义网络
networks:
  local-docker-network:
    driver: bridge  # 可选驱动类型(bridge/overlay)

wsl突然无法上网了(前提: win和wsl未作任何更改)

wsl --shutdown
# 用于应用程序在网络上进行通信。它允许应用程序通过网络协议(如 TCP/IP)进行数据传输。当 Winsock 配置出现问题时,可能会导致网络连接不稳定或无法连接到互联网。这时,可以使用 netsh winsock reset 命令来重置 Winsock 配置,以解决这些问题
netsh winsock reset

# 用于重置 Windows 操作系统中的网络设置和配置的命令
netsh int ip reset all

# 重置http代理
netsh winhttp reset proxy

# 清除dns缓存
ipconfig /flushdns

重装win11系统后,重新导入wsl镜像

重新导入

wsl --import-in-place Ubuntu-18.04 G:\wsl\Ubuntu-18.04\ext4.vhdx

当你尝试重新启动你刚刚导入的镜像时,可能会遇到的问题:

无法将磁盘“\\?\G:\wsl2-linux\instance\ubuntu-2404-node18\ext4.vhdx”附加到 WSL2: 拒绝访问。
错误代码: Wsl/Service/CreateInstance/MountVhd/HCS/E_ACCESSDENIED

[已退出进程,代码为 4294967295 (0xffffffff)]
现在可以使用Ctrl+D关闭此终端,或按 Enter 重新启动。

这是个权限问题, 尝试改下文件权限, 如图勾上 User 完全控制:

image.png

卸载镜像

wsl --unregister Ubuntu-18.04

参考资料

windows10(家庭版)和windows11(家庭版)开启WSL2_启动wsl-CSDN博客

Ubuntu24.04换源方法(新版源更换方式,包含Arm64)-CSDN博客

全网最全Win10/11系统下WSL2+Ubuntu20.04的全流程安装指南(两种支持安装至 D 盘方式)_win10 wsl2安装-CSDN博客

windows11 安装WSL2全流程_wsl2安装-CSDN博客

WSL2 搭建 Windows 更好用的前端开发环境 | Arthur Wang

zsh 安装与配置,使用 oh-my-zsh 美化终端 | Leehow的小站

Ubuntu 24.04.1 LTS 安装Docker 丝滑!!!_ubuntu 24.04.1安装docker-CSDN博客

在 Windows 11 中设置 WSL2 Ubuntu 的 networkingMode=mirrored 详细教程-CSDN博客

Windows远程连接Docker服务_docker运行windows镜像 如何远程访问-CSDN博客

Ubuntu 上安装和配置 Nexus Repository Manager_ubuntu安装nexus-CSDN博客

【Nexus】通过Nexus搭建Npm私库_nexus npm-hosted-CSDN博客