Docker学习(四)-Spring Boot on Docker

1.创建spring boot项目

https://start.spring.io/

pom.xml文件新增docker支持

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
            <plugin>
                <groupId>com.spotify</groupId>
                <artifactId>docker-maven-plugin</artifactId>
                <version>1.0.0</version>
                <configuration>
                    <imageName>${docker.image.prefix}/${project.artifactId}</imageName>
                    <dockerDirectory>src/main/docker</dockerDirectory>
                    <resources>
                        <resource>
                            <targetPath>/</targetPath>
                            <directory>${project.build.directory}</directory>
                            <include>${project.build.finalName}.jar</include>
                        </resource>
                    </resources>
                </configuration>
            </plugin>
        </plugins>
    </build>
    <properties>
        <java.version>1.8</java.version>
        <docker.image.prefix>springboot</docker.image.prefix>
    </properties>

添加swagger支持

<dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger2</artifactId>
            <version>2.5.0</version>
        </dependency>
        <!-- swagger-ui -->
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger-ui</artifactId>
            <version>2.5.0</version>
        </dependency>

开启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 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

 

拉取进行并启动

测试是否可用

 

posted @ 2019-11-16 16:11  释迦苦僧  阅读(602)  评论(0编辑  收藏  举报