Docker学习(四)-Spring Boot on Docker

1.创建spring boot项目

https://start.spring.io/

pom.xml文件新增docker支持

    
        
            
                org.springframework.boot
                spring-boot-maven-plugin
            
            
                com.spotify
                docker-maven-plugin
                1.0.0
                
                    ${docker.image.prefix}/${project.artifactId}
                    src/main/docker
                    
                        
                            /
                            ${project.build.directory}
                            ${project.build.finalName}.jar
                        
                    
                
            
        
    
    
        1.8
        springboot
    

添加swagger支持


            io.springfox
            springfox-swagger2
            2.5.0
        
        
        
            io.springfox
            springfox-swagger-ui
            2.5.0
        

开启swagger,并创建测试控制器

@EnableSwagger2
@SpringBootApplication
public class Application {

    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }

}
@Controller
@RequestMapping(value = "/test")
public class TestController {

    @RequestMapping(value = "/fun", method = RequestMethod.GET)
    @ResponseBody
    public String fun(@RequestParam(name = "str") String str) {
        String hostAddress = "";
        try {
            InetAddress address = InetAddress.getLocalHost();//获取的是本地的IP地址
            hostAddress = address.getHostAddress();
        } catch (Exception ex) {
        }
        return hostAddress + "返回:" + str;
    }
}

2.docker开始部署springBoot项目

进入发布服务器,创建目录,并新建Docker编译支持,Dockerfile文件

# From java image, version : 8
#指定镜像,这里用网易云仓库的java8镜像
FROM hub.c.163.com/library/java:8-jre
# 挂载test-docker目录
VOLUME /tmp
# COPY or ADD to image
COPY example.dockertest-0.0.1-SNAPSHOT.jar app.jar
ENTRYPOINT ["java","-Djava.security.egd=file:/dev/./urandom","-jar","/app.jar"]

使用Docker创建镜像

 docker build -t service-test .

-t代表要构建的镜像的tag

.代表当前目录,也就是Dockerfile所在的目录。

 

Docker学习(四)-Spring Boot on Docker_第1张图片

 

查看镜像列表

运行镜像并测试是否成功

docker run -d -p 8081:8080 service-test

curl http://192.168.50.23:8080/test/fun?str=aaa
curl http://192.168.50.23:8081/test/fun?str=aaa
curl http://192.168.50.23:8082/test/fun?str=aaa

 

 

 将镜像文件上传docker镜像服务器,私有镜像服务器搭建参见 https://www.cnblogs.com/woxpp/p/11871886.html

docker tag service-test 192.168.50.24:5000/service-test:latest
docker push 192.168.50.24:5000/service-test:latest

Docker学习(四)-Spring Boot on Docker_第2张图片

 

拉取进行并启动

Docker学习(四)-Spring Boot on Docker_第3张图片

测试是否可用

 

你可能感兴趣的:(Docker学习(四)-Spring Boot on Docker)