Spring Boot应用的Docker容器化部署学习:从入门到实践

当前位置:首页 > 广场 > Spring Boot应用的Docker容器化部署学习:从入门到实践

Spring Boot应用的Docker容器化部署学习:从入门到实践

2024-11-18广场23

---

Spring Boot应用的Docker容器化部署学习:从入门到实践

Spring Boot涓嶥ocker锛氬畬缇庤瀺鍚堢殑鑹烘湳

姒傝堪绡囩珷

韪忓叆鏁板瓧鍖栨椂浠o紝Docker瀹瑰櫒鍖栨妧鏈笌Spring Boot搴旂敤鐨勭粨鍚堝凡鎴愪负寮€鍙戣€呭績涓殑涓嶄簩涔嬮€夈€傛垜浠皢寮曢浣犺蛋杩汥ocker鐨勪笘鐣岋紝鐞嗚В鍏跺己澶х殑瀹瑰櫒鍖栨妧鏈紝骞舵帰绱㈠浣曚笌Spring Boot搴旂敤瀹岀編铻嶅悎銆傞€氳繃鏈瀛︿範涔嬫梾锛屼綘灏嗘繁鍏ヤ簡瑙e浣曟瀯寤篠pring Boot搴旂敤銆佹帉鎻ocker鍩虹鎿嶄綔銆佹垚鍔熷皢搴旂敤閮ㄧ讲鍒癉ocker瀹瑰櫒涓紝骞跺弬涓庡疄鎴樻紨缁冦€傛垜浠皢浼氬甫浣犱粠鐞嗚ВDocker鐨勫熀鏈蹇靛紑濮嬶紝閫愭娣卞叆浜嗚В鍏跺畨瑁呯幆澧冦€丏ocker鍛戒护鎿嶄綔绛夛紝鐩村埌绮鹃€歋pring Boot搴旂敤鍦―ocker涓殑闀滃儚鏋勫缓銆佸鍣ㄩ儴缃蹭笌绠$悊銆傛渶缁堬紝浣犲皢閫氳繃瀹為檯妗堜緥閮ㄧ讲涓€涓畬鏁寸殑Spring Boot搴旂敤鍒癉ocker鐜锛屽疄鐜伴珮鏁堣嚜鍔ㄥ寲閮ㄧ讲涓庤法鐜涓€鑷存€с€?/p>

寮曡█绡囩珷锛欴ocker涓嶴pring Boot鐨勮仈濮讳箣缇?/p>

鍦ㄨ蒋浠跺紑鍙戠殑鐜颁唬娴疆涓紝Docker瀹瑰櫒鍖栨妧鏈壆婕旂潃涓捐冻杞婚噸鐨勮鑹层€傚畠鍍忎竴浣嶇濂囩殑榄旀湳甯堬紝灏嗗簲鐢ㄥ強鍏朵緷璧栭」鎵撳寘鎴愪竴涓交閲忕骇銆佸彲绉绘鐨勭嫭绔嬭繍琛屽崟鍏冿紝纭繚搴旂敤鑳藉湪浠讳綍鐜涓鑸炶€呰埇浼橀泤鍦扮咯缈╄捣鑸炪€傝€孲pring Boot浣滀负Spring妗嗘灦鐨勫瓙椤圭洰锛屽ぉ鐢熷氨鍏峰绠€鍖朣pring搴旂敤鍒涘缓銆佸紑鍙戙€佹祴璇曞拰閮ㄧ讲鐨勫ぉ璧嬨€傚綋杩欎袱鑰呯粨鍚堟椂锛屽氨濡傚悓澶╀綔涔嬪悎锛屼笉浠呰兘鎻愬崌搴旂敤鐨勯儴缃叉晥鐜囷紝杩樿兘瀹炵幇璺ㄧ幆澧冪殑涓€鑷存€э紝浣垮緱搴旂敤鐨勭淮鎶ゃ€佹墿灞曞彉寰楁洿鍔犺交鏉句究鎹枫€?/p>

瀛︿範鐩爣姒傝

