IDEA集成Docker實(shí)現(xiàn)一鍵部署的詳細(xì)過程
在和前端聯(lián)調(diào)的過程中,然后每次電腦使用IDEA將服務(wù)啟動(dòng)后不能動(dòng),然后自己想改變代碼后重啟可能導(dǎo)致前端那邊報(bào)錯(cuò),所以為了給前端提供聯(lián)調(diào)的服務(wù)的同時(shí),我自己還可以正常工作,于是便想到了使用docker的方式,這樣就可以達(dá)到了兩全其美,何樂而不為。
1.初識(shí)Docker
Docker的三個(gè)基本概念:
- Dockerfile:鏡像構(gòu)建的模板,描述鏡像構(gòu)建的步驟,通常是拉去一些文件和依賴;
- image:鏡像,一個(gè)文件,用來創(chuàng)建容器。
- container:容器,一個(gè)可運(yùn)行的鏡像實(shí)例,里面運(yùn)行著一個(gè)完整的操作系統(tǒng),可以做一切你當(dāng)前操作系統(tǒng)可以做的事情。
從我的理解對(duì)上述三者做一個(gè)類比:dockerfile就是一個(gè)混凝土配比說明書(原材料,步驟等),根據(jù)該說明書攪拌出混凝土(鏡像),然后基于混凝土可以做成一個(gè)一個(gè)房間(容器),每個(gè)房間都是相互獨(dú)立,生活著不同的人。
對(duì)于我們開發(fā)人員來說,Docker 可以做到:
- 編寫本地代碼
- 使用 Docker 將程序推送到測試環(huán)境
- 發(fā)現(xiàn) bug 后在開發(fā)環(huán)境下修復(fù),重新部署到測試環(huán)境測試
- 測試完成將代碼合并到發(fā)布的代碼分支
2.Docker基于Windows集成IDEA
2.1 在window上安裝docker
注意一點(diǎn):一定要把windows的WSL開啟后再安裝,否則會(huì)導(dǎo)致docker啟動(dòng)不成功。
2.2設(shè)置docker配置
- 開放2375端口,勾上該選項(xiàng)

新增host:[ “0.0.0.0:2375”]
{
"debug": false,
"experimental": false,
"features": {
"buildkit": true
},
"hosts": [
"tcp://0.0.0.0:2375"
],
"insecure-registries": [],
"registry-mirrors": [
"https://hub-mirror.c.163.com",
"https://mirror.baidubce.com"
]
}
2.3 IDEA 連接docker 測試
- 老版本IDEA需要安裝docker的插件,新版本的話不用安裝直接使用

連接docker測試

