docker-compose ports和expose的区别详解

Esacpe 2020-01-25

docker-compose中æ两ç§æ¹å¼å¯ä»¥æ´é²å®¹å¨ç端å£ï¼portsåexposeã

ports

portsæ´é²å®¹å¨ç«¯å£å°ä¸»æºçä»»æ端å£ææå®ç«¯å£ï¼ç¨æ³ï¼

ports:
 
- "80:80" # ç»å®å®¹å¨ç80端å£å°ä¸»æºç80端å£
 
- "9000:8080" # ç»å®å®¹å¨ç8080端å£å°ä¸»æºç9000端å£
 
- "443" # ç»å®å®¹å¨ç443端å£å°ä¸»æºçä»»æ端å£ï¼å®¹å¨å¯å¨æ¶éæºåéç»å®ç主æºç«¯å£å·

ä¸ç®¡æ¯å¦æå®ä¸»æºç«¯å£ï¼ä½¿ç¨portsé½ä¼å°ç«¯å£æ´é²ç»ä¸»æºã

容å¨ä¸­å¯ä»¥è¿è¡ä¸äºç½ç»åºç¨ï¼è¦è®©å¤é¨ä¹å¯ä»¥è®¿é®è¿äºåºç¨ï¼å¯ä»¥éè¿ -Pï¼å¤§åï¼ æ -p ï¼å°åï¼ åæ°æ¥æå®ç«¯å£æ å°ã

(1) å½ä½¿ç¨-Pæ è®°æ¶ï¼Docker ä¼éæºæ å°ä¸ä¸ª49000~49900ç端å£å°åé¨å®¹å¨å¼æ¾çç½ç»ç«¯å£ã

使ç¨docker pså¯ä»¥çå°ï¼æ¬å°ä¸»æºç 49155 被æ å°å°äºå®¹å¨ç 5000 端å£ãæ­¤æ¶è®¿é®æ¬æºç 49155 端å£å³å¯è®¿é®å®¹å¨å web åºç¨æä¾ççé¢ã

$ sudo docker run -d -P training/webapp python app.py
 
$ sudo docker ps -l
 
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
 
bc533791f3f5 training/webapp:latest python app.py 5 seconds ago Up 2 seconds 0.0.0.0:49155->5000/tcp nostalgic_morse

åæ ·çï¼å¯ä»¥éè¿docker logså½ä»¤æ¥æ¥çåºç¨çä¿¡æ¯ã

$ sudo docker logs -f nostalgic_morse
 
* Running on http://0.0.0.0:5000/
 
10.0.2.2 - - [23/May/2014 20:16:31] "GET / HTTP/1.1" 200 -
 
10.0.2.2 - - [23/May/2014 20:16:31] "GET /favicon.ico HTTP/1.1" 404 -

(2) -pï¼å°åï¼åå¯ä»¥æå®è¦æ å°çIPå端å£ï¼ä½æ¯å¨ä¸ä¸ªæå®ç«¯å£ä¸åªå¯ä»¥ç»å®ä¸ä¸ªå®¹å¨ãæ¯æçæ ¼å¼æ hostPort:containerPortãip:hostPort:containerPortãip::containerPortã

expose

exposeæ´é²å®¹å¨ç»linkå°å½å容å¨ç容å¨ï¼ç¨æ³ï¼

expose:
- "3000"
- "8000"

以ä¸æ令å°å½å容å¨ç端å£3000å8000æ´é²ç»linkå°æ¬å®¹å¨ç容å¨ã

åportsçåºå«æ¯ï¼exposeä¸ä¼å°ç«¯å£æ´é²ç»ä¸»æºã

以ä¸å°±æ¯æ¬æçå¨é¨å容ï¼å¸æ对大家ç学习ææ帮å©ï¼ä¹å¸æ大家å¤å¤æ¯æèæ¬ä¹å®¶ã

相关推荐