缁忚繃杩欑瘒鏂囩珷鐨勬礂绀硷紝浣犲皢鑳藉锛?/p>

娲炴倝Docker瀹瑰櫒鍖栨妧鏈殑濂ョ锛屽寘鎷叾鍩烘湰鐞嗗康銆佸伐浣滃師鐞嗗強閫傜敤鍦烘櫙銆?/p>

鎺屾彙Spring Boot搴旂敤鐨勬瀯寤轰箣閬擄紝浠庡垱寤哄熀纭€搴旂敤鍒颁娇鐢―ocker杩涜鎵撳寘銆侀暅鍍忔瀯寤哄拰瀹瑰櫒閮ㄧ讲銆?/p>

鐔熺粌杩愮敤Docker CLI锛堝懡浠よ鐣岄潰锛夛紝杞绘澗杩涜瀹瑰櫒鐨勫垱寤恒€佸惎鍔ㄣ€佸仠姝㈠拰閲嶅惎绛夋搷浣溿€?/p>

瀹炴垬閮ㄧ讲Spring Boot搴旂敤鍒癉ocker瀹瑰櫒锛屾棤璁烘槸鏈湴杩樻槸浜戠鐜閮借兘娓稿垉鏈変綑銆?/p>

閫氳繃瀹為檯妗堜緥锛屽畬鏁翠綋楠孲pring Boot搴旂敤鍦―ocker瀹瑰櫒涓殑閮ㄧ讲杩囩▼锛屽苟杩涜娴嬭瘯涓庝紭鍖栥€?/p>

Spring Boot搴旂敤鍒濇帰锛氬熀纭€姒傚康涓庢瀯寤?/p>

璁╂垜浠厛鏉ヤ簡瑙pring Boot杩欎綅鈥滄柊鏄熲€濄€係pring Boot鏄竴涓揩閫熸瀯寤哄崟涓湇鍔$殑妗嗘灦锛屽畠鑷甫鍚勭閰嶇疆鐨勯粯璁ゅ€硷紝澶уぇ闄嶄綆浜嗛厤缃拰渚濊禆绠$悊鐨勫鏉傛€э紝璁╁紑鍙戦€熷害濡傜伀绠埇椋炲崌銆備竴涓猄pring Boot搴旂敤閫氬父鐢变互涓嬪嚑涓富瑕佺粍浠舵瀯鎴愶細

涓诲簲鐢ㄧ被锛圓pplication.java锛夛細閫氬父甯︽湁@SpringBootApplication娉ㄨВ锛屽苟鎼哄甫鐫€鍚姩鏁翠釜搴旂敤鐨刴ain鏂规硶銆?/p>

閰嶇疆绫伙紙Configuration.java锛夛細鐢ㄤ簬鑷畾涔夌洃鎺с€佹棩蹇椼€佹暟鎹簱杩炴帴绛夊叧閿厤缃€?/p>

涓氬姟閫昏緫灞傦細鍖呭惈瀹炵幇鍏蜂綋涓氬姟鍔熻兘鐨勭被鍜屾柟娉曘€?/p>

鏋勫缓鍩虹Spring Boot搴旂敤锛熻交鏉惧姞鎰夊揩锛佸彧闇€绠€鍗曞嚑姝ワ細

1. 鍒濆鍖朣pring Boot椤圭洰锛屽彲鍒╃敤Maven鎴朑radle鐨勬ā鏉挎彃浠惰交鏉惧畬鎴愩€?/p>

2. 缂栧啓涓诲簲鐢ㄧ被锛屽垱寤轰竴涓悕涓篈pplication.java鐨勬枃浠讹紝骞舵爣娉ㄤ笂@SpringBootApplication娉ㄨВ銆?/p>

3. 鑻ラ渶瑕佽嚜瀹氫箟閰嶇疆锛屽氨鍦╯rc/main/java鐩綍涓嬪垱寤虹浉搴旂殑閰嶇疆绫汇€?/p>

璧拌繘Docker瀹瑰櫒鍖栨妧鏈殑濂囧涓栫晫

