
文章目录1 Docker及编排工具部署1.1 Docker部署1.2 docker compose部署2 MySQL、Nginx、Redis等镜像安装2.1 MySQL镜像部署2.2 Nginx镜像部署2.3 Redis镜像部署2.4 ElasticSearch相关套件部署2.4.1 ElasticSearch镜像部署2.4.2 Kibana镜像部署2.4.3 IK分词器部署3 learn部署3.1 生产模式部署制作镜像3.2 开发模式部署挂载jar目录1 Docker及编排工具部署1.1 Docker部署安装步骤大致包括安装必要工具、添加Docker源、更新缓存、安装指定版本Docker、启动Docker、安装docker-compose-plugin1. 安装必要工具sudoyuminstall-yyum-utils device-mapper-persistent-data lvm22. 安装docker配置开机自启//添加Docker源阿里云sudoyum-config-manager --add-repo https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo //更新缓存sudoyum makecache fast //列出可用版本 yum list docker-ce--showduplicates|sort-r//安装指定版本推荐稳定版sudoyuminstall-ydocker-ce-20.10.24-3.el7 //启动Dockersudosystemctl startdockersudosystemctl restartdocker//设置开机自启sudosystemctlenabledocker//验证状态sudosystemctl statusdockerdocker--version1.2 docker compose部署//安装docker-compose-pluginsudoyuminstall-ydocker-compose-plugin //验证安装dockercompose version2 MySQL、Nginx、Redis等镜像安装2.1 MySQL镜像部署1. 配置镜像源//配置镜像源vim/etc/docker/daemon.json //文件内容{registry-mirrors:[http://docker.1ms.run,http://docker.mybacc.com,https://docker.m.daocloud.io,https://lispy.org,https://docker-0.unsee.tech,https://docker.xuanyuan.me,https://docker.mirrors.ustc.edu.cn,https://hub-mirror.c.163.com,https://mirror.baidubce.com,https://registry.docker-cn.com]}2. 获取镜像//拉镜像dockerpull mysql:5.7 //查看镜像dockerimages3. 目录结构4. docker-compose.yml配置文件services: mysql: image: mysql:5.7# MySQL 镜像版本可替换为 5.7container_name: mysql# 容器名称自定义restart: always# 开机自启容器异常退出时自动重启ports: -3306:3306# 端口映射主机 3306 - 容器 3306environment: MYSQL_ROOT_PASSWORD:123456# root 密码必须设置MYSQL_INITDB_SKIP_TZINFO:1# 跳过时区初始化避免警告TZ:Asia/Shanghai# 设置容器时区为上海volumes: - ./mysql/data:/var/lib/mysql# 数据持久化挂载提前创建对应宿主机目录- ./mysql/conf/my.cnf:/etc/mysql/conf.d/my.cnf# 自定义配置文件挂载提前创建 my.cnf- ./mysql/sql:/data/mysql# 初始化脚本提前创建对应宿主机目录- ./mysql/logs:/var/log/mysql# 日志目录提前创建对应宿主机目录# 可选解决部分系统的权限问题privileged:truehealthcheck: test:[CMD,mysqladmin,ping,-h,localhost,-u,root,--password123456]interval: 5s timeout: 3s retries:105. my.cnf配置文件[mysqld]character-set-serverutf8mb4 collation-serverutf8mb4_general_ciinit_connectSET NAMES utf8mb4default-time-zone08:00sql_modeSTRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTIONmax_connections300innodb_buffer_pool_size1Ginnodb_log_file_size256Minnodb_flush_log_at_trx_commit2sync_binlog0slow_query_logONslow_query_log_file/var/log/mysql/slow.loglong_query_time2log_error/var/log/mysql/error.log skip-name-resolve symbolic-links0max_allowed_packet128M6. 启动镜像// 进入docker-compose.yml配置文件目录 //启动 停止 查看dockercompose down mysqldockercompose up-dmysqldockerps-adockerps//mysql测试dockerexec-itmysql /bin/bash mysql-hlocalhost-uroot-p2.2 Nginx镜像部署1. 获取镜像dockerpull nginx:1.27.4dockerimages2. 目录结构3. docker-compose.yml配置文件services: nginx: image: nginx:1.27.4 container_name: nginx volumes: - ./nginx/nginx.conf:/etc/nginx/nginx.conf:ro# 主配置- ./nginx/conf.d:/etc/nginx/conf.d:ro# 虚拟主机配置- ./nginx/html:/usr/share/nginx/html:rw# 静态资源- ./nginx/logs:/var/log/nginx# 日志- ./nginx/cert:/etc/nginx/ssl:ro# SSL 证书- /etc/localtime:/etc/localtime:ro# 时区ports: -80:80 -8080:8080 environment: TZ: Asia/Shanghai restart: unless-stopped healthcheck: test:[CMD,nginx,-t]interval: 10s timeout: 3s retries:34. default.conf和nginx.conf配置文件//default.conf server{listen80;server_name localhost;root /usr/share/nginx/html;index index.html;location /{try_files$uri$uri/404;}}//nginx.conf events{}http{include /etc/nginx/mime.types;default_type application/octet-stream;sendfile on;keepalive_timeout65;include /etc/nginx/conf.d/*.conf;}5. 首页index.html文件!DOCTYPE htmlhtmllangzh-CNheadmetacharsetUTF-8metanameviewportcontentwidthdevice-width, initial-scale1.0titleNginx 部署成功/titlestylebody{font-family:Segoe UI, Arial, sans-serif;background: linear-gradient(135deg,#1e3c72, #2a5298);color:#fff;display: flex;justify-content: center;align-items: center;height: 100vh;margin:0;}.card{background: rgba(255,255,255,0.15);padding: 40px;border-radius: 16px;text-align: center;backdrop-filter: blur(10px);box-shadow:08px 32px rgba(0,0,0,0.2);}h1{margin-bottom: 20px;font-size:2.5rem;}p{font-size:1.1rem;opacity:0.9;}.status{margin-top: 20px;padding: 10px 20px;background:#27ae60;border-radius: 20px;display: inline-block;font-weight: bold;}code{background: rgba(0,0,0,0.3);padding: 4px 8px;border-radius: 4px;}/style/headbodydivclasscardh1 Nginx 部署成功/h1p当前时间spanidtime/span/pdivclassstatusSTATUS: RUNNING/div/divscriptfunctionupdateTime(){document.getElementById(time).innerTextnew Date().toLocaleString();}updateTime();setInterval(updateTime,1000);/script/body/html6. 启动镜像dockercompose up-dnginxdockercompose down nginxdockerlogs--tail300-fnginx2.3 Redis镜像部署1. 获取镜像dockerpull redis:7.2dockerimages2. docker-compose.yml配置文件services: redis: image: redis:7.2 container_name: redis restart: always ports: -6379:6379volumes: - ./redis/data:/data - ./redis/redis.conf:/usr/local/etc/redis/redis.conf command: redis-server /usr/local/etc/redis/redis.conf healthcheck: test:[CMD,redis-cli,-p,6379,ping]interval: 5s timeout: 3s retries:103. redis.conf配置文件# 下载官方默认配置wgethttps://raw.githubusercontent.com/redis/redis/7.2/redis.conf# 修改如下配置bind0.0.0.0 protected-mode no appendonlyyes4. 启动镜像# 启动容器dockercompose up-dredisdockercompose down redisdockerlogs--tail300-fredis# 测试dockerexec-itredis redis-cli2.4 ElasticSearch相关套件部署2.4.1 ElasticSearch镜像部署1. 获取镜像dockerpull elasticsearch:7.9.32. 目录结构3. docker-compose.yml配置文件services: elasticsearch: image: elasticsearch:7.9.3 container_name: elasticsearch restart: unless-stopped environment: -node.nameelasticsearch -cluster.namedocker-cluster -discovery.typesingle-node -xpack.security.enabledfalse -xpack.security.http.ssl.enabledfalse -xpack.security.transport.ssl.enabledfalse -ES_JAVA_OPTS-Xms512m -Xmx512mulimits: memlock: soft:-1hard:-1volumes: - /home/docker/elasticsearch/data:/usr/share/elasticsearch/data - /home/docker/elasticsearch/logs:/usr/share/elasticsearch/logs - /home/docker/elasticsearch/plugins:/usr/share/elasticsearch/plugins - /home/docker/elasticsearch/conf/elasticsearch.yml:/usr/share/elasticsearch/config/elasticsearch.yml:ro ports: -9200:9200networks: - elastic healthcheck: test:[CMD-SHELL,curl -sf http://localhost:9200/_cluster/health?wait_for_statusyellowtimeout3s || exit 1]interval: 5s timeout: 3s retries:10kibana: image: kibana:7.9.3 container_name: kibana restart: unless-stopped depends_on: - elasticsearch environment: -ELASTICSEARCH_HOSTShttp://elasticsearch:9200 ports: -5601:5601networks: - elastic healthcheck: test:[CMD-SHELL,curl -sf http://localhost:5601/app/home || exit 1]interval: 5s timeout: 3s retries:10networks: elastic: driver: bridge4. elasticsearch.yml配置文件cluster.name:docker-clusternetwork.host:0.0.0.05. 启动镜像sudochown-R1000:0 conf data logs pluginschmod755conf data logs pluginsdockercompose up-delasticsearchdockercompose down elasticsearch6. curl命令//创建索引curl-XPUTlocalhost:9200/person_info?pretty\-HContent-Type: application/json-d { settings: { number_of_shards: 3, number_of_replicas: 1 }, mappings: { properties: { id_card: { type: keyword }, name: { type: text, fields: { keyword: { type: keyword } } }, gender: { type: keyword }, age: { type: integer }, phone: { type: keyword }, email: { type: keyword }, department: { type: keyword }, position: { type: text }, salary: { type: double }, join_date: { type: date }, address: { type: text }, is_active: { type: boolean } } } }//插入数据 指定IDcurl-XPUTlocalhost:9200/person_info/_doc/1001?pretty\-HContent-Type: application/json-d { id_card: 110101199003071234, name: 张三, gender: 男, age: 34, phone: 13800001234, email: zhangsanexample.com, department: 技术部, position: 高级工程师, salary: 25000, join_date: 2020-06-15, address: 北京市朝阳区, is_active: true }//插入数据 自增IDcurl-XPOSTlocalhost:9200/person_info/_doc?pretty\-HContent-Type: application/json-d { name: 李四, gender: 女, age: 28, department: 人事部 }//按ID查询curl-XGETlocalhost:9200/person_info/_doc/1001?pretty//查询所有curl-s-XGEThttp://localhost:9200/person/_search?pretty\-HContent-Type: application/json\-d{ query: { match_all: {} }, from: 0, size: 10 }2.4.2 Kibana镜像部署1. 获取镜像dockerpull kibana:7.9.32. docker-compose.yml配置文件services: kibana: image: kibana:7.9.3 container_name: kibana restart: unless-stopped depends_on: - elasticsearch environment: -ELASTICSEARCH_HOSTShttp://elasticsearch:9200 ports: -5601:5601networks: - elastic healthcheck: test:[CMD-SHELL,curl -sf http://localhost:5601/app/home || exit 1]interval: 5s timeout: 3s retries:10networks: elastic: driver: bridge3. 启动镜像dockercompose up-dkibanadockercompose down kibana4. kibana页面2.4.3 IK分词器部署1. 获取zip包wgethttps://release.infinilabs.com/analysis-ik/stable/elasticsearch-analysis-ik-7.9.3.zip2. 配置环境启动unzip/home/docker/bak/elasticsearch-analysis-ik-7.9.3.zip /home/docker/elasticsearch/plugins/ik/dockercompose up-delasticsearchdockercompose down elasticsearch3. 验证dockerexecelasticsearch elasticsearch-plugin list3 learn部署3.1 生产模式部署制作镜像1. Dockerfile文件FROM eclipse-temurin:8-jre-alpine MAINTAINER yyh ENVTZAsia/Shanghai WORKDIR /home/learn RUNmkdir-p/home/learn/logs COPY learn-1.1.0.jar /home/learn/learn-1.1.0.jar EXPOSE80882. 制作镜像dockerbuild-tlearn:1.1.0.dockerimages3. docker-compose.yml配置文件services: learn: image: learn:1.1.0 container_name: learn restart: unless-stopped ports: -8088:8088volumes:# 外挂配置文件- /home/docker/learn/application.yml:/home/learn/application.yml:ro# 日志目录- /home/docker/learn/logs:/home/learn/logs environment: - TZ:Asia/Shanghai command:java-Xms256m-Xmx512m-jarlearn-1.1.0.jar--spring.profiles.activeprod healthcheck: test:[CMD,wget,-q,--spider,http://localhost:8088/learn/user/hello]interval: 5s timeout: 3s retries:104. 文件目录5. 启动镜像dockercompose up-dlearndockercompose down learndockerlogs--tail300-flearndockerexec-itlearn /bin/bashdockerexec-itlearnshdockercompose stop learndockercomposermlearn6. 配置nginx转发//learn.conf server{listen8080;server_name localhost;root /usr/share/nginx/html;index index.html;location /{try_files$uri$uri/404;}#learn服务location /learn/{proxy_pass http://192.168.10.102:8088;proxy_http_version1.1;proxy_set_header Host$host;proxy_set_header X-Real-IP$remote_addr;proxy_set_header X-Forwarded-For$proxy_add_x_forwarded_for;proxy_set_header X-Forwarded-Proto$scheme;}}dockerexecnginx nginx-tdockercompose down nginxdockercompose up-dnginx3.2 开发模式部署挂载jar目录1. 配置服务目录2. 配置文件services: learn_test: image: eclipse-temurin:8-jre-alpine container_name: learn_test restart: unless-stopped ports: -8089:8089volumes: - /home/docker/learn_test/learn-1.1.0.jar:/home/learn/learn-1.1.0.jar# 外挂配置文件- /home/docker/learn_test/application.yml:/home/learn/application.yml:ro# 日志目录- /home/docker/learn_test/logs:/home/learn/logs - /etc/localtime:/etc/localtime environment: -TZAsia/Shanghai working_dir: /home/learn command:java-Xms256m-Xmx512m-jarlearn-1.1.0.jar--spring.profiles.activeprod healthcheck: test:[CMD,wget,-q,--spider,http://localhost:8089/learn_test/user/hello]interval: 5s timeout: 3s retries:103. 启动服务目录dockercompose up-dlearn_testdockercompose down learn_testcurl--requestGEThttp://192.168.10.102:8089/learn_test/user/hello4. 配置nginx转发//learn.conf server{listen8080;server_name localhost;root /usr/share/nginx/html;index index.html;location /{try_files$uri$uri/404;}#learn_test服务location /learn_test/{proxy_pass http://192.168.10.102:8089;proxy_http_version1.1;proxy_set_header Host$host;proxy_set_header X-Real-IP$remote_addr;proxy_set_header X-Forwarded-For$proxy_add_x_forwarded_for;proxy_set_header X-Forwarded-Proto$scheme;}}dockerexecnginx nginx-tdockercompose down nginxdockercompose up-dnginx