Package the docker image in the idea environment

1. First edit the docker file on the server where the docker service is located

  1、编辑 vim /usr/lib/systemd/system/docker.service
         ExecStart=/usr/bin/dockerd -H tcp://0.0.0.0:2375 -H unix://var/run/docker.sock
   
  2、保存后 刷新docker 
      systemctl daemon-reload  
      systemctl start docker  
   
  3、测试 
      输入netstat -anp|grep 2375 会显示docker正在监听2375端口
      输入curl 127.0.0.1:2375/info  如果有显示信息,则表明已经生效

2. Add a Dockerfile file to the project root directory and add the following configuration:

<build>
<plugins>
    <!--启动入口、添加maven依赖-->
    <plugin>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-maven-plugin</artifactId>
        <configuration>
            <!-- 指定该Main Class为全局的唯一入口 -->
            <mainClass>com.xuanqing.controller.EurekaApplication</mainClass>
            <layout>ZIP</layout>
        </configuration>
        <executions>
            <execution>
                <goals>
                    <goal>repackage</goal><!--可以把依赖的包都打包到生成的Jar包中-->
                </goals>
            </execution>
        </executions>
    </plugin>
    <!-- 打包到 docker 服务-->
    <plugin>
        <groupId>com.spotify</groupId>
        <artifactId>docker-maven-plugin</artifactId>
        <configuration>
            <imageName>${project.name}:${project.version}</imageName>
            <!--DockerFile目录,当前为根目录-->
            <dockerDirectory>${project.basedir}</dockerDirectory>
            <dockerHost>http://ip:2375</dockerHost>
            <resources>
                <resource>
                    <targetPath>/</targetPath>
                    <directory>${project.build.directory}</directory>
                    <include>${project.build.finalName}.jar</include>
                </resource>
            </resources>
        </configuration>
    </plugin>
</plugins>
</build>

3.Dockerfile file content

FROM openjdk:8-jdk-alpine
MAINTAINER  zhuyu
COPY ["gxxccs-1.1.0.jar" , "/usr/gxxccs.jar"]
ENV PARAMS=""
ENTRYPOINT ["sh" , "-c" , "java $PARAMS  -jar /usr/gxxccs.jar"]

4. Packaging: mvn clean package docker:build -DskipTests

5. View the image and start the container
docker images
docker run -d --name gxxccs–restart=always
-p 8080:8080 -e PARAMS="-DXms=1000m -DXmx=1000m -Dspring.profiles.active=prod -Dserver. port=8080”
gxxccs:1.0.2

6. Export and import images
docker save -o gxxccs-1.1.0.tar gxxccs:1.0.2
docker load -i gxxccs-1.1.0.tar

Reference URL: https://blog.csdn.net/weixin_41387105/article/details/126508375

Guess you like

Origin blog.csdn.net/zhuyu19911016520/article/details/131420160