Docker學習(四)-Spring Boot on Docker

1.建立spring boot項目html

https://start.spring.io/java

pom.xml文件新增docker支持spring

<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支持docker

<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,並建立測試控制器springboot

@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文件app

# 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建立鏡像dom

docker build -t service-test .

-t表明要構建的鏡像的tagcurl

.表明當前目錄,也就是Dockerfile所在的目錄。maven

 

 

查看鏡像列表

運行鏡像並測試是否成功

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鏡像服務器,私有鏡像服務器搭建參見 http://www.javashuo.com/article/p-elpnlkxi-bb.html

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

 

拉取進行並啓動

測試是否可用

相關文章
相關標籤/搜索