> 技术文档 > Docker环境下部署常用软件服务的完整流程,例如mysql,redis,elasticsearch,nginx等常用软件服务!以及springboot项目部署_springboot+redis+mysql+elasticsearch+nacos完整项目

Docker环境下部署常用软件服务的完整流程,例如mysql,redis,elasticsearch,nginx等常用软件服务!以及springboot项目部署_springboot+redis+mysql+elasticsearch+nacos完整项目


部署软件安装

1. 安装docker
2. 安装mysql
3. 安装redis
4. 安装elasticsearch
5. 安装nacos
6. 安装seata
7. 安装sentinel
8. 安装minio
9. 安装nginx部署前端项目
10. 安装启动springboot项目

注:目前先安装这些,后续有别的会同步更新

安装docker

简介

Docker CE是免费的Docker产品的新名称,Docker CE包含了完整的Docker平台,非常适合开发人员和运维团队构建容器APP。

注意:原则上部署到docker,如果需要部署到其他,如宝塔,应与其他同事及项目经理说明,同时做好文档说明


Ubuntu(使用 apt-get 进行安装

# step 1: 安装必要的一些系统工具sudo apt-get updatesudo apt-get install ca-certificates curl gnupg# step 2: 信任 Docker 的 GPG 公钥sudo install -m 0755 -d /etc/apt/keyringscurl -fsSL https://mirrors.aliyun.com/docker-ce/linux/ubuntu/gpg | sudo gpg --dearmor -o /etc/apt/keyrings/docker.gpgsudo chmod a+r /etc/apt/keyrings/docker.gpg# Step 3: 写入软件源信息echo \\ \"deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.gpg] https://mirrors.aliyun.com/docker-ce/linux/ubuntu \\ \"$(. /etc/os-release && echo \"$VERSION_CODENAME\")\" stable\" | \\ sudo tee /etc/apt/sources.list.d/docker.list > /dev/null# Step 4: 安装Dockersudo apt-get updatesudo apt-get install docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin# 安装指定版本的Docker-CE:# Step 1: 查找Docker-CE的版本:# apt-cache madison docker-ce# docker-ce | 17.03.1~ce-0~ubuntu-xenial | https://mirrors.aliyun.com/docker-ce/linux/ubuntu xenial/stable amd64 Packages# docker-ce | 17.03.0~ce-0~ubuntu-xenial | https://mirrors.aliyun.com/docker-ce/linux/ubuntu xenial/stable amd64 Packages# Step 2: 安装指定版本的Docker-CE: (VERSION例如上面的17.03.1~ce-0~ubuntu-xenial)# sudo apt-get -y install docker-ce=[VERSION]

CentOS (使用 yum 进行安装)

 # step 1: 安装必要的一些系统工具sudo yum install -y yum-utils# Step 2: 添加软件源信息yum-config-manager --add-repo https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo# Step 3: 安装Dockersudo yum install docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin# Step 4: 开启Docker服务sudo service docker start# 注意:# 官方软件源默认启用了最新的软件,您可以通过编辑软件源的方式获取各个版本的软件包。例如官方并没有将测试版本的软件源置为可用,您可以通过以下方式开启。同理可以开启各种测试版本等。# vim /etc/yum.repos.d/docker-ce.repo# 将[docker-ce-test]下方的enabled=0修改为enabled=1## 安装指定版本的Docker-CE:# Step 1: 查找Docker-CE的版本:# yum list docker-ce.x86_64 --showduplicates | sort -r# Loading mirror speeds from cached hostfile# Loaded plugins: branch, fastestmirror, langpacks# docker-ce.x86_64 17.03.1.ce-1.el7.centos docker-ce-stable# docker-ce.x86_64 17.03.1.ce-1.el7.centos @docker-ce-stable# docker-ce.x86_64 17.03.0.ce-1.el7.centos docker-ce-stable# Available Packages# Step2: 安装指定版本的Docker-CE: (VERSION例如上面的17.03.0.ce.1-1.el7.centos)# sudo yum -y install docker-ce-[VERSION]

安装校验

 root@iZbp12adskpuoxodbkqzjfZ:$ docker version Client: Docker Engine - Community Version:  28.3.0 API version: 1.51 Go version: go1.24.4 Git commit: 38b7060 Built: Tue Jun 24 15:44:12 2025 OS/Arch:  linux/amd64 Context:  defaultServer: Docker Engine - Community Engine: Version: 28.3.0 API version: 1.51 (minimum version 1.24) Go version: go1.24.4 Git commit: 265f709 Built: Tue Jun 24 15:44:12 2025 OS/Arch: linux/amd64 Experimental: false containerd: Version: 1.7.27 GitCommit: 05044ec0a9a75232cad458027ca83437aae3f4da runc: Version: 1.2.5 GitCommit: v1.2.5-0-g59923ef docker-init: Version: 0.19.0 GitCommit: de40ad0

修改镜像

# 由于国外的镜像源非常慢,经常拉取不到镜像或者失败,使用的是国内的轩辕镜像# 需要在/etc/docker/daemon.json修改,没有则创建即可{  \"registry-mirrors\": [\"https://docker.xuanyuan.me\"] }# 修改完需要执行sudo systemctl daemon-reloadsudo systemctl restart docker

docker已安装成功,我们就来安装项目所需要的软件,例如mysql,redis,elasticsearch,minio等。

安装mysql


第一步:先拉取镜像,目前我们需要的mysql最低的要求是8.0以上的,安装以8.4.5为例

docker pull mysql:8.4.5# 如需要最新版本# docker pull mysql:latest

可使用docker images查看拉取的镜像

root@iZbp12adskpuoxodbkqzjfZ:$ docker imagesREPOSITORY TAG IMAGE ID CREATED SIZEmysql 8.4.5 060652337213 2 months ago 777MB

第二步:创建持久化目录,一定要考虑数据持久化的问题。如果没有挂在持久化数据卷,一旦MySQL容器被销毁,则数据将会全部丢失。

# 用于存放mysql配置文件mkdir -p /home/mysql/conf# 用于存放mysql数据mkdir -p /home/mysql/data

第三步:启动MySQL容器

docker run -itd -p 3306:3306 --restart=always --name mysql -v /home/mysql/conf:/etc/mysql/conf.d -v /home/mysql/data:/var/lib/mysql -e MYSQL_ROOT_PASSWORD=tAgONhU2pICMbfT -e TZ=Asia/Shanghai mysql:8.4.5## 解释## docker run -itd 是docker中用于以后台交互模式启动容器的命令组合,其中 -i 保持标准输入打开,-t 分配伪终端,-d 使容器在后台运行。## -p 3306:3306 是将容器的 MySQL 3306(后者) 端口映射到宿主机的 3306(前者) 端口## --restart=always 是docker开机启动,失败也会一直重启,创建容器时没有添加参数,导致的后果是:当 docker 重启时,容器未能自动启动## --name 为容器指定一个名称为 mysql## -v‌ 或 ‌--volume‌ 是挂载主机目录或命名卷到容器。例如将mysql容器中的/etc/mysql/conf.d挂载到宿主主机/home/mysql/conf下。## --env‌ 或 ‌-e‌ 是设置容器内的环境变量。MYSQL_ROOT_PASSWORD=tAgONhU2pICMbfT 是设置mysql中root的密码## mysql:8.4.5 是要启动的容器镜像

第四步:查看mysql是否安装启动成功

docker ps -f name=mysql## 也可以查看全部容器状态 docker ps -a# CONTAINER ID IMAGE COMMAND  CREATED STATUS PORTS NAMES# 6d0db98107e1 mysql:8.4.5 \"docker-entrypoint.s…\" 3 days ago Up 3 days 0.0.0.0:3306->3306/tcp, [::]:3306->3306/tcp, 33060/tcp mysql## 安装启动成功状态显示Up和端口信息## 可进去容器中执行命令,有两种方式## 第一种,直接进入docker exec -it 6d0db98107e1 mysql -uroot -p## 第二种,先进入bash命令docker exec -it 6d0db98107e1 /bin/bash## 再进入mysqlmysql -uroot -p## 6d0db98107e1 为容器id,即为字段CONTAINER ID,可执行docker ps -a查看## 如没有成功可执行命令查看日志docker logs mysql

到这mysql就安装成功,接下来就是mysql操作了,例如开远程访问等操作。

## 开远程操作,尽量不要开root用户远程CREATE USER \'username\'@\'%\' IDENTIFIED BY \'password\';GRANT ALL PRIVILEGES ON database.* TO \'username\'@\'%\';## 解释## username 创建的用户名## password 创建用户名的密码,尽可能复杂些## database 远程访问的数据库## 整体就是,创建一个username的用户名来单独专门访问database数据库

安装Redis


简介

Redis是一种开放源代码(BSD许可)的内存中数据结构存储,用作数据库,缓存和消息代理。Redis提供数据结构,例如字符串,哈希,列表,集合,带范围查询的排序集合,位图,超日志,地理空间索引和流。Redis具有内置的复制[集群],Lua脚本,LRU驱逐,事务和不同级别的磁盘持久性[磁盘],并通过Redis Sentinel和Redis Cluster自动分区提供了高可用性【集群】。

Redis的特点

  • Redis读取的速度是110000次/s,写的速度是81000次/s
  • Redis的所有操作都是原子性的,同时Redis还支持对几个操作全并后的原子性执行。线程安全
  • 支持多种数据结构:string(字符串);list(列表);hash(哈希),set(集合);zset(有序集合)
  • 持久化–磁盘,主从复制(集群)步:redis版本没有要求,拉取最新版本即可

第一步:拉取redis镜像

docker pull redis:latest

第二步:创建持久化目录

# 用于存放redis配置文件mkdir -p /home/redis/conf# 用于存放redis数据mkdir -p /home/redis/data

需要在/home/redis/conf下创建redis.conf文件

touch redis.conf

编辑redis.conf文件,redis.conf文件内容如下,按照实际修改

# 是否允许远程连接,正式不建议开远程# bind 127.0.0.1 -::1# Redis 6.0 及以上版本引入了保护模式,需要远程则为no,否则为yesprotected-mode noport 6379tcp-backlog 511#redis密码 强烈建议设置复杂一些requirepass Mx1TwBazxKl3QbDgtimeout 0tcp-keepalive 300daemonize nosupervised nopidfile /var/run/redis_6379.pidloglevel noticelogfile \"\"databases 30always-show-logo yessave 900 1save 300 10save 60 10000stop-writes-on-bgsave-error yesrdbcompression yesrdbchecksum yesdbfilename dump.rdbdir ./replica-serve-stale-data yesreplica-read-only yesrepl-diskless-sync norepl-disable-tcp-nodelay noreplica-priority 100lazyfree-lazy-eviction nolazyfree-lazy-expire nolazyfree-lazy-server-del noreplica-lazy-flush noappendonly yesappendfilename \"appendonly.aof\"no-appendfsync-on-rewrite noauto-aof-rewrite-percentage 100auto-aof-rewrite-min-size 64mbaof-load-truncated yesaof-use-rdb-preamble yeslua-time-limit 5000slowlog-max-len 128notify-keyspace-events \"\"hash-max-ziplist-entries 512hash-max-ziplist-value 64list-max-ziplist-size -2list-compress-depth 0set-max-intset-entries 512zset-max-ziplist-entries 128zset-max-ziplist-value 64hll-sparse-max-bytes 3000stream-node-max-bytes 4096stream-node-max-entries 100activerehashing yeshz 10dynamic-hz yesaof-rewrite-incremental-fsync yesrdb-save-incremental-fsync yes

第三步:启动redis容器

docker run -itd --name redis --restart=always --log-opt max-size=100m --log-opt max-file=2 -p 6379:6379 -v /home/redis/conf/redis.conf:/etc/redis/redis.conf -v /home/redis/data:/data redis redis-server /etc/redis/redis.conf

命令如上安装mysql有介绍,不做赘述

安装elasticsearch


简介

Elasticsearch是一个基于Lucene的搜索服务器。它提供了一个分布式多用户能力的全文搜索引擎,基于RESTful web接口。Elasticsearch是用Java语言开发的,并作为Apache许可条款下的开放源码发布,是一种流行的企业级搜索引擎。Elasticsearch用于云计算中,能够达到实时搜索,稳定,可靠,快速,安装使用方便。官方客户端在Java、.NET(C#)、PHP、Python、Apache Groovy、Ruby和许多其他语言中都是可用的。根据DB-Engines的排名显示,Elasticsearch是最受欢迎的企业搜索引擎,其次是Apache Solr,也是基于Lucene。

Elasticsearch 教程Elasticsearch是一个实时分布式的开源全文搜索和分析引擎。它用于单页应用程序(SPA)项目。Elasticsearch是一个用Java开发的开放源码,世界上许多大组织都在使用它。它是根据Apache许可证2.0版授权的。

第一步:拉取elasticsearch镜像,由于小弟项目使用的是7.17.15,所以此教程就以此版本为主

docker pull elasticsearch:7.17.15

第二步:创建持久化目录

# config 挂载配置文件# data 数据# plugins 插件,主要放分词器的啦mkdir -p /home/elasticsearch{ config,data,plugins}# 注意注意,data文件需要权限chmod 777 /home/elasticsearch/data

进去config目录,创建文件elasticsearch.yml

cd /home/elasticsearch/configtouch elasticsearch.yml

elasticsearch.yml内容如下

# ======================== Elasticsearch Configuration =========================## NOTE: Elasticsearch comes with reasonable defaults for most settings.# Before you set out to tweak and tune the configuration, make sure you# understand what are you trying to accomplish and the consequences.## The primary way of configuring a node is via this file. This template lists# the most important settings you may want to configure for a production cluster.## Please consult the documentation for further information on configuration options:# https://www.elastic.co/guide/en/elasticsearch/reference/index.html## ---------------------------------- Cluster -----------------------------------## Use a descriptive name for your cluster:##cluster.name: my-application## ------------------------------------ Node ------------------------------------## Use a descriptive name for the node:##node.name: node-1## Add custom attributes to the node:##node.attr.rack: r1## ----------------------------------- Paths ------------------------------------## Path to directory where to store the data (separate multiple locations by comma):##path.data: /path/to/data## Path to log files:##path.logs: /path/to/logs## ----------------------------------- Memory -----------------------------------## Lock the memory on startup:##bootstrap.memory_lock: true## Make sure that the heap size is set to about half the memory available# on the system and that the owner of the process is allowed to use this# limit.## Elasticsearch performs poorly when the system is swapping the memory.## ---------------------------------- Network -----------------------------------## By default Elasticsearch is only accessible on localhost. Set a different# address here to expose this node on the network:##network.host: 192.168.0.1## By default Elasticsearch listens for HTTP traffic on the first free port it# finds starting at 9200. Set a specific HTTP port here:##http.port: 9200## For more information, consult the network module documentation.## --------------------------------- Discovery ----------------------------------## Pass an initial list of hosts to perform discovery when this node is started:# The default list of hosts is [\"127.0.0.1\", \"[::1]\"]##discovery.seed_hosts: [\"host1\", \"host2\"]## Bootstrap the cluster using an initial set of master-eligible nodes:##cluster.initial_master_nodes: [\"node-1\", \"node-2\"]## For more information, consult the discovery and cluster formation module documentation.## ---------------------------------- Various -----------------------------------## Require explicit names when deleting indices:##action.destructive_requires_name: true## ---------------------------------- Security ----------------------------------##  *** WARNING ***## Elasticsearch security features are not enabled by default.# These features are free, but require configuration changes to enable them.# This means that users don’t have to provide credentials and can get full access# to the cluster. Network connections are also not encrypted.## To protect your data, we strongly encourage you to enable the Elasticsearch security features. # Refer to the following documentation for instructions.## https://www.elastic.co/guide/en/elasticsearch/reference/7.16/configuring-stack-security.htmlingest.geoip.downloader.enabled: false

注意:具体要求具体配置即可

第三步:启动elasticsearch容器

docker run -itd --restart=always --name elasticsearch -p 9200:9200 -p 9300:9300 -v /home/elasticsearch/config/elasticsearch.yml:/usr/share/elasticsearch/config/elasticsearch.yml -v /home/elasticsearch/data:/usr/share/elasticsearch/data -v /home/elasticsearch/plugins:/usr/share/elasticsearch/plugins -e \"discovery.type=single-node\" -e \"ES_JAVA_OPTS=-Xms512m -Xmx512m\" elasticsearch:7.17.15

验证是否成功

root@iZbp12adskpuoxodbkqzjfZ:$ curl 127.0.0.1:9200{  \"name\" : \"bde65d9ac2a2\", \"cluster_name\" : \"elasticsearch\", \"cluster_uuid\" : \"criLqflqRzeG_IxDs7byxA\", \"version\" : {  \"number\" : \"7.17.15\", \"build_flavor\" : \"default\", \"build_type\" : \"docker\", \"build_hash\" : \"0b8ecfb4378335f4689c4223d1f1115f16bef3ba\", \"build_date\" : \"2023-11-10T22:03:46.987399016Z\", \"build_snapshot\" : false, \"lucene_version\" : \"8.11.1\", \"minimum_wire_compatibility_version\" : \"6.8.0\", \"minimum_index_compatibility_version\" : \"6.0.0-beta1\" }, \"tagline\" : \"You Know, for Search\"}

安装成功了,那么就要来分词器了
小弟常用四大分词器:

analysis-icu: 利用ICU库提供了针对非英语语言的增强分词功能,特别是对亚洲语言的支持。analysis-ik: 是一款中文的分词插件,支持自定义词库。analysis-pinyin: 处理中文拼音的分词插件,其核心功能是将中文文本转换为拼音或拼音首字母,支持多种过滤参数以定制输出格式。analysis-stconvert: 处理简体和繁体的转换。

安装分词器,进入/home/elasticsearch/plugins,分别新建目录analysis-icu,analysis-ik,analysis-pinyin,analysis-stconvert

cd /home/elasticsearch/pluginsmkdir -p /home/elasticsearch/plugins/{ analysis-icu,analysis-ik,analysis-pinyin,analysis-stconvert}

然后进去对应的目录下载安装对应的分词器,依次执行命令即可

analysis-icu安装

# 进入对应的目录cd /home/elasticsearch/plugins/analysis-icu# 下载分词器wget https://artifacts.elastic.co/downloads/elasticsearch-plugins/analysis-icu/analysis-icu-7.17.15.zip# 解压分词器unzip analysis-icu-7.17.15.zip

analysis-ik安装

# 进入对应的目录cd /home/elasticsearch/plugins/analysis-ik# 下载分词器wget https://release.infinilabs.com/analysis-ik/stable/elasticsearch-