项目结构
sentinel
├── compose-sentinel.yaml
├── Dockerfile
├── download.sh
├── logs
├── sentinel.jar
└── start.sh
.env
APP_NAME=sentinel
APP_VERSION=1.8.0
TIME_ZONE="Asia/Shanghai"
JVM_XMS=256m
JVM_XMX=256m
JVM_XMN=128m
SERVER_PORT=8088
AUTH_USERNAME=sentinel
AUTH_PASSWORD=123456
compose-sentinel.yaml
version: '2'
services:
sentinel:
build:
context: .
dockerfile: Dockerfile
container_name: ${APP_NAME}
image: ${APP_NAME}:${APP_VERSION}
restart: on-failure
env_file:
- ./.env
environment:
- SERVER_PORT:${SERVER_PORT}
- AUTH_USERNAME:${AUTH_USERNAME}
- AUTH_PASSWORD:${AUTH_PASSWORD}
volumes:
# echo 'Asia/Shanghai' > /etc/timezone/timezone
- /etc/timezone/timezone:/etc/timezone
- /etc/localtime:/etc/localtime
- ./logs/:/home/sentinel/logs/:rw
ports:
- 8088:8088
Dockerfile
FROM java:8
LABEL maintainer="an23gn@163.com"
# 允许挂载该目录
VOLUME /home/sentinel/logs
ADD sentinel.jar /home/sentinel/sentinel.jar
# set environment
ENV BASE_DIR="/home/sentinel" \
JVM_XMS="512m" \
JVM_XMX="512m" \
JVM_XMN="256m" \
JVM_MS="32m" \
JVM_MMS="20m" \
TIME_ZONE="Asia/Shanghai" \
SERVER_PORT="" \
AUTH_USERNAME="" \
AUTH_PASSWORD=""
WORKDIR $BASE_DIR
EXPOSE ${SERVER_PORT}
CMD java -jar $BASE_DIR/sentinel.jar -server -Xms=$JVM_XMS -Xmx=$JVM_XMX -Xmn=$JVM_XMN -XX:MetaspaceSize=$JVM_MS -XX:MaxMetaspaceSize=$JVM_MMS --Dserver.port=$SERVER_PORT --auth.username=$AUTH_USERNAME --auth.password=$AUTH_PASSWORD
download.sh
#!/usr/bin/env bash
source .env
wget https://github.com/alibaba/Sentinel/releases/download/${APP_VERSION}/sentinel-dashboard-${APP_VERSION}.jar
mv ${APP_NAME}-dashboard-${APP_VERSION}.jar ${APP_NAME}.jar
start.sh
#!/usr/bin/env bash
source .env
# 容器名称
CONTAINER_NAME=${APP_NAME}
# 镜像名
IMAGES_NAME=${CONTAINER_NAME}:${APP_VERSION}
# 部署文件名
COMPOSE_FILE_NAME=compose-${CONTAINER_NAME}.yaml
# 查询已存在的容器
exist_container=`docker inspect --format '{{.State.Running}}' ${CONTAINER_NAME}`
# 判断部署文件是否存在
if [[ ! -e "${COMPOSE_FILE_NAME}" ]];then
echo "${COMPOSE_FILE_NAME} not found."
exit 8
fi
# 如果jar包不存在则下载jar包
if [[ ! -e "${APP_NAME}.jar" ]];then
echo "${APP_NAME}.jar not found, The program is about to download."
chmod +x ./download.sh
. ./download.sh
fi
NONE_IMAGES=$(docker images | grep none | awk '{print $3}')
# 移除空白镜像
if [[ ! -n "${NONE_IMAGES}" ]]; then
echo "没有None镜像需要移除"
else
docker rmi ${NONE_IMAGES}
echo "None镜像移除成功"
fi
# 移除已存在的镜像与容器
function clean() {
# 移除容器
lines=`docker ps -a | grep ${CONTAINER_NAME} | wc -l`
if [[ "$lines" -gt 0 ]]; then
docker ps -a --filter="name=${CONTAINER_NAME}" -a|tail -2|tail -1|awk '{print $1}' | xargs docker rm -f
echo "容器:${CONTAINER_NAME}已移除"
fi
# 移除镜像
lines=`docker images | grep ${CONTAINER_NAME} | grep ${CONTAINER_NAME} | wc -l`
# lines=`docker images | grep 'fg-game' | grep 'fg-game' | wc -l`
if [[ "$lines" -gt 0 ]]; then
docker images -q --filter reference=${IMAGES_NAME} | xargs docker rmi -f
# docker images | grep ${CONTAINER_NAME} | awk '{print $1}' | xargs docker rmi -f
echo "镜像:${IMAGES_NAME}已删除"
fi
}
# 强制重新创建容器
function up(){
echo "正在创建并启动容器"
docker-compose -f ${COMPOSE_FILE_NAME} up --build --force-recreate -d
docker ps -a
docker logs -f ${CONTAINER_NAME}
}
# 停止容器
function down(){
docker-compose -f ${COMPOSE_FILE_NAME} down;
}
# 停止容器
function stop (){
docker-compose -f ${COMPOSE_FILE_NAME} stop;
}
# 启动容器
function start (){
docker-compose -f ${COMPOSE_FILE_NAME} start;
docker ps -a
docker logs -f ${CONTAINER_NAME}
}
for opt in "$@"
do
case "$opt" in
up)
up
;;
down)
down
;;
stop)
stop
;;
start)
start
;;
clean)
clean
;;
restart)
down
clean
up
;;
*)
echo $"Usage: $0 {up|down|start|stop|clean|restart}"
exit 1
esac
done