鍦ㄦ棩鏂版湀寮傜殑杞欢鎶€鏈鍩燂紝Docker瀹瑰櫒鍖栨妧鏈凡鐒舵垚涓哄紑鍙戣€呯殑寰楀姏鍔╂墜銆傝鎴戜滑涓€鍚屾帰绱ocker鐨勯瓍鍔涳紝娣卞叆浜嗚В瀹冨浣曟敼鍙樺簲鐢ㄧ殑閮ㄧ讲涓庣鐞嗘柟寮忋€?/p>

Docker瀹瑰櫒鐨勬蹇靛垵鎺?/p>

Docker瀹瑰櫒锛屽熀浜庢搷浣滅郴缁熺骇铏氭嫙鍖栨妧鏈紝涓哄紑鍙戣€呮彁渚涗簡涓€涓皢搴旂敤鍙婂叾渚濊禆椤规墦鍖呮垚鐙珛杩愯鐜鐨勮В鍐虫柟妗堛€傝繖浜涘鍣ㄥ叡浜涓绘満鐨勫唴鏍革紝璧勬簮鍗犵敤鏋佸皬锛屽惎鍔ㄨ繀閫燂紝澶уぇ鎻愰珮浜嗚祫婧愬埄鐢ㄧ巼鍜屽簲鐢ㄩ儴缃叉晥鐜囥€傛洿閲嶈鐨勬槸锛屽鍣ㄧ殑杩愯鐜涓€鑷存€э紝纭繚浜嗗簲鐢ㄥ湪涓嶅悓鐜涓兘鑳界ǔ瀹氥€佷竴鑷村湴杩愯銆?/p>

鎼缓Docker鐜

瑕佸紑濮婦ocker鐨勬梾绋嬶紝棣栧厛闇€瑕佸湪璁$畻鏈轰笂瀹夎Docker銆傚畨瑁呰繃绋嬬畝娲佹槑浜嗭紝鏍规嵁涓嶅悓鐨勬搷浣滅郴缁燂紝瀹夎鎸囦护涔熸湁鎵€涓嶅悓銆?/p>

馃搶瀵逛簬Windows鐢ㄦ埛锛岃璁块棶Docker瀹樼綉涓嬭浇Windows鐗堟湰鐨凞ocker Desktop銆?/p>

馃搶瀵逛簬Linux鐢ㄦ埛锛屽彲浠ュ湪鍙戣鐗堢殑杞欢浠撳簱涓交鏉惧畨瑁呫€備緥濡傚湪Ubuntu涓婏紝浣跨敤浠ヤ笅鍛戒护锛?/p>

```bash

sudo apt-get update

sudo apt-get install docker.io

```

馃搶瀵逛簬macOS鐢ㄦ埛锛屽彲浠ヤ娇鐢℉omebrew杞绘澗瀹夎Docker锛?/p>

```bash

brew install docker

```

楠岃瘉瀹夎鎴愭灉

瀹夎瀹屾垚鍚庯紝鍙渶杩愯浠ヤ笅鍛戒护锛屽嵆鍙獙璇丏ocker鏄惁鎴愬姛瀹夎锛?/p>

```bash

docker --version

```

濡傛灉杩斿洖Docker鐨勭増鏈俊鎭紝閭d箞鎭枩浣犲凡缁忔垚鍔熷畨瑁呬簡Docker銆?/p>

Docker鍩虹鍛戒护鎿嶄綔鎸囧崡

鎺ヤ笅鏉ワ紝璁╂垜浠啛鎮変竴浜涘熀鏈殑Docker鍛戒护鍙婂叾鐢ㄦ硶锛?/p>

馃敼鍒涘缓骞惰繍琛屽鍣細

```bash

docker run -d -p :image_name

```

鍙傛暟瑙i噴锛?d琛ㄧず鍚庡彴杩愯瀹瑰櫒锛?p鏄犲皠涓绘満绔彛鍒板鍣ㄧ鍙o紱image_name鏄鍣ㄩ暅鍍忕殑鍚嶇О銆?/p>

馃敼鏌ョ湅瀹瑰櫒锛?/p>

