目錄
- 1.初識Docker
- 2.Docker基于Windows集成IDEA
- 2.1 在window上安裝docker
- 2.2設(shè)置docker配置
- 2.3 IDEA 連接docker 測試
- 2.4啟動Springboot應(yīng)用測試
- 3.Docker基于Linux集成IDEA
- 4.連接宿主機redis服務(wù)
- 5.連接docker中redis服務(wù)
在和前端聯(lián)調(diào)的過程中,然后每次電腦使用IDEA將服務(wù)啟動后不能動,然后自己想改變代碼后重啟可能導(dǎo)致前端那邊報錯,所以為了給前端提供聯(lián)調(diào)的服務(wù)的同時,我自己還可以正常工作,于是便想到了使用docker的方式,這樣就可以達到了兩全其美,何樂而不為。
1.初識Docker
Docker的三個基本概念:
- Dockerfile:鏡像構(gòu)建的模板,描述鏡像構(gòu)建的步驟,通常是拉去一些文件和依賴;
- image:鏡像,一個文件,用來創(chuàng)建容器。
- container:容器,一個可運行的鏡像實例,里面運行著一個完整的操作系統(tǒng),可以做一切你當前操作系統(tǒng)可以做的事情。
從我的理解對上述三者做一個類比:dockerfile就是一個混凝土配比說明書(原材料,步驟等),根據(jù)該說明書攪拌出混凝土(鏡像),然后基于混凝土可以做成一個一個房間(容器),每個房間都是相互獨立,生活著不同的人。
對于我們開發(fā)人員來說,Docker 可以做到:
- 編寫本地代碼
- 使用 Docker 將程序推送到測試環(huán)境
- 發(fā)現(xiàn) bug 后在開發(fā)環(huán)境下修復(fù),重新部署到測試環(huán)境測試
- 測試完成將代碼合并到發(fā)布的代碼分支
2.Docker基于Windows集成IDEA
2.1 在window上安裝docker
注意一點:一定要把windows的WSL開啟后再安裝,否則會導(dǎo)致docker啟動不成功。
2.2設(shè)置docker配置
- 開放2375端口,勾上該選項
新增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)的其他機器可以使用局域網(wǎng)ipv4連接;如果是遠程機器的話使用公網(wǎng)ip連接。
如上圖中出現(xiàn)Connection successful為成功標志
// 當使用ip訪問時連接不成功的話在windows的admin權(quán)限終端窗口執(zhí)行如下命令,端口代理 netsh interface portproxy add v4tov4 listenport=2375 connectaddress=127.0.0.1 connectport=2375 listenaddress=<your ipv4> protocol=tcp //對2375端口添加防火墻規(guī)則 netsh advfirewall firewall add rule name="docker_daemon" dir=in action=allow protocol=TCP localport=2375
說說小編的個人經(jīng)歷:完成了宿主機配置后,在局域網(wǎng)內(nèi)的其他機器都是可以連接docker的,但是第二天早上再次連接就不行了,然后搞了好幾天還是不行,突然一個偶然的機會又能重新連接上了。
//執(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啟動Springboot應(yīng)用測試
- 構(gòu)建測試項目
@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); } }
在項目中添加Dockerfile文件
#這是基礎(chǔ)鏡像 FROM java:8 VOLUME /tmp #復(fù)制jar包到鏡像中,并且將名字改成app.jar ADD ./target/SpringBootWithDocker-1.0-SNAPSHOT.jar DemoApp.jar #在容器啟動的時候運行命令,來啟動我們的項目(這其實就是一段Linux命令,該命令可以在服務(wù)啟動時加一些參數(shù)) ENTRYPOINT ["sh", "-c", "java -jar DemoApp.jar"]
上述注意一點:該文件的放置位置會影響ADD后面的尋找jar包的路徑,因為我后面在build鏡像時出現(xiàn)找不到j(luò)ar的報錯,原因就是我將該Dockerfile放在了該項目的某一個文件夾下了。
項目結(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)用名字是大寫會報錯的--> <imageTags> <imageTag>latest</imageTag> </imageTags> <dockerDirectory>${project.basedir}</dockerDirectory><!--Dockerfile所在的目錄--> <dockerHost>http://127.0.0.1:2375</dockerHost><!--docker所在的宿主機地址,或者填寫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)用程序
打包后會發(fā)現(xiàn)target目錄下有jar包出現(xiàn)
配置Docker,此處配置要和pom文件最終生成的名字tag要保持一直
部署項目后使用localhost:8080/get/hello訪問返回數(shù)據(jù)即為成功
docker控制臺中文亂碼修復(fù)[可選]
//添加字符參數(shù)后 重啟IDEA -Dfile.encoding=UTF-8 -Dsun.jnu.encoding=UTF-8
3.Docker基于Linux集成IDEA
待更新。。。
4.連接宿主機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 //宿主機的ip,如果你當前啟動項目的docker沒有安裝redis,此處填localhost會報錯 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; } } //重新打包然后點擊docker進行運行
5.連接docker中redis服務(wù)
獲取redis的密碼
- 使用命令連接容器:docker exec -it containerName /bin/bash
- 使用命令連接redis客戶端:redis-cli
- 使用auth {password} 授權(quán)成功 可以進行操作
在對spring-boot項目中修改配置之前,我們找到docker中redis在宿主機的端口號,這樣我們才能保證連接成功。
修改項目中的配置
//添加Redis配置 # spring default config spring.redis: host: your-ipv4 //宿主機的ip,如果你當前啟動項目的docker沒有安裝redis,此處填localhost會報錯 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 //重新打包進行部署