Modify the file information path as follows:
/etc/docker/daemon.json
Add the following content to the configuration file:
"hosts": ["tcp://0.0.0.0:2375", "unix:///var/run/docker.sock"]
Note: If you don’t have this daemon.json, create one yourself in the /etc/docker/ directory
touch daemon.json
tcp is for remote access, and unix is for local access. If local access is not enabled, the following error will occur when using it on the server:
Cannot connect to the Docker daemon at unix:///var/run/docker.sock. Is the docker daemon running ?
After the modification is completed, execute the following commands in sequence:
#重新加载配置文件 systemctl daemon-reload # 重启服务 systemctl restart docker.service # 查看端口是否开启 默认端口2375 netstat -anp|grep 2375##2. Configure port openingExecute the following commands in sequence
添加指定需要开放的端口: firewall-cmd --zone=public --add-port=2375/tcp --permanent 重载入添加的端口: firewall-cmd --reload 查询指定端口是否开启成功: firewall-cmd --query-port=2375/tcpNote: The above configurations are all performed when the firewall is turned on. If the system firewall is not turned on, the above configuration is not required. For example, my server does not have a firewall configured. Run The following information is displayed: Finally, let’s see if our configuration is effective
curl http://127.0.0.1:2375/infoIf the following information appears, the configuration is successful 3. IDEA installs the Docker plug-inIf your idea version is higher, the docker plug-in is built-in If not, don’t worry, we can install it by ourselves
##4.IDEA configure docker
Note: I mentioned above 192.168.1.2 is my own server IP. Just change it to the server IP where your docker is located.
After clicking Apply, a popup will appear in your service. Click to connect, and we will find that our docker configuration Container information
5. SpringBoot integrated Docker configuration
<!--docker-maven-plugin插件打包--> <plugin> <groupId>com.spotify</groupId> <artifactId>docker-maven-plugin</artifactId> <version>1.0.0</version> <configuration> <!--镜像名称--> <imageName>${project.artifactId}</imageName> <!--指定标签--> <imageTags> <imageTag>latest</imageTag> </imageTags> <!--基础镜像jdk1.8--> <baseImage>java</baseImage> <!--制作者提供本人信息--> <maintainer>ninesun@qq.com</maintainer> <!--切换到Root目录--> <workdir>/ROOT</workdir> <cmd>["java", "-version"]</cmd> <entryPoint>["java", "-jar", "${project.build.finalName}.jar"]</entryPoint> <!--指定DockerFile路径--> <!-- <dockerDirectory>${project.basedir}/src/main/docker</dockerDirectory>--> <!--指定远程docker api地址--> <dockerHost>http://192.168.1.2:2375</dockerHost> <!-- 这里是复制 jar 包到 docker 容器指定目录配置 --> <resources> <resource> <targetPath>/ROOT</targetPath> <!--用于指定需要复制的根目录--> <directory>${project.build.directory}</directory> <!--用于指定需要复制的jar文件--> <include>${project.build.finalName}.jar</include> </resource> </resources> </configuration> </plugin>
All my maven configurations are as follows:
4.0.0 org.springframework.boot spring-boot-starter-parent 2.6.6 com.example docker 0.0.1-SNAPSHOT docker docker 11 true true org.springframework.boot spring-boot-starter-web org.springframework.boot spring-boot-starter-test test <!--docker-maven-plugin插件打包--> <plugin> <groupId>com.spotify</groupId> <artifactId>docker-maven-plugin</artifactId> <version>1.0.0</version> <configuration> <!--镜像名称--> <imageName>${project.artifactId}</imageName> <!--指定标签--> <imageTags> <imageTag>latest</imageTag> </imageTags> <!--基础镜像jdk1.8--> <baseImage>java</baseImage> <!--制作者提供本人信息--> <maintainer>ninesun@qq.com</maintainer> <!--切换到Root目录--> <workdir>/ROOT</workdir> <cmd>["java", "-version"]</cmd> <entryPoint>["java", "-jar", "${project.build.finalName}.jar"]</entryPoint> <!--指定DockerFile路径--> <!-- <dockerDirectory>${project.basedir}/src/main/docker</dockerDirectory>--> <!--指定远程docker api地址--> <dockerHost>http://192.168.1.2:2375</dockerHost> <!-- 这里是复制 jar 包到 docker 容器指定目录配置 --> <resources> <resource> <targetPath>/ROOT</targetPath> <!--用于指定需要复制的根目录--> <directory>${project.build.directory}</directory> <!--用于指定需要复制的jar文件--> <include>${project.build.finalName}.jar</include> </resource> </resources> </configuration> </plugin> org.springframework.boot spring-boot-maven-plugin
Note: After we successfully pull the plug-in, we will find that the directory structure becomes as follows:
We need to delete a startup class, otherwise the packaging will fail. I deleted the DockerApplication directly
5.2 build image
Enter our project directory through cmd
Execute
mvn docker:build
The following information appears Indicates successful packaging
Next, return to our idea, you can see that
This is what we packaged Mirror
Execute on the serverdocker images
5.3 Start the mirror
docker run -d --name idea-docker-test -p 8089:8080 docker
Note: The reason why I exposed port 8089 is because of a conflict with 8080. You can change it according to your own situation
Continue Let’s visit the test interface we wrote:
You can see that it has been successfully deployed and can also be accessed successfully
We can see that it can also be viewed in Idea Let’s get to the image we just started successfully and the log output
The above is the detailed content of How does idea quickly package the SpringBoot project into a Docker image and deploy it?. For more information, please follow other related articles on the PHP Chinese website!