```bash

docker ps

```

鏄剧ず褰撳墠鎵€鏈夋鍦ㄨ繍琛岀殑瀹瑰櫒銆?/p>

馃敼鍋滄瀹瑰櫒锛?/p>

```bash

docker stop container_id

```

浣跨敤瀹瑰櫒鐨処D鎴栧悕绉版潵鍋滄瀹瑰櫒銆?/p>

馃敼鍒犻櫎瀹瑰櫒锛?/p>

```bash

docker rm container_id

```

鍒犻櫎宸插仠姝㈢殑瀹瑰櫒銆?/p>

馃敼鍒楀嚭鎵€鏈夐暅鍍忥細

```bash

docker images

```

鏌ョ湅鎵€鏈夊彲鐢ㄧ殑Docker闀滃儚銆?/p>

---

鍒╃敤Dockerfile鏋勫缓Docker闀滃儚鐨勫疄鎴樻寚鍗?/p>

鍦ㄨ繖涓暟瀛楀寲鏃朵唬锛孌ocker宸叉垚涓哄簲鐢ㄩ儴缃茬殑鏍囬厤宸ュ叿銆傞€氳繃Dockerfile锛屾垜浠彲浠ヨ交鏉惧湴灏嗗簲鐢ㄦ墦鍖呮垚闀滃儚锛屽啀閮ㄧ讲鍒癉ocker瀹瑰櫒涓€傞偅涔堬紝濡備綍鏋勫缓Docker闀滃儚骞堕儴缃插簲鐢ㄥ憿锛熻鎴戜滑涓€姝ユ娣卞叆浜嗚В銆?/p>

浣犻渶瑕佷竴涓狣ockerfile銆傝繖鏄竴涓熀鏈殑Dockerfile绀轰緥锛?/p>

浣跨敤瀹樻柟Java杩愯鏃朵綔涓哄熀纭€闀滃儚锛?/p>

```Dockerfile

FROM openjdk:8-jdk-alpine

```

璁剧疆宸ヤ綔鐩綍鍜屽鍒舵枃浠讹細

```Dockerfile

WORKDIR /app

COPY . /app

```

璁剧疆鐜鍙橀噺鍜岀鍙o細

```Dockerfile

ENV JAVA_OPTS=""

EXPOSE 8080

```

瀹夎渚濊禆鍜屾竻鐞嗭細

```Dockerfile

RUN apt-get update && apt-get install -y curl && rm -rf /var/lib/apt/lists/

```

璁剧疆榛樿鍏ュ彛鐐瑰拰杩愯搴旂敤绋嬪簭鐨勫懡浠わ細

```Dockerfile

ENTRYPOINT ["java", "-Djava.security.egd=file:/dev/./urandom", "-jar", "target/.jar"]

CMD ["java", "-Djava.security.egd=file:/dev/./urandom", "-jar", "target/.jar"]

``` 鎺ヤ笅鏉ユ槸鏋勫缓Docker闀滃儚鐨勫懡浠わ細 褰撲綘瀹屾垚浜咲ockerfile鐨勭紪鍐欏悗锛屽氨鍙互寮€濮嬫瀯寤轰綘鐨凞ocker闀滃儚浜嗐€備娇鐢ㄤ互涓嬪懡浠ゆ潵鏋勫缓闀滃儚锛?/p>

