nacos Dockerfile(不需要特殊处理,使用docker conpose可以不写)
# 基础镜像
FROM nacos/nacos-server
# author
MAINTAINER jianglifeng<jlifeng@foxmail.com>
# 复制conf文件到路径
COPY ./conf/application.properties /home/nacos/conf/application.properties
redis Dockerfile(不需要特殊处理,使用docker conpose可以不写)
# 基础镜像
FROM redis
# author
MAINTAINER jianglifeng<jlifeng@foxmail.com>
#执行命令
RUN mkdir -p /home/nc/redis
# 挂载目录
VOLUME /home/nc/redis
# 指定路径
WORKDIR /home/nc/redis
# 复制conf文件到路径
COPY ./conf/redis.conf /home/nc/redis/redis.conf
nginx Dockerfile (不需要特殊处理,使用docker conpose可以不写)
# 基础镜像
FROM nginx
# author
MAINTAINER jianglifeng<jlifeng@foxmail.com>
#执行脚本
RUN mkdir -p /home/nc/projects/nc-ui
# 挂载目录
VOLUME /home/nc/projects/nc-ui
# 指定路径
WORKDIR /home/nc/projects/nc-ui
# 复制conf文件到路径
COPY ./conf/nginx.conf /etc/nginx/nginx.conf
# 复制html文件到路径
COPY ./html/dist /home/nc/projects/nc-ui
业务服务 Dockerfile(直接换上对应服务的jar)
# 基础镜像
FROM openjdk:8-jre
# author
MAINTAINER jianglifeng<jlifeng@foxmail.com>
#执行脚本
RUN mkdir -p /home/nc/projects/nc-ui
# 挂载目录
VOLUME /home/nc
# 指定路径
WORKDIR /home/nc
# 启动服务,启动环境从环境变量文件.env中获取
ENTRYPOINT java -jar $JAVA_OPTS jar文件名.jar
docker-compose.yml
version : '3.7'
services:
# 生产环境使用单独的nacos
nc-nacos:
container_name: nc-nacos
image: nacos/nacos-server
build:
context: ./nacos
environment:
- MODE=standalone
- TZ=Asia/Shanghai
volumes:
- ./nacos/logs/:/home/nacos/logs
- ./nacos/conf/application.properties:/home/nacos/conf/application.properties
ports:
- "8848:8848"
- "9848:9848"
- "9849:9849"
nc-redis:
container_name: nc-redis
image: redis
build:
context: ./redis
ports:
- "6380:6379"
environment:
- TZ=Asia/Shanghai
volumes:
- ./redis/conf/redis.conf:/home/nc/redis/redis.conf
- ./redis/data:/data
command: redis-server /home/nc/redis/redis.conf
nc-nginx:
container_name: nc-nginx
image: nginx
build:
context: ./nginx
ports:
- "70:80"
environment:
- TZ=Asia/Shanghai
volumes:
- ./nginx/html/dist:/home/nc/projects/nc-ui
- ./nginx/conf/nginx.conf:/etc/nginx/nginx.conf
- ./nginx/logs:/var/log/nginx
depends_on:
- nc-gateway
links:
- nc-gateway
nc-gateway:
container_name: nc-gateway
build:
context: ./nc/gateway
dockerfile: dockerfile
ports:
- "8080:8080"
environment:
- TZ=Asia/Shanghai
- JAVA_OPTS=${JAVA_OPTS}
volumes:
- ./logs/nc-gateway:/home/nc/logs/nc-gateway
- ./nc/gateway/jar/xx-gateway.jar:/home/nc/xx-gateway.jar
depends_on:
- nc-redis
- nc-nacos
links:
- nc-redis
- nc-nacos
nc-auth:
container_name: nc-auth
build:
context: ./nc/auth
dockerfile: dockerfile
ports:
- "9200:9200"
environment:
- TZ=Asia/Shanghai
- JAVA_OPTS=${JAVA_OPTS}
volumes:
- ./logs/nc-auth:/home/nc/logs/nc-auth
- ./nc/auth/jar/xx-auth.jar:/home/nc/xx-auth.jar
depends_on:
- nc-redis
- nc-nacos
links:
- nc-redis
- nc-nacos
nc-modules-system:
container_name: nc-modules-system
build:
context: ./nc/modules/system
dockerfile: dockerfile
ports:
- "9201:9201"
# 部分环境变量从.env文件中获取
environment:
- OSS_ACCESS_KEY_ID=${OSS_ACCESS_KEY_ID}
- OSS_ACCESS_KEY_SECRET=${OSS_ACCESS_KEY_SECRET}
- JAVA_OPTS=${JAVA_OPTS}
- TZ=Asia/Shanghai
volumes:
- ./logs/nc-system:/home/nc/logs/nc-system
- ./nc/xx/system/jar/xx-modules-system.jar:/home/nc/xx-modules-system.jar
depends_on:
- nc-redis
- nc-nacos
links:
- nc-redis
- nc-nacos
nginx配置文件(https自行编写)
worker_processes 1;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
server {
listen 80;
server_name localhost;
location / {
root /home/nc/projects/nc-ui;
try_files $uri $uri/ /index.html;
index index.html index.htm;
}
location /prod-api/{
proxy_set_header Host $http_host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header REMOTE-HOST $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_pass http://ruoyi-gateway:8080/;
}
# 避免actuator暴露
if ($request_uri ~ "/actuator") {
return 403;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
}
脚本
docker环境变量文件
如果定义的环境变量名与宿主机的一样,则优先宿主机上的。文章来源地址https://www.toymoban.com/news/detail-674991.html
# 对象存储AK
OSS_ACCESS_KEY_ID=123
OSS_ACCESS_KEY_SECRET=321
# jvm参数
JAVA_OPTS=-Dspring.profiles.active=prev
部署脚本deploy.sh
#!/bin/sh
# 使用说明,用来提示输入参数
usage() {
echo "Usage: sh 执行脚本.sh [port|base|modules|stop|rm]"
exit 1
}
active=.env.$2
# 启动基础环境(必须)
base(){
docker-compose up -d nc-redis nc-nacos
}
# 启动程序模块(必须),指定启动环境(生产、非生产)
modules(){
docker-compose --env-file $active up -d 容器名称列表
}
# 关闭所有环境/模块
stop(){
docker-compose stop
}
# 删除所有环境/模块
rm(){
docker-compose rm
}
# 删除所有镜像
rmi(){
docker rmi 镜像名称列表
}
# 根据输入参数,选择执行对应方法,不输入则执行使用说明
case "$1" in
"port")
port
;;
"base")
base
;;
"modules")
modules
;;
"stop")
stop
;;
"rm")
rm
;;
*)
usage
;;
esac
批量复制文件到对应目录copy.sh
#!/bin/sh
# 复制项目的文件到对应docker路径,便于一键生成镜像。
usage() {
echo "Usage: sh copy.sh"
exit 1
}
# copy html
echo "begin copy html "
cp -r ../jar-dist/dist/** ./nginx/html/dist
# copy jar
echo "begin copy xx-gateway "
cp ../jar-dist/xx-gateway.jar ./nc/gateway/jar
echo "begin copy xx-auth "
cp ../jar-dist/xx-auth.jar ./nc/auth/jar
echo "begin copy xx-modules-system "
cp ../jar-dist/xx-modules-system.jar ./nc/modules/system/jar
echo "begin copy xx-modules-file "
cp ../jar-dist/xx-modules-file.jar ./nc/modules/file/jar
echo "begin copy xx-modules-marketing "
cp ../jar-dist/xx-modules-marketing.jar ./nc/modules/marketing/jar
目录结构
docker/
│ copy.sh
│ deploy.sh
│ docker-compose.yml
│
├─nacos
│ │ dockerfile
│ │
│ └─conf
│ application.properties
│
├─nc
│ ├─auth
│ │ │ dockerfile
│ │ │
│ │ └─jar
│ │ readme.txt
│ │
│ ├─gateway
│ │ │ dockerfile
│ │ │
│ │ └─jar
│ │ readme.txt
│ │
│ └─modules
│ ├─file
│ │ │ dockerfile
│ │ │
│ │ └─jar
│ │ readme.txt
│ │
│ ├─marketing
│ │ │ dockerfile
│ │ │
│ │ └─jar
│ └─system
│ │ dockerfile
│ │
│ └─jar
│ readme.txt
│
├─nginx
│ │ dockerfile
│ │
│ ├─conf
│ │ nginx.conf
│ │
│ └─html
│ └─dist
│ readme.txt
│
└─redis
│ dockerfile
│
└─conf
redis.conf
日志输出模板logback.xml(放到resources目录下)
<?xml version="1.0" encoding="UTF-8"?>
<configuration scan="true" scanPeriod="60 seconds" debug="false">
<!-- 日志存放路径 -->
<property name="log.path" value="logs/nc-xx" />
<!-- 日志输出格式 -->
<property name="log.pattern" value="%d{HH:mm:ss.SSS} [%thread] %-5level %logger{20} - [%method,%line] - %msg%n" />
<!-- 控制台输出 -->
<appender name="console" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>${log.pattern}</pattern>
</encoder>
</appender>
<!-- 系统日志输出 -->
<appender name="file_info" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>${log.path}/info.log</file>
<!-- 循环政策:基于时间创建日志文件 -->
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<!-- 日志文件名格式 -->
<fileNamePattern>${log.path}/info.%d{yyyy-MM-dd}.log</fileNamePattern>
<!-- 日志最大的历史 60天 -->
<maxHistory>60</maxHistory>
</rollingPolicy>
<encoder>
<pattern>${log.pattern}</pattern>
</encoder>
<filter class="ch.qos.logback.classic.filter.LevelFilter">
<!-- 过滤的级别 -->
<level>INFO</level>
<!-- 匹配时的操作:接收(记录) -->
<onMatch>ACCEPT</onMatch>
<!-- 不匹配时的操作:拒绝(不记录) -->
<onMismatch>DENY</onMismatch>
</filter>
</appender>
<appender name="file_error" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>${log.path}/error.log</file>
<!-- 循环政策:基于时间创建日志文件 -->
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<!-- 日志文件名格式 -->
<fileNamePattern>${log.path}/error.%d{yyyy-MM-dd}.log</fileNamePattern>
<!-- 日志最大的历史 60天 -->
<maxHistory>60</maxHistory>
</rollingPolicy>
<encoder>
<pattern>${log.pattern}</pattern>
</encoder>
<filter class="ch.qos.logback.classic.filter.LevelFilter">
<!-- 过滤的级别 -->
<level>ERROR</level>
<!-- 匹配时的操作:接收(记录) -->
<onMatch>ACCEPT</onMatch>
<!-- 不匹配时的操作:拒绝(不记录) -->
<onMismatch>DENY</onMismatch>
</filter>
</appender>
<!-- 系统模块日志级别控制 -->
<logger name="com.xx" level="info" />
<!-- Spring日志级别控制 -->
<logger name="org.springframework" level="warn" />
<root level="info">
<appender-ref ref="console" />
</root>
<!--系统操作日志-->
<root level="info">
<appender-ref ref="file_info" />
<appender-ref ref="file_error" />
</root>
</configuration>
文章来源:https://www.toymoban.com/news/detail-674991.html
到了这里,关于微服务项目容器编排docker-compose.yml、Dockerfile文件模板、相关配置文件、shell脚本的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!