Note:如果是本地的應(yīng)用可以使用tcp://localhost:2375連接;如果是局域網(wǎng)的其他機(jī)器可以使用局域網(wǎng)ipv4連接;如果是遠(yuǎn)程機(jī)器的話使用公網(wǎng)ip連接。
如上圖中出現(xiàn)Connection successful為成功標(biāo)志
// 當(dāng)使用ip訪問時(shí)連接不成功的話在windows的admin權(quán)限終端窗口執(zhí)行如下命令,端口代理 netsh interface portproxy add v4tov4 listenport=2375 connectaddress=127.0.0.1 connectport=2375 listenaddress=<your ipv4> protocol=tcp //對(duì)2375端口添加防火墻規(guī)則 netsh advfirewall firewall add rule name="docker_daemon" dir=in action=allow protocol=TCP localport=2375
說說小編的個(gè)人經(jīng)歷:完成了宿主機(jī)配置后,在局域網(wǎng)內(nèi)的其他機(jī)器都是可以連接docker的,但是第二天早上再次連接就不行了,然后搞了好幾天還是不行,突然一個(gè)偶然的機(jī)會(huì)又能重新連接上了。
//執(zhí)行下述的命令 然后查看2375的端口 netsh interface portproxy show all //刪除所有的端口代理 netsh interface portproxy delete v4tov4 listenaddress=<your ipv4> listenport=2375 //重新執(zhí)行端口代理 netsh interface portproxy add v4tov4 listenport=2375 connectaddress=127.0.0.1 connectport=2375 listenaddress=<your ipv4> protocol=tcp 在瀏覽器中訪問yourip:2375/version測試,如果有數(shù)據(jù)返回那就是連接成功了。
2.4啟動(dòng)Springboot應(yīng)用測試
- 構(gòu)建測試項(xiàng)目
@RestController
public class TestController {
@GetMapping("/get/hello")
public String get(){
return "Hello World";
}
}
@SpringBootApplication
public class SpringBootWithDockerStarter {
public static void main(String[] args) {
SpringApplication.run(SpringBootWithDockerStarter.class, args);
}
}
在項(xiàng)目中添加Dockerfile文件
#這是基礎(chǔ)鏡像 FROM java:8 VOLUME /tmp #復(fù)制jar包到鏡像中,并且將名字改成app.jar ADD ./target/SpringBootWithDocker-1.0-SNAPSHOT.jar DemoApp.jar #在容器啟動(dòng)的時(shí)候運(yùn)行命令,來啟動(dòng)我們的項(xiàng)目(這其實(shí)就是一段Linux命令,該命令可以在服務(wù)啟動(dòng)時(shí)加一些參數(shù)) ENTRYPOINT ["sh", "-c", "java -jar DemoApp.jar"]
上述注意一點(diǎn):該文件的放置位置會(huì)影響ADD后面的尋找jar包的路徑,因?yàn)槲液竺嬖赽uild鏡像時(shí)出現(xiàn)找不到j(luò)ar的報(bào)錯(cuò),原因就是我將該Dockerfile放在了該項(xiàng)目的某一個(gè)文件夾下了。
項(xiàng)目結(jié)構(gòu)如下:

添加maven的docker打包插件
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<plugin><!--制作docker鏡像的maven插件-->
<groupId>com.spotify</groupId>
<artifactId>docker-maven-plugin</artifactId>
<version>1.2.2</version>
<executions>
<execution>
<id>build-image</id>
<phase>package</phase>
<goals>
<goal>build</goal>
</goals>
</execution>
</executions>
<configuration>
<imageName>${project.artifactId}</imageName><!--鏡像名,注意:這里的鏡像名一定要小寫,如果你的應(yīng)用名字是大寫會(huì)報(bào)錯(cuò)的-->
<imageTags>
<imageTag>latest</imageTag>
</imageTags>
<dockerDirectory>${project.basedir}</dockerDirectory><!--Dockerfile所在的目錄-->
<dockerHost>http://127.0.0.1:2375</dockerHost><!--docker所在的宿主機(jī)地址,或者填寫http://yourip:2375-->
<resources>
<resource><!--這里配置的就是打包后jar所在的位置-->
<targetPath>/</targetPath>
<directory>${project.build.directory}</directory><!--構(gòu)建的class文件路徑 一般是target-->
<include>${project.build.finalName}.jar</include>
</resource>
</resources>
</configuration>
</plugin>
</plugins>
</build>
打包該應(yīng)用程序

打包后會(huì)發(fā)現(xiàn)target目錄下有jar包出現(xiàn)
配置Docker,此處配置要和pom文件最終生成的名字tag要保持一直

部署項(xiàng)目后使用localhost:8080/get/hello訪問返回?cái)?shù)據(jù)即為成功

docker控制臺(tái)中文亂碼修復(fù)[可選]

