springboot 学习之路 28(与docker的整合)

在idea中使用docker 一键部署到docker服务器上

  1. 在docker服务器上开启2375端口

    [root@microservice ~]# vim /usr/lib/systemd/system/docker.service
    在ExecStart=/usr/bin/dockerd-current 后面加上-H tcp://0.0.0.0:2375 -H unix://var/run/docker.sock \
    重新加载配置文件和启动:
    systemctl daemon-reload
    systemctl start docker
    

  1. 在idea中配置docker服务器

  2. 修改pom.xml配置文件

    <?xml version="1.0" encoding="UTF-8"?>
    <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
        <modelVersion>4.0.0</modelVersion>
        <parent>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-parent</artifactId>
            <version>2.1.0.RELEASE</version>
            <relativePath/> <!-- lookup parent from repository -->
        </parent>
        <groupId>com.huhy</groupId>
        <artifactId>springboot_docker</artifactId>
        <version>0.0.1-SNAPSHOT</version>
        <name>springboot_docker</name>
        <description>docker部署springboot项目</description>
    
        <properties>
            <java.version>1.8</java.version>
            <!--properties节点中设置docker镜像的前缀“huhy”-->
            <docker.image.prefix>huhy</docker.image.prefix>    
            <skipTests>true</skipTests>
        </properties>
    
        <dependencies>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-web</artifactId>
            </dependency>
    
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-test</artifactId>
                <scope>test</scope>
            </dependency>
        </dependencies>
    
        <build>
            <finalName>docker_spring_boot</finalName>
            <plugins>
                <plugin>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-maven-plugin</artifactId>
                </plugin>
                <!--加入maven插件“docker-maven-plugin”-->
    
                <plugin>
                    <groupId>com.spotify</groupId>
                    <artifactId>docker-maven-plugin</artifactId>
                    <!--<version>0.2.3</version>-->
                    <version>1.0.0</version>
                    <executions>
                        <execution>
                            <id>build-image</id>
                            <!--用户只需执行mvn package ,就会自动执行mvn docker:build-->
                            <phase>package</phase>
                            <goals>
                                <goal>build</goal>
                            </goals>
                        </execution>
                    </executions>
                    <configuration>
                        <imageName>${docker.image.prefix}/${project.artifactId}</imageName>
                        <dockerDirectory>src/main/docker</dockerDirectory>
                        <!--指定标签-->
                        <imageTags>
                            <imageTag>latest</imageTag>
                        </imageTags>
                        <!--指定远程 docker api地址-->
                        <dockerHost>http://192.168.248.154:2375</dockerHost>
                        <resources>
                            <resource>
                                <targetPath>/</targetPath>
                                <directory>${project.build.directory}</directory>
                                <include>${project.build.finalName}.jar</include>
                            </resource>
                        </resources>
                    </configuration>
                </plugin>
            </plugins>
        </build>
    
    </project>
    
    
    1. src/main/docker 下配置Dockerfile文件

      FROM java:8
      VOLUME /tmp
      ADD docker_spring_boot.jar app.jar
      ENTRYPOINT ["java","-Djava.security.egd=file:/dev/./urandom","-jar","/app.jar"]
      
    2. maven ---> package 执行之后既可生成镜像

    3. 根据镜像生成容器

    4. 测试:

关于docker一键部署很简单,配置方式也有很多,上述是我的配置方式,仅供大家参考,有其他简单的也可以给我留言

posted @ 2020-04-26 13:26  陽66  阅读(282)  评论(0编辑  收藏  举报