1、idea安装docker插件:

2、Idea连接本地docker测试:
如果连接失败,连接失败原因:基于mac的docker for mac本身不支持远程访问,需要依赖一款名叫 socat的网络工具提供的镜像来做远程访问。
安装socat执行以下步骤:
2.1、brew install socat 出现以下问题,执行2.2步骤;

2.2 brew更新步骤:
- echo 'export HOMEBREW_BOTTLE_DOMAIN=https://mirrors.ustc.edu.cn/homebrew-bottles' >> ~/.zshrc
- source ~/.zshrc
- brew update
- brew install socat
2.3 启动socat:
socat -d TCP-LISTEN:2375,range=127.0.0.1/32,reuseaddr,fork UNIX:/var/run/docker.sock
后台启动:
nohup socat -d TCP-LISTEN:2375,range=127.0.0.1/32,reuseaddr,fork UNIX:/var/run/docker.sock &
最后返回idea进行docker连接就可以了;
连接成功可以看到docker中已经启动的容器和已经装载的镜像;

3、 新建一个springboot项目进行测试;
项目结构如下:

3.1 新建Dockerfile文件:
- FROM openjdk:8-jdk-alpine
- ADD *.jar app.jar
- ENTRYPOINT ["java","-Djava.security.egd=file:/dev/./urandom","-jar","/app.jar"]
3.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>
-
- <groupId>com.tealala.docker-idea</groupId>
- <artifactId>docker-idea-practice</artifactId>
- <version>1.0-SNAPSHOT</version>
-
- <parent>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-starter-parent</artifactId>
- <version>2.2.7.RELEASE</version>
- <relativePath />
- </parent>
-
- <properties>
- <maven.compiler.source>8</maven.compiler.source>
- <maven.compiler.target>8</maven.compiler.target>
- <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
- <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
- <docker.image.prefix>com.demo</docker.image.prefix>
- <java.version>1.8</java.version>
- </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>
- <dependency>
- <groupId>log4j</groupId>
- <artifactId>log4j</artifactId>
- <version>1.2.17</version>
- </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>1.0.0</version>
- <configuration>
- <dockerDirectory>src/main/docker</dockerDirectory>
- <resources>
- <resource>
- <targetPath>/</targetPath>
- <directory>${project.build.directory}</directory>
- <include>${project.build.finalName}.jar</include>
- </resource>
- </resources>
- </configuration>
- </plugin>
- <plugin>
- <artifactId>maven-antrun-plugin</artifactId>
- <executions>
- <execution>
- <phase>package</phase>
- <configuration>
- <tasks>
- <copy todir="src/main/docker" file="target/${project.artifactId}-${project.version}.${project.packaging}"></copy>
- </tasks>
- </configuration>
- <goals>
- <goal>run</goal>
- </goals>
- </execution>
- </executions>
- </plugin>
-
- </plugins>
- </build>
-
- </project>
3.3 新建application.properties文件:
server.port=8088
3.4 新建DockerApplication文件:
- package com.tealala;
-
- import org.springframework.boot.SpringApplication;
- import org.springframework.boot.autoconfigure.SpringBootApplication;
-
- @SpringBootApplication
- public class DockerApplication {
- public static void main(String[] args) {
- SpringApplication.run(DockerApplication.class, args);
- }
- }
3.5新建DockerController文件:
- package com.tealala;
-
- import org.apache.juli.logging.Log;
- import org.apache.juli.logging.LogFactory;
- import org.springframework.web.bind.annotation.RequestMapping;
- import org.springframework.web.bind.annotation.RestController;
-
- @RestController
- public class DockerController {
- static Log LOGGER = LogFactory.getLog(DockerController.class);
-
- @RequestMapping("/docker")
- public String index(){
- LOGGER.info("hello docker!");
- return "Hello Docker!";
- }
- }
3.6 docker启动配置:

3.7启动docker,生成对应的镜像

3.8 生成对应的容器:

运行成功的容器:

请求测试:
