先写项目的启动脚本,命名为run.sh

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#!/bin/sh

if [ -z $JAVA_OPTS ];then
JAVA_OPTS="-Xms256m -Xmx512m"
fi

if [ -z $JAR_PATH ];then
JAR_PATH="/opt/server"
fi

if [ -z $EXAM_ENV ];then
EXAM_ENV="prod"
fi

if [ x$LOG != "xfalse" ];then
mkdir -p logs
LOGGING_OPT="--logging.path=./logs"
fi

echo $JAVA_OPTS -Dlogging.path=./logs -DSpring.profiles.active=$EXAM_ENV -jar ${JAR_PATH}/*.jar

java $JAVA_OPTS -Dlogging.path=./logs -DSpring.profiles.active=$EXAM_ENV -jar ${JAR_PATH}/*.jar

dockerfile文件打包镜像

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
FROM openjdk:8

RUN mkdir -p /opt/server/

COPY target/${'项目名称'}-SNAPSHOT.jar /opt/server

COPY run.sh /opt/server/

COPY src/main/resources/static /server

EXPOSE 8888

WORKDIR /opt/server

ENTRYPOINT ["sh", "/opt/server/run.sh"]

docker-compose.yml文件,依赖的镜像为MySQL以及redis

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
version: '2.1'
services:
exam:
container_name: exam_server
image: exam:1.0
ports:
- 8888:8888
environment:
- DB_HOST=mysql
- DB_PORT=3306
- DB_NAME=exam_system
- DB_USER=root
- DB_PASSWORD=123456
- REDIS_HOST=redis
- REDIS_PASSWORD=123456
depends_on:
- mysql
- redis
mysql:
container_name: exam_mysql
image: mysql:5.7
ports:
- 3306:3306
environment:
- MYSQL_ROOT_PASSWORD=123456
privileged: true
volumes:
- /opt/exam/volume_data/mysql/data:/var/lib/mysql
- /opt/exam/volume_data/mysql/conf:/etc/mysql/conf.d
- /opt/exam/init_sql:/docker-entrypoint-initdb.d
redis:
container_name: exam_redis
image: redis:6
command: redis-server /etc/redis/redis.conf --requirepass 123456
privileged: true
ports:
- 6379:6379
volumes:
- /opt/exam/volume_data/redis/data:/data
- /opt/exam/volume_data/redis/redis.conf:/etc/redis/redis.conf

其中需要把初始化数据库脚本放在/opt/server/init_sql目录下面

redis的配置文件放在/opt/server/volume_data/redis下面