1、Dockerfile创建docker镜像
FROM centos:latest
RUN yum -y update \
&& yum -y install vim \
&& yum -y install git \
&& yum -y install java-1.8.0-openjdk-devel.x86_64 \
&& yum install -y maven \
&& mkdir ~/.m2
RUN echo '<?xml version="1.0" encoding="UTF-8"?><settings xmlns="http://maven.apache.org/SETTINGS/1.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0 http://maven.apache.org/xsd/settings-1.0.0.xsd"><mirrors><mirror><id>alimaven</id><mirrorOf>central</mirrorOf><name>aliyun maven</name><url>http://maven.aliyun.com/nexus/content/repositories/central/</url></mirror></mirrors></settings>' > ~/.m2/settings.xml
2、编写程序运行的脚本.sh
#!/bin/bash
IMAGE_NAME=???
SERVER_NAME=???
JAR_PATH=???
profile=$2
port=$3
usage() {
echo "Usage: sh 执行脚本.sh [init|start|stop|restart|status|pull] [profile] [port]"
exit 1
}
init(){
CID=$(docker ps | grep "$SERVER_NAME" | awk '{print $1}')
IID=$(docker images | grep "$IMAGE_NAME" | awk '{print $3}')
if [ -n "$IID" ]; then
echo "Exit $SERVER_NAME image,IID=$IID"
else
echo "NOT exit $SERVER_NAME image,start build image..."
docker build -t $IMAGE_NAME .
echo "$SERVER_NAME image has been builded"
fi
if [ -n "$CID" ]; then
echo "Exit $SERVER_NAME container,CID=$CID. ---Remove container"
docker stop $SERVER_NAME
docker rm $SERVER_NAME
fi
echo "$SERVER_NAME container,start build..."
docker run -e TZ="Asia/Shanghai" -id -m 512M --memory-swap=1G --name $SERVER_NAME -v ??? $IMAGE_NAME
echo "$SERVER_NAME container build end"
}
is_exist(){
pid=`ps -ef|grep $JAR_PATH|grep -v grep|awk '{print $2}' `
if [ -z "${pid}" ]; then
return 1
else
return 0
fi
}
start(){
is_exist
if [ $? -eq "0" ]; then
echo "${SERVER_NAME} is already running. pid=${pid} ."
else
echo --------Starting application --------
nohup java -server -XX:-DisableExplicitGC -XX:+UseParNewGC -XX:+UseConcMarkSweepGC -jar $JAR_PATH --spring.profiles.active=${profile:-dev} --server.port=${port:-8000} > start.log 2>&1 &
echo --------------Started!---------------
fi
}
stop(){
is_exist
if [ $? -eq "0" ]; then
kill -9 $pid
echo -----------Application Stopped------------
else
echo "${JAR_PATH} is not running"
fi
}
status(){
is_exist
if [ $? -eq "0" ]; then
echo "${JAR_PATH} is running. Pid is ${pid}"
else
echo "${JAR_PATH} is NOT running."
fi
}
restart(){
stop
start
}
pull(){
echo "----------git:find status---------"
git status
echo "----------git:pull new coads---------"
git pull origin develop
if [ $? -ne 0 ]; then
exit
fi
echo "----------mvn clean package -Dmaven.test.skip=true---------"
mvn clean package -Dmaven.test.skip=true
if [ $? -ne 0 ]; then
exit
fi
echo "----------Preparing start application ---------"
is_exist
if [ $? -eq "0" ]; then
restart
else
start
fi
}
case "$1" in
"init")
init
;;
"start")
start
;;
"stop")
stop
;;
"status")
status
;;
"restart")
restart
;;
"pull")
pull
;;
*)
usage
;;
esac
3、运行的命令
3.1、构造镜像名字
docker build -t 镜像名字 .
3.2、初始化
sh 运行运行的脚本.sh init
3.3、拉代码运行
docker exec -it 容器名字 bash
sh 运行运行的脚本.sh pull 运行环境(dev...) 运行端口
ps -ef
sh 运行运行的脚本.sh start 运行环境(dev...) 运行端口
sh 运行运行的脚本.sh stop