假设springboot项目 myboot-api , 在其根目录下创建文件Dockerfile
内容如下:
FROM java:8
MAINTAINER xxx
RUN ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
RUN echo 'Asia/Shanghai' >/etc/timezone
ENV LANG=zh_CN.UTF-8 \
JAVA_OPTS="-server -Xms512m -Xmx512m" \
SPRING_PROFILES_ACTIVE="dev"
#ARG JAR_FILE
#ADD ${JAR_FILE} app.jar
ADD target/myboot-api.jar app.jar
ENTRYPOINT exec java ${JAVA_OPTS} -Djava.security.egd=file:/dev/./urandom -jar -Dspring.profiles.active=${SPRING_PROFILES_ACTIVE} /app.jar
其中ENV 环境变量
- JAVA_OPTS JVM堆内存起始最大值配置
- SPRING_PROFILES_ACTIVE application.yml环境
Linux 命令行创建镜像 启动容器
echo "===============动态参数配置 begin===============>"
APPLICATION_NAME=xxx-srm-api
echo "image and container name is $APPLICATION_NAME"
# springboot启动的端口号
BootPort=8082
echo "the spring boot ($APPLICATION_NAME) port is $BootPort"
# docker中的springboot启动的端口号
DockerBootPort=8082
echo "===============动态参数配置 end===============>"
echo "build docker image"
# mvn dockerfile:build
docker build -f Dockerfile -t $APPLICATION_NAME:latest .
echo "current docker images:"
docker images | grep $APPLICATION_NAME
echo "start container ===============> "
docker run -d -p $BootPort:$DockerBootPort -e JAVA_OPTS="-server -Xms512m -Xmx512m" -e SPRING_PROFILES_ACTIVE="test" --name $APPLICATION_NAME $APPLICATION_NAME:latest