Maven构建Docker镜像并且Push

允许Docker远程访问(可选)
  1. 编辑docker的宿主机文件/lib/systemd/system/docker.service

    $ vi /lib/systemd/system/docker.service
    # ExecStart=/usr/bin/dockerd -H fd:// --containerd=/run/containerd/containerd.sock后面加上-H 0.0.0.0:2375
    ExecStart=/usr/bin/dockerd -H fd:// --containerd=/run/containerd/containerd.sock后面加上-H 0.0.0.0:2375
    $ wq!
    
  2. 重启服务

    $ systemctl daemon-reload
    $ service docker restart
    
  3. 验证

    访问:http://192.168.246.135:2375/info,能出现数据就表示开启成功

IDEA 配置docker-maven-plugin
  1. 创建Spring Boot项目,这一步略

  2. 配置pom.xml

    
    
        4.0.0
        
            org.springframework.boot
            spring-boot-starter-parent
            2.6.1
             
        
        com.pasca
        student-docker
        latest
        Student-Docker
        Student-Docker
        
            1.8
            192.168.246.135:2375
            pasca
        
        
            ...其他项目依赖省略
        
    
        
            
                ...其他插件省略
                
                
                    com.spotify
                    docker-maven-plugin
                    1.2.2
    
                    
                    
                        
                            build-image
                            
                            package
                            
                                build
                            
                        
                    
    
                    
                        
                        pasca/${project.artifactId}
                        
                        
                            ${project.version}
                        
                        
                        ${project.basedir}/src/main/resources
    
                        
                        http://192.168.246.135:2375
    
                        
                        
                            
                                /
                                
                                ${project.build.directory}
                                
                                ${project.build.finalName}.jar
                            
                        
                    
                
            
        
    
    
  3. 编写Dockerfile

    FROM bitnami/java:1.8.312
    
    MAINTAINER Docker Practice Project by Pasca
    
    ADD *.jar app.jar
    
    ENV TZ Asia/Shanghai
    EXPOSE 8080
    ENTRYPOINT ["java","-jar","/app.jar"]
    
  4. 制作镜像mvn package

    经过这一步,本地项目已经在远程服务上面创建了镜像

  5. 编写docker-compose.yaml

    version: '3.8'
    services:
      pasca-app-student:
        image: pasca/student-docker:latest
        container_name: pasca-app-student
        restart: always
        ports:
          - '8080:8080'
    
  6. 启动docker compose up -d

你可能感兴趣的:(Maven构建Docker镜像并且Push)