```bash

docker build -t <闀滃儚鍚嶇О>:<鏍囩> .

``` 鍏朵腑锛宍<闀滃儚鍚嶇О>`鏄綘涓洪暅鍍忚瀹氱殑鍚嶅瓧锛岃€宍<鏍囩>`鍒欐槸浣犱负闀滃儚璁惧畾鐨勬爣绛俱€?鏋勫缓瀹屾垚鍚庯紝浣犲氨鍙互寮€濮嬮儴缃睤ocker瀹瑰櫒浜嗐€備娇鐢ㄤ互涓嬪懡浠ゆ潵杩愯浣犵殑Docker瀹瑰櫒锛?浣犱細鐪嬪埌瀹瑰櫒鍦ㄥ悗鍙板紑濮嬭繍琛岋紝涓绘満鐨勭鍙h鏄犲皠鍒板鍣ㄧ殑绔彛涓娿€?杩欐椂锛屼綘鐨勫簲鐢ㄥ氨宸茬粡鎴愬姛閮ㄧ讲鍦―ocker瀹瑰櫒涓簡锛?浣犲彲浠ラ€氳繃璁块棶搴旂敤鐨刄RL鏉ラ獙璇侀儴缃叉槸鍚︽垚鍔熴€?闄や簡鍗曚釜瀹瑰櫒鐨勯儴缃诧紝Docker杩樻彁渚涗簡Docker Compose鏉ョ鐞嗗瀹瑰櫒搴旂敤銆傚浜庡鏉傜殑澶氬鍣ㄥ簲鐢紝浣犲彲浠ュ垱寤轰竴涓悕涓篸ocker-compose.yml鐨勬枃浠舵潵瀹氫箟鍜岀鐞嗗涓鍣ㄣ€備緥濡傦細 鍦ㄨ繖涓猋AML鏂囦欢涓紝鎴戜滑瀹氫箟浜嗕袱涓湇鍔★細web鍜宒b銆傞€氳繃鎵ц浠ヤ笅鍛戒护鏉ュ惎鍔ㄥ瀹瑰櫒搴旂敤锛?杩欐椂锛屼綘鐨勫瀹瑰櫒搴旂敤灏卞凡缁忓紑濮嬭繍琛屼簡銆傝鍋滄鎵€鏈夋湇鍔★紝鍙渶鎵ц浠ヤ笅鍛戒护锛?濡傛灉浣犳湁涓€涓熀浜嶴pring Boot鐨刉eb搴旂敤骞舵兂灏嗗叾閮ㄧ讲鍒癉ocker瀹瑰櫒涓紝浣犲彲浠ユ寜鐓т互涓嬫楠よ繘琛岋細 姝ラ1鏄垱寤篠pring Boot椤圭洰锛涙楠?鏄紪鍐橠ockerfile锛涙楠?鏄瀯寤篋ocker闀滃儚锛涙楠?鏄儴缃睤ocker瀹瑰櫒锛涙渶鍚庝竴姝ユ槸娴嬭瘯涓庝紭鍖栥€傚亣璁句綘宸茬粡瀹屾垚浜嗚繖浜涙楠ゅ苟姝g‘閰嶇疆浜嗙幆澧冿紝浣犲彲浠ラ€氳繃鎵ц浠ヤ笅鍛戒护鏉ラ儴缃蹭綘鐨勫簲鐢細 閫氳繃璁块棶搴旂敤鐨刄RL锛屼綘鍙互楠岃瘉搴旂敤鏄惁宸茬粡鎴愬姛閮ㄧ讲銆備负浜嗙‘淇濆簲鐢ㄩ儴缃叉垚鍔燂紝浣犺繕鍙互浣跨敤Docker鐨刣ocker logs鍛戒护鎴朌ocker Compose鏉ョ洃鎺ф棩蹇楀拰搴旂敤鐘舵€併€?鐜板湪锛屼綘宸茬粡鎺屾彙浜嗗浣曚娇鐢―ockerfile鏋勫缓Docker闀滃儚骞堕儴缃插簲鐢ㄧ殑鍩烘湰姝ラ銆傞€氳繃瀹炶返鍜屼笉鏂殑瀛︿範锛屼綘灏嗚兘澶熸洿娣卞叆鍦颁簡瑙ocker鍜屽鍣ㄧ殑鐩稿叧鐭ヨ瘑锛屼负鏈潵鐨勯」鐩墦涓嬪潥瀹炵殑鍩虹銆傝繄鍚慏ocker瀹瑰櫒鍖栭儴缃蹭箣鏃咃細鎺屾彙Spring Boot搴旂敤鐨勬瀬鑷翠綋楠?/p>

