牛逼!Docker遇到Intellij IDEA,再次解放了生产力~
链接:https://juejin.im/post/5d026212f265da1b8608828b
一、开发前准备
1. Docker的安装可以参考https://docs.docker.com/install/
2. 配置docker远程连接端口
vi /usr/lib/systemd/system/docker.service
3. 重启docker
systemctl daemon-reload
systemctl start docker
4. 开放端口
firewall-cmd --zone=public --add-port=2375/tcp --permanent
5. Idea安装插件,重启
6. 连接远程docker
(1) 编辑配置
(2) 填远程docker地址
(3) 连接成功,会列出远程docker容器和镜像
二、新建项目
1. 创建springboot项目
(1) 配置pom文件
<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.0modelVersion>
<groupId>docker-demogroupId>
<artifactId>com.demoartifactId>
<version>1.0-SNAPSHOTversion>
<parent>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-parentartifactId>
<version>2.0.2.RELEASEversion>
<relativePath />
parent>
<properties>
<project.build.sourceEncoding>UTF-8project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8project.reporting.outputEncoding>
<docker.image.prefix>com.demodocker.image.prefix>
<java.version>1.8java.version>
properties>
<build>
<plugins>
<plugin>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-maven-pluginartifactId>
plugin>
<plugin>
<groupId>com.spotifygroupId>
<artifactId>docker-maven-pluginartifactId>
<version>1.0.0version>
<configuration>
<dockerDirectory>src/main/dockerdockerDirectory>
<resources>
<resource>
<targetPath>/targetPath>
<directory>${project.build.directory}directory>
<include>${project.build.finalName}.jarinclude>
resource>
resources>
configuration>
plugin>
<plugin>
<artifactId>maven-antrun-pluginartifactId>
<executions>
<execution>
<phase>packagephase>
<configuration>
<tasks>
<copy todir="src/main/docker" file="target/${project.artifactId}-${project.version}.${project.packaging}">copy>
tasks>
configuration>
<goals>
<goal>rungoal>
goals>
execution>
executions>
plugin>
plugins>
build>
<dependencies>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-webartifactId>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-testartifactId>
<scope>testscope>
dependency>
<dependency>
<groupId>log4jgroupId>
<artifactId>log4jartifactId>
<version>1.2.17version>
dependency>
dependencies>
project>
(2) 在src/main目录下创建docker目录,并创建Dockerfile文件
FROM openjdk:8-jdk-alpine
ADD *.jar app.jar
ENTRYPOINT ["java","-Djava.security.egd=file:/dev/./urandom","-jar","/app.jar"]
(3) 在resource目录下创建application.properties文件
logging.config=classpath:logback.xml
logging.path=/home/developer/app/logs/
server.port=8990
(4) 创建DockerApplication文件
public class DockerApplication {
public static void main(String[] args) {
SpringApplication.run(DockerApplication.class, args);
}
}
(5) 创建DockerController文件
public class DockerController {
static Log log = LogFactory.getLog(DockerController.class);
public String index() {
log.info("Hello Docker!");
return "Hello Docker!";
}
}
(6) 增加配置
Bind ports : 绑定宿主机端口到容器内部端口。格式为[宿主机端口]:[容器内部端口]
(7) Maven打包
(8) 运行
(9) 运行成功
(10) 浏览器访问
最近热文
• 一个员工的离职成本,很恐怖! • 如果面试被问“红黑树”,可以这样回答 • 她是北大“一个人的毕业照”主人公,2010级古生物专业独苗,十年后搞起了AI • 刚刚用鸿蒙跑了个“hello world”!跑通后,我特么开始怀疑人生了.... 最近整理了一份大厂算法刷题指南,包括一些刷题技巧,在知乎上已经有上万赞。同时还整理了一份6000页面试笔记。关注下面公众号,在公众号内回复「刷题」,即可免费获取!回复「加群」,可以邀请你加入读者群!
明天见(。・ω・。)ノ♡
评论