ÔõÑùÔÚLinuxÉÏʹÓÃDocker¹¹½¨ÈÝÆ÷»¯µÄ΢ЧÀͼܹ¹£¿
ÔõÑùÔÚlinuxÉÏʹÓÃdocker¹¹½¨ÈÝÆ÷»¯µÄ΢ЧÀͼܹ¹£¿
СÐò£º
Ëæ×ÅÔÆÅÌËãºÍÈÝÆ÷ÊÖÒÕµÄÆÕ¼°£¬Î¢Ð§ÀͼÜ×é³ÉΪÁË¿ª·¢ÕßÃǵÄÊ×Ñ¡¡£Ëü¿ÉÒÔÈÃÓ¦ÓóÌÐòƾ֤һ×éС¶ø×ÔÖεÄÄ£¿é¾ÙÐпª·¢¡¢²âÊԺͰ²ÅÅ£¬ÌáÉý¿ª·¢Ð§ÂʺÍÎÞаÐÔ¡£¶øDocker×÷ΪÏÖÔÚ×îÊ¢ÐеÄÈÝÆ÷ÊÖÒÕÖ®Ò»£¬ÎªÎ¢Ð§À͵Ĺ¹½¨ºÍ°²ÅÅÌṩÁ˱㵱¡£±¾ÎĽ«ÏÈÈÝÔÚLinuxÉÏʹÓÃDocker¹¹½¨ÈÝÆ÷»¯µÄ΢ЧÀͼܹ¹µÄÒªÁ죬ͬʱÌṩÏìÓ¦µÄ´úÂëʾÀý¡£
Ò»¡¢×°ÖÃDockerºÍDocker Compose
ÔÚ×îÏÈ֮ǰ£¬Ê×ÏÈÐèÒªÔÚLinuxϵͳÉÏ×°ÖÃDockerºÍDocker Compose¡£ÏêϸµÄ×°ÖÃÒªÁì¿ÉÒԲο¼Docker¹Ù·½Îĵµ¡£
¶þ¡¢½¨ÉèDocker¾µÏñ
ÔÚʹÓÃDocker¹¹½¨Î¢Ð§Àͼܹ¹Ö®Ç°£¬ÎÒÃÇÐèÒªÏȽ¨ÉèÊÊÓÃÓÚÿ¸ö΢ЧÀ͵ÄDocker¾µÏñ¡£ÏÂÃæÒÔÒ»¸ö¼òÆÓµÄwebЧÀÍΪÀý£¬ÑÝʾÔõÑù½¨ÉèDocker¾µÏñ¡£
½¨ÉèÒ»¸öÎļþ¼Ð£¬²¢ÔÚÆäÖн¨ÉèÒ»¸öDockerfile£¬ÓÃÓÚ¹¹½¨¾µÏñ¡£¿ÉÒÔʹÓÃÒÔÏÂÏÂÁ
mkdir web-service && cd web-service
touch Dockerfile
ÔÚDockerfileÖÐÌí¼ÓÒÔÏÂÄÚÈÝ£º
FROM python:3.8
WORKDIR /app
COPY requirements.txt .
RUN pip install –no-cache-dir -r requirements.txt
COPY . .
CMD [“python”, “app.py”]
ÆäÖУ¬FROMÖ¸¶¨ÁË»ù´¡¾µÏñ£¬ÕâÀïʹÓõÄÊÇpython:3.8¡£WORKDIRÖ¸¶¨ÁËÊÂÇéĿ¼£¬COPYÓÃÓÚ¿½±´Ó¦ÓóÌÐòÎļþµ½¾µÏñÖУ¬CMDÖ¸¶¨ÁËÈÝÆ÷Æô¶¯ºóÔËÐеÄÏÂÁî¡£
½¨Éèrequirements.txtÎļþ£¬²¢Ìí¼ÓÓ¦ÓóÌÐòµÄÒÀÀµÏî¡£¿ÉÒÔʹÓÃÒÔÏÂÏÂÁ
touch requirements.txt
½«Ó¦ÓóÌÐòÎļþ¿½±´µ½Ä¿½ñĿ¼£¬²¢Ìí¼ÓËùÐèµÄÒÀÀµÏȻºó£¬ÔËÐÐÒÔÏÂÏÂÁî¹¹½¨Docker¾µÏñ£º
docker build -t web-service .
ÖÁ´Ë£¬ÎÒÃÇÀֳɽ¨ÉèÁËÒ»¸öÓÃÓÚwebЧÀ͵ÄDocker¾µÏñ¡£
Èý¡¢Ê¹ÓÃDocker Compose±àÅÅ΢ЧÀͼܹ¹
Docker ComposeÊÇÒ»ÖÖ¹¤¾ß£¬¿ÉÒÔ½ç˵ºÍÖÎÀíÈÝÆ÷»¯Ó¦ÓóÌÐòµÄ¶à¸öЧÀÍ¡£ÏÂÃæÒÔÒ»¸ö¼òÆÓµÄʾÀýÀ´ÑÝʾÔõÑùʹÓÃDocker Compose±àÅÅ΢ЧÀͼܹ¹¡£
½¨ÉèÒ»¸ödocker-compose.ymlÎļþ£¬²¢Ìí¼ÓÒÔÏÂÄÚÈÝ£º
version: ‘3’
services:
web:
build:
context: ./web-service
dockerfile: Dockerfile
ports:
8080:8080
depends_on:
db
db:
image: postgres
ports:
5432:5432
ÆäÖУ¬versionÖ¸¶¨ÁËDocker ComposeµÄ°æ±¾£¬services½ç˵ÁËÿ¸öЧÀ͵Ĺ¹½¨ºÍÉèÖá£ÔÚÕâ¸öʾÀýÖУ¬ÎÒÃǽç˵ÁËÒ»¸öwebЧÀͺÍÒ»¸ödbЧÀÍ£¬webЧÀÍÒÀÀµÓÚdbЧÀÍ¡£
ÔËÐÐÒÔÏÂÏÂÁîÆô¶¯Î¢Ð§Àͼܹ¹£º
docker-compose up
ͨ¹ýÖ´ÐÐÉÏÊöÏÂÁDocker½«»áƾ֤docker-compose.ymlÎļþ×îÏȹ¹½¨ºÍÆô¶¯Ð§ÀÍ¡£
ËÄ¡¢²âÊÔ΢ЧÀͼܹ¹
ÔÚÆô¶¯Î¢Ð§Àͼܹ¹ºó£¬¿ÉÒÔͨ¹ýä¯ÀÀÆ÷»òÕßÀàËƵÄÇëÇ󹤾߶ÔwebЧÀ;ÙÐлá¼ûºÍ²âÊÔ¡£ÔÚÕâ¸öʾÀýÖУ¬webЧÀͽ«¼àÌýÍâµØµÄ8080¶Ë¿Ú¡£
Îå¡¢½áÂÛ
±¾ÎÄÏÈÈÝÁËÔõÑùÔÚLinuxÉÏʹÓÃDocker¹¹½¨ÈÝÆ÷»¯µÄ΢ЧÀͼܹ¹¡£Í¨¹ýDocker£¬ÎÒÃÇ¿ÉÒÔ¿ìËÙ½¨Éè¡¢±àÅźͰ²ÅÅÈÝÆ÷»¯µÄ΢ЧÀÍ¡£ÕâΪ¿ª·¢ÕßÃÇÌṩÁ˸ü¸ßЧ¡¢¸üÎÞаµÄ¿ª·¢ºÍ°²ÅÅ·½·¨¡£Ï£Íû±¾ÎÄÄÜ×ÊÖúµ½¸÷ÈË£¬ÔÚÏÖʵÏîÄ¿ÖÐ˳ËìÓ¦ÓÃÈÝÆ÷»¯µÄ΢ЧÀͼܹ¹¡£
ÒÔÉϾÍÊÇÔõÑùÔÚLinuxÉÏʹÓÃDocker¹¹½¨ÈÝÆ÷»¯µÄ΢ЧÀͼܹ¹£¿µÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