从零开始用 docker 运行 spring boot 应用
假设已经安装好Docker
Springboot应用
pom添加依赖和构建插件
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.3.RELEASE</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
应用代码
package com.atbug.spring.boot.test;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* Created by addo on 2017/5/15.
*/
@SpringBootApplication
@RestController
public class Application {
@RequestMapping("/")
public String home(){
return "Hello world!";
}
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
应用构建
mvn clean package
Centos 7 with Java8
获取Centos7 镜像
docker pull centos:7
准备centos-java8的dockerfile
FROM centos:7
MAINTAINER Addo Zhang "duwasai@gmail.com"
# Set correct environment variables.
ENV HOME /root
ENV LANG en_US.UTF-8
ENV LC_ALL en_US.UTF-8
RUN yum install -y curl; yum upgrade -y; yum update -y; yum clean all
ENV JDK_VERSION 8u11
ENV JDK_BUILD_VERSION b12
RUN curl -LO "http://download.oracle.com/otn-pub/java/jdk/$JDK_VERSION-$JDK_BUILD_VERSION/jdk-$JDK_VERSION-linux-x64.rpm" -H 'Cookie: oraclelicense=accept-securebackup-cookie' && rpm -i jdk-$JDK_VERSION-linux-x64.rpm; rm -f jdk-$JDK_VERSION-linux-x64.rpm; yum clean all
ENV JAVA_HOME /usr/java/default
RUN yum remove curl; yum clean all
创建centos-java8镜像
docker build -t addo/centos-java8 .
Docker中运行应用
准备应用镜像的dockerfile
FROM addo/centos-java8
ADD target/boot-test-1.0-SNAPSHOT.jar /opt/app.jar
EXPOSE 8080
CMD java -jar /opt/app.jar
构建应用镜像
docker build -t temp/spring-boot-app .
运行
docker run --name spring-boot-app -p 8080:8080 temp/spring-boot-app