鍦ㄥ畬鎴愭湰绡囨枃妗g殑瀛︿範鍚庯紝浣犲皢寮€鍚竴娈靛叏鏂扮殑鏃呯▼鈥斺€斿皢Spring Boot搴旂敤鎴愬姛閮ㄧ讲鍒癉ocker瀹瑰櫒涓€傝繖涓嶆槸涓€娆$畝鍗曠殑浠诲姟锛岃€屾槸涓€娆″疄鐜拌法鐜涓€鑷存€т笌楂樻晥鑷姩鍖栭儴缃茬殑鎺㈤櫓銆?/p>

鎯宠薄涓€涓嬶紝浣犲皢浜叉墜鎿嶄綔锛屽皢涓€涓椿璺冪殑搴旂敤锛岃浆鍖栦负鑳藉湪Docker瀹瑰櫒涓嚜濡傝繍琛岀殑褰㈡€併€傝繖涓繃绋嬩笉浠呰浣犳繁鍏ヤ簡瑙pring Boot涓嶥ocker鐨勬繁搴﹁瀺鍚堬紝鏇存槸涓€娆″疄璺甸儴缃叉渚嬶紝绉疮瀹濊吹缁忛獙鐨勭粷浣虫満浼氥€?/p>

鍦ㄨ繖涓繃绋嬩腑锛屼綘灏嗘帴瑙﹀埌涓嶅彧鏄畝鍗曠殑閮ㄧ讲姝ラ锛岃繕灏嗘繁鍏ユ帰绱㈡洿澶嶆潅鐨勫簲鐢ㄥ満鏅€備緥濡傦紝浣犲皢鎻紑寰湇鍔℃灦鏋勭殑绁炵闈㈢罕锛屼簡瑙e叾濡備綍涓庡鍣ㄦ妧鏈畬缇庤瀺鍚堛€備綘杩樹細鎺ヨЕ鍒板鍣ㄧ紪鎺掑伐鍏凤紝濡侹ubernetes锛屽涔犲浣曞皢鍏堕泦鎴愬埌浣犵殑搴旂敤涓紝杩涗竴姝ユ彁鍗囦綘鐨勬妧鏈疄鍔涖€?/p>

涓轰簡鎸佺画瀛︿範鍜屽疄璺碉紝鎴戜滑榧撳姳浣犳繁鍏ユ帰绱€侱ocker瀹樻柟鏂囨。銆丼pring Boot瀹樻柟鏂囨。浠ュ強鎱曡缃戠瓑鍦ㄧ嚎瀛︿範璧勬簮鏄綘鐨勬渶浣充紮浼淬€傝繖浜涜祫婧愬皢甯姪浣犱笉鏂墿灞曟妧鑳借竟鐣岋紝鏃犺鏄湪鐞嗚鐭ヨ瘑杩樻槸瀹炶返鎿嶄綔涓婇兘鑳藉緱鍒板叏闈㈢殑鎻愬崌銆?/p>

褰撲綘瀹屾垚杩欐鏃呯▼鍚庯紝浣犲皢涓嶄粎浠呮槸涓€涓猄pring Boot鎴朌ocker鐨勫紑鍙戣€咃紝浣犲皢鏄竴涓帉鎻′簡鐜颁唬搴旂敤閮ㄧ讲鎶€鏈殑涓撲笟浜哄+锛屽噯澶囧ソ杩庢帴浠讳綍鎶€鏈寫鎴樸€?/p>

涓嶈鍋滄瀛︿範鐨勬浼愶紝缁х画鍓嶈繘锛屽洜涓哄湪鎶€鏈殑涓栫晫閲岋紝姘歌繙鏈夋湭鐭ョ瓑寰呬綘鍘绘帰绱紝姘歌繙鏈夋柊鐨勬妧鑳界瓑寰呬綘鍘绘帉鎻°€傝鎴戜滑涓€璧凤紝杩堝悜鏇村箍闃旂殑鏈潵锛?/p>

文章从网络整理,文章内容不代表本站观点,转账请注明【蓑衣网】

本文链接:https://www.baoguzi.com/68636.html

Spring Boot应用的Docker容器化部署学习:从入门到实践 | 分享给朋友: