1. IDEA install Docker plugin
1. Plug-in download and installation
Files
> Settings
> Plugins
, Search for "docker", select Docker integration installation.
2. After restarting IDEA, open Settings to view Docker and create a new connection
Check the option "TCP socket" in "Connect to Docker daemon with:" and enter "tcp: // host ip: 2375" in "Engine API URL", where host ip is the address of the remote Docker server. First open port 2375 on the server. If the "Connection successful" prompt appears below, the connection is successful.
Second, create a springcloud project
Create a springcloud eureka registry project
pom.xml
<dependencies>
<!-- 增加 eureka-server 的依赖 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-netflix-eureka-server</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter</artifactId>
</dependency>
</dependencies>
<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>0.4.3</version>
<!-- 将插件绑定在某个 phase 执行 -->
<executions>
<execution>
<!-- 将插件绑定在 package 这个 phase 上。用户只需执行 mvn package ,就会自动执行 mvn docker:build -->
<phase>package</phase>
<goals>
<goal>build</goal>
</goals>
</execution>
</executions>
<configuration>
<!-- 远程 Docker 服务器的地址 -->
<dockerHost>http://172.24.4.61:2375</dockerHost>
<!-- 镜像名称为最终构建的 jar 包名称,注意镜像名称中不能包含大写字母 -->
<imageName>${project.build.finalName}</imageName>
<!-- 将 jar 包和 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>
</plugins>
</build>
Write Dockerfile and place it under src / docker directory
FROM java:8
VOLUME /tmp
ADD springcloud-center.jar app.jar
RUN sh -c 'touch /app.jar'
ENTRYPOINT [ "sh", "-c", "java $JAVA_OPTS -Djava.security.egd=file:/dev/./urandom -jar /app.jar" ]
Third, deploy the project to Docker
1. Build an image
Run the mvn command to package the project
mvn package
Since the plug-in binding is configured in pom.xml and executed mvn package
, it will be automatically executed mvn docker:build
. If the console outputs "BUILD SUCCESS", the image is successfully built.
2. View the Docker server image
You can view the status of images in the Docker plugin window of IDEA, or execute the view image command on the Docker server
docker images
3. Create and start the container
Switch to the docker-compose directory and execute
docker-compose -f docker-compose-demo.yml up -d
Four, problem handling
1. Connection reset by peer is abnormal
Exception information is returned when executing the mvn command:
com.spotify.docker.client.shaded.javax.ws.rs.ProcessingException: org.apache.http.client.ClientProtocolException: Cannot retry request with a non-repeatable request entity: Connection reset by peer -> [Help 1]
The reason is that the image name imageName should not contain capital letters.
2. ADD failed exception when playing mirror
Exception information is returned when executing the mvn command:
ADD failed: stat /var/lib/docker/tmp/docker-builderXXXXXX: no such file or dir
The reason is that the jar package name defined in the ADD statement of the Dockerfile is inconsistent with the actual jar package name of the mvn package, resulting in the jar package file not being found.
For example, the name of the jar package is springcloud-center-0.0.1-SNAPSHOT.jar, and the ADD statement in the Dockerfile is ADD springcloud-center.jar app.jar
.