资讯动态

《数据不丢失!本地挂载的 Docker 一键启动PS1脚本》

发布时间:2026/8/4 20:14:02 来源:尧图企业网站定制
在日常开发中我们经常需要本地运行一些中间件或工具比如绘图工具 Excalidraw、缓存 Redis、关系型数据库 MySQL。使用 Docker 可以避免污染宿主机环境实现快速启动和迁移。本文分享三个实用的 PowerShell 一键启动脚本以及对应的配置文件让你在 Windows 上也能轻松管理容器化开发环境。这里写目录标题一、Excalidraw —— 本地化部署的白板神器启动脚本 start-excalidraw.ps1二、Redis —— 高性能键值缓存配置文件 redis.conf启动脚本 start-redis.ps1三、MySQL —— 两种部署方案方案一使用自定义端口3307与环境变量方案二标准 3306 端口 更丰富的配置总结问题四、mongodb7一、Excalidraw —— 本地化部署的白板神器Excalidraw 是一款开源的虚拟白板工具支持手绘风格图形。通过 Docker 运行官方镜像可以享受私有的绘图服务。启动脚本start-excalidraw.ps1# start-excalidraw.ps1# Excalidraw Docker 容器启动脚本$ContainerNameexcalidraw$ImageNameexcalidraw/excalidraw:latest$HostPort 5000$ContainerPort 80$LocalPathD:\develop\Docker Repository\excalidraw$ContainerPath/app/data# 确保本地挂载目录存在if(-not(Test-Path$LocalPath)){New-Item-ItemType Directory-Path$LocalPath-Force|Out-NullWrite-Host已创建目录:$LocalPath}# 检查是否已在运行$RunningContainer dockerps-q-fname$ContainerName2$nullif($RunningContainer){Write-Host容器 $ContainerName 已在运行中。exit0}# 检查是否存在已停止的容器$ExistingContainer dockerps-aq-fname$ContainerName-fstatusexited2$nullif($ExistingContainer){Write-Host启动已存在的容器 $ContainerName ...dockerstart$ContainerName}else{Write-Host创建并启动新容器 $ContainerName ...docker run-d --name$ContainerName-p${HostPort}:${ContainerPort}-v${LocalPath}:${ContainerPath}--restart unless-stopped $ImageName}if($LASTEXITCODE-eq0){Write-HostExcalidraw 已启动访问地址: http://localhost:$HostPort}else{Write-Host启动失败请检查 Docker 是否运行。}使用说明容器内数据会持久化到D:\develop\Docker Repository\excalidraw访问http://localhost:5000即可使用二、Redis —— 高性能键值缓存配置文件redis.conf# 基础配置 bind 0.0.0.0 port 6379 requirepass 12345678 # 持久化配置 dir /data dbfilename dump.rdb appendonly yes appenddirname appendonlydir # 日志配置 logfile /var/log/redis/redis-server.log loglevel notice # 内存与策略 maxmemory 256mb maxmemory-policy allkeys-lru启动脚本start-redis.ps1# 变量区按需修改$CONTAINER_NAMEredis-local# 容器名称$HOST_PORT6379# 主机映射端口$REDIS_VERSIONlatest# Redis 版本$CONF_PATHD:\develop\Docker Repository\redis\conf\redis.conf# 配置文件本地路径$DATA_PATHD:\develop\Docker Repository\redis\data# 数据持久化目录$LOG_PATHD:\develop\Docker Repository\redis\log# 日志目录可选# 部署逻辑智能启动/创建 $existingContainer dockerps-aq-fname$CONTAINER_NAMEif($existingContainer){Write-HostExisting container$CONTAINER_NAMEfound, starting...-ForegroundColor Yellow dockerstart$CONTAINER_NAME$actionstart}else{Write-HostContainer$CONTAINER_NAMEdoes not exist, creating and running...-ForegroundColor Yellow docker run-d --name$CONTAINER_NAME--restart unless-stopped -p${HOST_PORT}:6379-v${CONF_PATH}:/usr/local/etc/redis/redis.conf-v${DATA_PATH}:/data-v${LOG_PATH}:/var/log/redis redis:${REDIS_VERSION} redis-server/usr/local/etc/redis/redis.conf$actioncreate}# 结果检查与停留if($LASTEXITCODE-eq0){Write-Hostn Redis operation succeeded!-ForegroundColor GreenWrite-Host Container name :$CONTAINER_NAME-ForegroundColor CyanWrite-Host Port mapping :$HOST_PORT- 6379-ForegroundColor CyanWrite-Host Config file :$CONF_PATH-ForegroundColor GrayWrite-Host Data volume :$DATA_PATH-ForegroundColor Grayif($action-eqstart){Write-Host Status : Started existing container-ForegroundColor Green}else{Write-Host Status : Created and running new container-ForegroundColor Green}}else{Write-Hostn Redis operation failed! Error code:$LASTEXITCODE-ForegroundColor RedWrite-HostCommon failure reasons:-ForegroundColor RedWrite-Host • Port$HOST_PORTis already in use-ForegroundColor RedWrite-Host • Config file not found or invalid:$CONF_PATH-ForegroundColor RedWrite-Host • Data/Log directory missing or no permission:$DATA_PATHor$LOG_PATH-ForegroundColor RedWrite-Host • Docker is not running or not installed-ForegroundColor RedWrite-Host • Failed to pull image redis:${REDIS_VERSION} (network or version issue)-ForegroundColor RedWrite-Host • Container name conflict (remove old one: docker rm -f$CONTAINER_NAME)-ForegroundColor RedWrite-Host • Syntax error in redis.conf preventing Redis from starting-ForegroundColor Red}# 停留等待用户按键后关闭窗口Write-HostnPress any key to exit...-ForegroundColor Gray$null$Host.UI.RawUI.ReadKey(NoEcho,IncludeKeyDown)提醒记得修改redis.conf中的requirepass为自己的密码。三、MySQL —— 两种部署方案MySQL 容器需要挂载配置文件、数据目录和日志目录。下面提供两种常用方式。方案一使用自定义端口3307与环境变量启动脚本run-mysql-3307.ps1# 变量区按需修改$MYSQL_PORT3307$MYSQL_VERSIONlatest$CONTAINER_NAMEmysql-local$ROOT_PASSWORD12345678$DEFAULT_DBmyapp$BASE_PATHD:\develop\Docker Repository\mysql3307# 一键部署 docker run-d --name$CONTAINER_NAME--restart unless-stopped -p${MYSQL_PORT}:3306-e MYSQL_ROOT_PASSWORD$ROOT_PASSWORD-e MYSQL_DATABASE$DEFAULT_DB-v${BASE_PATH}\conf\my.cnf:/etc/mysql/my.cnf-v${BASE_PATH}\data:/var/lib/mysql-v${BASE_PATH}\log:/var/log/mysql mysql:${MYSQL_VERSION}# 结果检查 if($LASTEXITCODE-eq0){Write-Hostn[SUCCESS] MySQL container is running!-ForegroundColor GreenWrite-Host Container name :$CONTAINER_NAMEWrite-Host Port mapping :$MYSQL_PORT- 3306Write-Host Root password :$ROOT_PASSWORDWrite-Host Default DB :$DEFAULT_DBWrite-Host Data volume :$BASE_PATH}else{Write-Hostn[FAILED] MySQL container deployment failed (exit code:$LASTEXITCODE)-ForegroundColor RedWrite-HostCommon reasons:-ForegroundColor RedWrite-Host - Port$MYSQL_PORTis already in useWrite-Host - Path$BASE_PATHdoes not exist or has no permissionWrite-Host - Docker is not running or not installedWrite-Host - Failed to pull image mysql:${MYSQL_VERSION}Write-Host - Container name$CONTAINER_NAMEalready exists (run docker rm -f$CONTAINER_NAME)Write-Host - Config file my.cnf is missing or invalid}Write-HostnPress any key to exit...$null$Host.UI.RawUI.ReadKey(NoEcho,IncludeKeyDown)配套my.cnf放在conf目录下[mysqld] port3306 # 容器内部端口固定为 3306 character-set-serverutf8mb4 collation-serverutf8mb4_unicode_ci default-time-zone08:00 max_connections200 innodb_buffer_pool_size256M # 日志配置 log-error/var/log/mysql/error.log general_log1 general_log_file/var/log/mysql/general.log slow_query_log1 slow_query_log_file/var/log/mysql/slow.log long_query_time2 [client] default-character-setutf8mb4⚠️注意-p参数应写为${MYSQL_PORT}:3306不要把容器内的 3306 映射成 3307。这里主机端口为 3307连接时使用-P 3307。方案二标准 3306 端口 更丰富的配置启动脚本run-mysql.ps1# run-mysql.ps1$basePathD:/develop/Docker Repository/mysql# 确保目录存在(conf,data,logs)|ForEach-Object{$dirJoin-Path$basePath$_if(!(Test-Path$dir)){New-Item-ItemType Directory-Path$dir-Force|Out-NullWrite-Host已创建:$dir}}# 运行容器使用标准 3306 端口docker run-d --name mysql --restart unless-stopped -p 3306:3306 -v$basePath/conf/my.cnf:/etc/mysql/my.cnf-v$basePath/data:/var/lib/mysql-v$basePath/logs:/var/log/mysql-e MYSQL_ROOT_PASSWORD12345678 -e TZAsia/Shanghai mysql:8.4.4更完整的my.cnf适合生产级调优[client] socket /var/run/mysqld/mysqld.sock default-character-setutf8mb4 [mysqld] # 基础设置 user mysql pid-file /var/run/mysqld/mysqld.pid socket /var/run/mysqld/mysqld.sock basedir /usr datadir /var/lib/mysql tmpdir /tmp lc-messages-dir /usr/share/MySQL skip-external-locking # 字符集统一 utf8mb4 character-set-server utf8mb4 collation-server utf8mb4_unicode_ci character-set-client-handshake FALSE init_connect SET NAMES utf8mb4 # 时区 default-time-zone 08:00 # 日志适配挂载路径 log_error /var/log/mysql/error.log slow_query_log 1 slow_query_log_file /var/log/mysql/mysql-slow.log long_query_time 2 general_log 0 general_log_file /var/log/mysql/mysql-query.log log-bin /var/log/mysql/mysql-bin.log server-id 1 # InnoDB 调优 innodb_buffer_pool_size 1G innodb_log_file_size 256M innodb_file_per_table 1 innodb_flush_method O_DIRECT innodb_flush_log_at_trx_commit 2 # 连接设置 max_connections 200 wait_timeout 28800 interactive_timeout 28800 max_allowed_packet 64M [mysqldump] quick quote-names max_allowed_packet 64M default-character-set utf8mb4两种方案的区别方案一主机端口 3307适合本地已有 MySQL 服务占用 3306的情况。方案二主机端口 3306配置更完整binlog、时区、InnoDB 调优等适合作为主力开发数据库。总结通过上述脚本你可以在 Windows 上快速拉起三个常用的开发工具/中间件无需手动安装也不用担心环境冲突。每个脚本都具备「检查容器是否存在 → 启动或创建」的逻辑支持--restart unless-stopped保证开机自启。使用建议将脚本保存为.ps1文件在 PowerShell 中执行。首次运行前根据实际情况修改路径、密码和端口。若需停止容器使用docker stop 容器名彻底删除用docker rm 容器名。问题使用ps管理模式下net stop winnat net start winnat四、mongodb7# 变量区按需修改$MONGO_PORT27017# 主机映射端口$MONGO_VERSION7.0# MongoDB 版本$CONTAINER_NAMEmongo7-local# 容器名称$ROOT_USERNAMEroot# root 用户名$ROOT_PASSWORD12345678# root 密码$DEFAULT_DBmyapp# 默认创建的数据库$BASE_PATHD:\develop\Docker Repository\mongodb7# 本地挂载根目录# 部署逻辑智能启动/创建 $existingContainer dockerps-aq-fname$CONTAINER_NAMEif($existingContainer){Write-HostExisting container$CONTAINER_NAMEfound, starting...-ForegroundColor Yellow dockerstart$CONTAINER_NAME$actionstart}else{Write-HostContainer$CONTAINER_NAMEdoes not exist, creating and running...-ForegroundColor Yellow docker run-d --name$CONTAINER_NAME--restart unless-stopped -p${MONGO_PORT}:27017-e MONGO_INITDB_ROOT_USERNAME$ROOT_USERNAME-e MONGO_INITDB_ROOT_PASSWORD$ROOT_PASSWORD-e MONGO_INITDB_DATABASE$DEFAULT_DB-v${BASE_PATH}\data:/data/db-v${BASE_PATH}\config:/data/configdb-v${BASE_PATH}\log:/var/log/mongodb mongo:${MONGO_VERSION}$actioncreate}# 结果检查与停留if($LASTEXITCODE-eq0){Write-Hostn MongoDB operation succeeded!-ForegroundColor GreenWrite-Host Container name :$CONTAINER_NAME-ForegroundColor CyanWrite-Host Port mapping :$MONGO_PORT- 27017-ForegroundColor CyanWrite-Host Root username :$ROOT_USERNAME-ForegroundColor YellowWrite-Host Root password :$ROOT_PASSWORD-ForegroundColor YellowWrite-Host Default DB :$DEFAULT_DB-ForegroundColor YellowWrite-Host Data volume : ${BASE_PATH}\data-ForegroundColor Grayif($action-eqstart){Write-Host Status : Started existing container-ForegroundColor Green}else{Write-Host Status : Created and running new container-ForegroundColor Green}}else{Write-Hostn MongoDB operation failed! Error code:$LASTEXITCODE-ForegroundColor RedWrite-HostCommon failure reasons:-ForegroundColor RedWrite-Host • Port$MONGO_PORTis already in use-ForegroundColor RedWrite-Host • Base path does not exist or has no permission:$BASE_PATH-ForegroundColor RedWrite-Host • Docker is not running or not installed-ForegroundColor RedWrite-Host • Failed to pull image mongo:${MONGO_VERSION} (network or version issue)-ForegroundColor RedWrite-Host • Container name conflict (remove old one: docker rm -f$CONTAINER_NAME)-ForegroundColor RedWrite-Host • Insufficient disk space for MongoDB data-ForegroundColor RedWrite-Host • MongoDB version incompatible with your architecture (e.g., ARM on Windows)-ForegroundColor Red}# 停留等待用户按键后关闭窗口Write-HostnPress any key to exit...-ForegroundColor Gray$null$Host.UI.RawUI.ReadKey(NoEcho,IncludeKeyDown)后续更新在本篇有续集如有问题欢迎留言交流。

读完文章,也想定制专属网站?

尧图设计师 24 小时内与您沟通定制方案

免费获取报价