• Mac使用本地docker,在IDEA进行本地容器化部署、启动


    1、idea安装docker插件:

    2、Idea连接本地docker测试:

     

     如果连接失败,连接失败原因:基于mac的docker for mac本身不支持远程访问,需要依赖一款名叫 socat的网络工具提供的镜像来做远程访问。

    安装socat执行以下步骤:

    2.1、brew install socat  出现以下问题,执行2.2步骤;

    2.2 brew更新步骤:

    1. echo 'export HOMEBREW_BOTTLE_DOMAIN=https://mirrors.ustc.edu.cn/homebrew-bottles' >> ~/.zshrc
    2. source ~/.zshrc
    3. brew update
    4. 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文件:

    1. FROM openjdk:8-jdk-alpine
    2. ADD *.jar app.jar
    3. ENTRYPOINT ["java","-Djava.security.egd=file:/dev/./urandom","-jar","/app.jar"]

     3.2 pom.xml文件需要引入的依赖和插件:

    1. <?xml version="1.0" encoding="UTF-8"?>
    2. <project xmlns="http://maven.apache.org/POM/4.0.0"
    3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    4. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    5. <modelVersion>4.0.0</modelVersion>
    6. <groupId>com.tealala.docker-idea</groupId>
    7. <artifactId>docker-idea-practice</artifactId>
    8. <version>1.0-SNAPSHOT</version>
    9. <parent>
    10. <groupId>org.springframework.boot</groupId>
    11. <artifactId>spring-boot-starter-parent</artifactId>
    12. <version>2.2.7.RELEASE</version>
    13. <relativePath />
    14. </parent>
    15. <properties>
    16. <maven.compiler.source>8</maven.compiler.source>
    17. <maven.compiler.target>8</maven.compiler.target>
    18. <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    19. <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
    20. <docker.image.prefix>com.demo</docker.image.prefix>
    21. <java.version>1.8</java.version>
    22. </properties>
    23. <dependencies>
    24. <dependency>
    25. <groupId>org.springframework.boot</groupId>
    26. <artifactId>spring-boot-starter-web</artifactId>
    27. </dependency>
    28. <dependency>
    29. <groupId>org.springframework.boot</groupId>
    30. <artifactId>spring-boot-starter-test</artifactId>
    31. <scope>test</scope>
    32. </dependency>
    33. <dependency>
    34. <groupId>log4j</groupId>
    35. <artifactId>log4j</artifactId>
    36. <version>1.2.17</version>
    37. </dependency>
    38. </dependencies>
    39. <build>
    40. <plugins>
    41. <plugin>
    42. <groupId>org.springframework.boot</groupId>
    43. <artifactId>spring-boot-maven-plugin</artifactId>
    44. </plugin>
    45. <plugin>
    46. <groupId>com.spotify</groupId>
    47. <artifactId>docker-maven-plugin</artifactId>
    48. <version>1.0.0</version>
    49. <configuration>
    50. <dockerDirectory>src/main/docker</dockerDirectory>
    51. <resources>
    52. <resource>
    53. <targetPath>/</targetPath>
    54. <directory>${project.build.directory}</directory>
    55. <include>${project.build.finalName}.jar</include>
    56. </resource>
    57. </resources>
    58. </configuration>
    59. </plugin>
    60. <plugin>
    61. <artifactId>maven-antrun-plugin</artifactId>
    62. <executions>
    63. <execution>
    64. <phase>package</phase>
    65. <configuration>
    66. <tasks>
    67. <copy todir="src/main/docker" file="target/${project.artifactId}-${project.version}.${project.packaging}"></copy>
    68. </tasks>
    69. </configuration>
    70. <goals>
    71. <goal>run</goal>
    72. </goals>
    73. </execution>
    74. </executions>
    75. </plugin>
    76. </plugins>
    77. </build>
    78. </project>

    3.3 新建application.properties文件:

    server.port=8088

     3.4 新建DockerApplication文件:

    1. package com.tealala;
    2. import org.springframework.boot.SpringApplication;
    3. import org.springframework.boot.autoconfigure.SpringBootApplication;
    4. @SpringBootApplication
    5. public class DockerApplication {
    6. public static void main(String[] args) {
    7. SpringApplication.run(DockerApplication.class, args);
    8. }
    9. }

    3.5新建DockerController文件:

    1. package com.tealala;
    2. import org.apache.juli.logging.Log;
    3. import org.apache.juli.logging.LogFactory;
    4. import org.springframework.web.bind.annotation.RequestMapping;
    5. import org.springframework.web.bind.annotation.RestController;
    6. @RestController
    7. public class DockerController {
    8. static Log LOGGER = LogFactory.getLog(DockerController.class);
    9. @RequestMapping("/docker")
    10. public String index(){
    11. LOGGER.info("hello docker!");
    12. return "Hello Docker!";
    13. }
    14. }

    3.6 docker启动配置:

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

     3.8 生成对应的容器:

     运行成功的容器:

     请求测试:

     

     

  • 相关阅读:
    【postgresql】替换 mysql 中的ifnull()
    基于Springboot的新闻资讯发布系统设计与实现(源码+lw+部署文档+讲解等)
    基于混沌编码实现图像加密与解密附带MATLAB代码
    低代码开发之腾讯云微搭工具
    ubuntu18.04上使用7z分卷压缩和解压
    基于Springboot+mybatis+mysql+html教育培训中心教学系统
    列表进行处理,组成句子
    【IDEA】idea2021.2.1版本无限试用30天重置脚本 实战教程
    自己动手从零写桌面操作系统GrapeOS系列教程——20.汇编语言读硬盘实战
    Android Studio的下载安装与使用
  • 原文地址:https://blog.csdn.net/tealala/article/details/125505686