springboot labeled with maven executable jar, to construct image deployment vessel docker

This article describes how to apply springboot labeled jar package, and deploy it to build for the docker docker in the mirror jar

Application Packaging

With the required spring-boot-maven-plugin package, the following code into the application file pom

 <plugin>
 <groupId>org.springframework.boot</groupId>
 <artifactId>spring-boot-maven-plugin</artifactId>
 <executions>
 <execution>
 <goals>
 <goal>repackage</goal>
 </goals>
 </execution>
 </executions> 
 </plugin>

springboot labeled with maven executable jar, to construct image deployment vessel docker


Packaging commands execute maven fight

mvn -DskipTests=true clean package

After completion of the command in the application directory / target / next fight will break out of the jar package,

springboot labeled with maven executable jar, to construct image deployment vessel docker


This jar contains the application package will hit all depend, directly copying out the jar package, execute the command line java -jar application name, you can run

Construction of Mirror

Base image needs to have java runtime environment, here with java: 8 as the base image

Write Dockerfile

#基础镜像:仓库是java,tag是8
FROM java:8
#将打包好的spring程序拷贝到容器中的指定位置
ADD target/lit-webstarter.jar lit-webstarter
#容器对外暴露8080端口
EXPOSE 8080
#容器启动后需要执行的命令
ENTRYPOINT [ "sh", "-c", "java $JAVA_OPTS -Djava.security.egd=file:/dev/./urandom -jar lit-webstarter.jar"]

将Dockerfile 放到应用录下,

springboot labeled with maven executable jar, to construct image deployment vessel docker


命令行cd到应用目录下, 和Dockerfile同级目录, 执行构建镜像命令

docker build -t lit-web:latest-dev .

其中 lit-web 表示镜像名称, latest-dev 表示镜像到tag, . 表示当前目录

构建完成后, 执行docker images 即可看到刚才构建到镜像

运行

执行命令

docker run --name lit-web -p 8080:8080 -d lit-web:latest-dev

其中 --name lit-web 指定容器别名, lit-web:latest-dev 是刚才构建的镜像

如果应用链接到数据库也是在docker容器中, 在本地开发时可以使用loclahost 访问容器中到数据库, 但是当应用也部署到docker中时, 使用localhost 就不能正常访问另一个容器当数据库了,

解决方法如下:

在application.yml 文件中添加 docker的profile

spring:
 profiles: docker
c3p0:
 jdbcUrl: jdbc:mysql://${mysql-docker:mysql-server}:3306/lit
 driverClass: com.mysql.jdbc.Driver
 user: root
 password: 123456

其中mysql-docker 为环境变量, 在运行容器时指定, 默认为mysql-server作为访问数据库容器的别名

然后在启动jar时指定激活的profile为docker, Dockerfile 修改为:

#基础镜像:仓库是java,tag是8
FROM java:8
#将打包好的spring程序拷贝到容器中的指定位置
ADD target/lit-webstarter.jar lit-webstarter.jar
#容器对外暴露8080端口
EXPOSE 8080
ENV JAVA_OPTS="-Dspring.profiles.active=docker"
#容器启动后需要执行的命令
ENTRYPOINT [ "sh", "-c", "java $JAVA_OPTS -Djava.security.egd=file:/dev/./urandom -jar lit-webstarter.jar"]

在运行时指定环境变量并指定访问数据库容器的别名

docker run --name lit -p 8080:8080 --link mysql:mysql-server -e JAVA_OPTS="-Dspring.profiles.active=docker -Dmysql-docker=mysql-server" -d lit-web:latest-dev

Wherein the alias database is --link mysql container, the container can not know the alias Id,

mysql-server is an alias to use when accessing the database container container application, and the application to the operating parameters mysql-docker consistent

-e JAVA_OPTS = environment variable to specify Dockerfile


Guess you like

Origin blog.51cto.com/14455981/2423143