java maven docker-maven-plugin 结合Dockerfile 构建 docker image 并上传到docker server

开启 docker 远程 api

  • 修改 docker.server 文件,修改如下:
    • vim /lib/systemd/system/docker.service
    • ExecStart=/usr/bin/dockerd 修改为 ExecStart=/usr/bin/dockerd -H tcp://0.0.0.0:2375 -H unix:///var/run/docker.sock

      开启docker远程api.png

  • 重启 docker
    • systemctl daemon-reload
    • systemctl restart docker
  • 验证是否开启
    • netstat -anp | grep 2375 是否显示docker正在监听2375端口
    • curl 127.0.0.1:2375/info 是否返回docker信息
    • telnet (docker centos ip) port 在 win10 下是否能 telnet,如:telnet 192.168.0.110 2375 如果不能,检查检查关闭防火墙 systemctl stop firewalld.service

创建 maven 项目,以项目 docker-eureka-server 为例

  • 通过 https://start.spring.io/ 创建 docker-eureka-server

  • 在项目下创建 src/main/docker,并创建 Dockerfile 文件,内容如下:

    FROM java:8
    VOLUME /tmp
    ADD docker-eureka-server-0.0.1-SNAPSHOT.jar app.jar
    RUN bash -c 'touch /app.jar'
    EXPOSE 8761
    ENTRYPOINT ["java","-Djava.security.egd=file:/dev/./urandom","-jar","/app.jar"]
    
  • pom.xml 配置 docker-maven-plugin 插件构建 docker image 并上传 docker server , 插件配置内容如下:

    <!-- 添加docker-maven-plugin插件 -->
            <plugin>
                <groupId>com.spotify</groupId>
                <artifactId>docker-maven-plugin</artifactId>
                <version>0.4.13</version>
                <configuration>
                     <!-- 配置docker server 位置,否则默认127.0.0.1,报org.apache.http.conn.HttpHostConnectException: Connect to localhost:2375 [localhost/127.0.0.1] failed-->
                     <dockerHost>http://192.168.0.110:2375</dockerHost>
                    <!-- 注意imageName一定要是符合正则[a-z0-9-_.]的,否则构建不会成功 -->
                    <imageName>jteasy/${project.artifactId}:${project.version}</imageName>
                    <!-- Dockerfile 文件位置 -->
                    <dockerDirectory>${project.basedir}/src/main/docker</dockerDirectory>
                    <resources>
                        <resource>
                            <targetPath>/</targetPath>
                            <directory>${project.build.directory}</directory>
                            <include>${project.build.finalName}.jar</include>
                        </resource>
                    </resources> 
                </configuration>
            </plugin>
    
  • application.yml 配置,内容如下:

    server:
    port: 8761
    
    eureka:
      instance:
        hostname: localhost
      client:
        registerWithEureka: false
        fetchRegistry: false
        serviceUrl:
          defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
    
  • DockerEurekaServerApplication.java 文件内容如下:

    @SpringBootApplication
    @EnableEurekaServer
    public class DockerEurekaServerApplication {
    
        public static void main(String[] args) {
            SpringApplication.run(DockerEurekaServerApplication.class, args);
        }
    }
    
  • 以上完成后,可以进行 mvn 构建了 (注意:启动docker server),命令如下:

    • clean package docker:build -DskipTests

      maven Dockerfile 构建.png

  • 构建完毕,到 docker serverdocker images 查看镜像信息

    docker images 镜像.png

  • 运行镜像

    • docker run -d -p 8761:8761 jteasy/docker-eureka-server:0.0.1-SNAPSHOT

      镜像运行.png

注:

  • 这一步可能会出现 iptables failed: iptables --wait -t nat -A DOCKER -p tcp -d 0/0 --dport 8761 -j DNAT --to-destination 172.17.0.2:8761 ! -i docker0: iptables: No chain/target/match by that name 类似的docker 端口映射错误异常信息
    解决方法:

    • pkill docker
    • iptables -t nat -F
    • ifconfig docker0 down
    • brctl delbr docker0 如果没有 brctl 命令,执行 yum -y install bridge-utils.noarch tunctl.x86_64,然后重新执行 brctl
    • systemctl restart docker
  • docker maven 结合只需要关注 Dockerfilepom.xml 插件,其他的和普通项目没差别



作者:逐暗者
链接:https://www.jianshu.com/p/fa997d338068
來源:简书
简书著作权归作者所有,任何形式的转载都请联系作者获得授权并注明出处。

猜你喜欢

转载自blog.csdn.net/qq_32711309/article/details/87877732