//添加字符參數(shù)后 重啟IDEA -Dfile.encoding=UTF-8 -Dsun.jnu.encoding=UTF-8
3.Docker基于Linux集成IDEA
待更新。。。
4.連接宿主機(jī)redis服務(wù)
//添加Redis依賴
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
//添加Redis配置
# spring default config
spring.redis:
host: your-ipv4 //宿主機(jī)的ip,如果你當(dāng)前啟動(dòng)項(xiàng)目的docker沒有安裝redis,此處填localhost會(huì)報(bào)錯(cuò)
port: 6379
timeout: 5000
lettuce.pool:
# max connection number in connection poll, default number is 8
max-active: 20
# max wait time, default -1, this means there is no restrict. Unit: ms
max-wait: -1
# max idle connection number, default is 8
max-idle: 8
# min idle connection number, default is 0
min-idle: 0
@Configuration
public class RedisConfig {
@Bean(name = "redisTemplate")
public StringRedisTemplate redisTemplate(RedisConnectionFactory redisConnectionFactory) {
StringRedisTemplate stringRedisTemplate = new StringRedisTemplate();
stringRedisTemplate.setConnectionFactory(redisConnectionFactory);
return stringRedisTemplate;
}
}
@RestController
@RequestMapping("/docker")
public class DockerController {
@Autowired
private StringRedisTemplate stringRedisTemplate;
@GetMapping("/redis/set")
public String setRedisData(@RequestParam("value") String value){
String key = "docker";
stringRedisTemplate.opsForValue().set(key, value);
String strValue = stringRedisTemplate.opsForValue().get(key);
return strValue;
}
}
//重新打包然后點(diǎn)擊docker進(jìn)行運(yùn)行
5.連接docker中redis服務(wù)
獲取redis的密碼

- 使用命令連接容器:docker exec -it containerName /bin/bash
- 使用命令連接redis客戶端:redis-cli
- 使用auth {password} 授權(quán)成功 可以進(jìn)行操作
在對(duì)spring-boot項(xiàng)目中修改配置之前,我們找到docker中redis在宿主機(jī)的端口號(hào),這樣我們才能保證連接成功。

修改項(xiàng)目中的配置
//添加Redis配置
# spring default config
spring.redis:
host: your-ipv4 //宿主機(jī)的ip,如果你當(dāng)前啟動(dòng)項(xiàng)目的docker沒有安裝redis,此處填localhost會(huì)報(bào)錯(cuò)
port: 49153 //和上面圖片的端口保持一致 <----第一處修改
password: redispw //添加密碼 <----第二處修改
timeout: 5000
lettuce.pool:
# max connection number in connection poll, default number is 8
max-active: 20
# max wait time, default -1, this means there is no restrict. Unit: ms
max-wait: -1
# max idle connection number, default is 8
max-idle: 8
# min idle connection number, default is 0
min-idle: 0
//重新打包進(jìn)行部署
到此這篇關(guān)于IDEA集成Docker實(shí)現(xiàn)一鍵部署的文章就介紹到這了,更多相關(guān)idea docker部署內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
docker使用Dockerfile構(gòu)建鏡像的方法
這篇文章主要介紹了docker使用Dockerfile構(gòu)建鏡像的方法,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-12-12
docker創(chuàng)建容器的兩種實(shí)現(xiàn)方式(交互式與守護(hù)式)
這篇文章主要介紹了docker創(chuàng)建容器的兩種實(shí)現(xiàn)方式(交互式與守護(hù)式),具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2025-05-05
docker自定義網(wǎng)絡(luò)從入門到實(shí)踐
Docker 網(wǎng)絡(luò)模式?jīng)Q定了容器之間、容器與宿主機(jī)之間的通信方式,掌握網(wǎng)絡(luò)機(jī)制是理解容器互聯(lián)互通的關(guān)鍵,本文給大家介紹docker自定義網(wǎng)絡(luò)從入門到實(shí)踐,感興趣的朋友跟隨小編一起看看吧2025-10-10
超詳細(xì)Docker Desktop下安裝rocketmq的教程
這篇文章主要介紹了Docker Desktop下安裝rocketmq,本文內(nèi)容通過圖文操作命令給大家講解的非常詳細(xì),需要的朋友可以參考下2021-10-10
淺析SpringBoot打包上傳到docker并實(shí)現(xiàn)多實(shí)例部署(IDEA版)
這篇文章主要介紹了SpringBoot打包上傳到docker并實(shí)現(xiàn)多實(shí)例部署(IDEA版),本文通過圖文并茂實(shí)例詳解的形式給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-04-04

