ÔõÑù¾ÙÐÐDocker¾µÏñµÄ¹¹½¨ºÍÖÎÀí
ÔõÑù¾ÙÐÐDocker¾µÏñµÄ¹¹½¨ºÍÖÎÀí
DockerÊÇÒ»ÖÖ¿ªÔ´µÄÈÝÆ÷»¯Æ½Ì¨£¬¿ÉÒÔ×ÊÖú¿ª·¢Õß½«Ó¦ÓóÌÐòºÍÆäÒÀÀµÏî´ò°ü³ÉÒ»¸ö×ÔÁ¦µÄ¾µÏñ£¬²¢ÄÜÔÚÈκεط½ÔËÐС£±¾ÎĽ«ÏÈÈÝÔõÑùʹÓÃDocker¹¹½¨ºÍÖÎÀí¾µÏñ£¬Í¬Ê±ÌṩһЩÏêϸµÄ´úÂëʾÀý¡£
Ò»¡¢×°ÖÃDocker
Ê×ÏÈ£¬ÐèÒªÔÚÍâµØ»úеÉÏ×°ÖÃDocker¡£DockerÌṩÁ˲î±ðµÄ°æ±¾£¬¿ÉÒÔƾ֤×Ô¼ºµÄ²Ù×÷ϵͳÏÂÔØÊʺϵİ汾²¢¾ÙÐÐ×°Öá£
¶þ¡¢±àдDockerfile
Docker¾µÏñµÄ¹¹½¨ÊÇͨ¹ýDockerfileÎļþÀ´Íê³ÉµÄ¡£DockerfileÊÇÒ»¸ö°üÀ¨Á˹¹½¨Ö¸ÁîµÄÎı¾Îļþ£¬Í¨¹ýÕâЩָÁî¸æËßDockerÔõÑù¹¹½¨¾µÏñ¡£
ÏêϸµÄDockerfileÄÚÈÝ»áƾ֤²î±ðµÄÓ¦ÓóÌÐòºÍÇéÐζøÓÐËù²î±ð£¬ÏÂÃæÊÇÒ»¸ö¼òÆÓµÄʾÀý:
# ʹÓùٷ½µÄNode.js»ù´¡¾µÏñ×÷Ϊ»ù´¡ FROM node:12 # ÉèÖÃÊÂÇéĿ¼ WORKDIR /app # ½«Ä¿½ñĿ¼ÏµÄËùÓÐÎļþ¸´ÖƵ½ÊÂÇéĿ¼ÖÐ COPY . . # ×°ÖÃÓ¦ÓóÌÐòµÄÒÀÀµÏî RUN npm install # ̻¶ӦÓóÌÐòµÄ¶Ë¿Ú EXPOSE 3000 # ½ç˵ĬÈϵÄÆô¶¯ÏÂÁî CMD ["npm", "start"]
µÇ¼ºó¸´ÖÆ
Èý¡¢¹¹½¨¾µÏñ
ÔÚDockerfileËùÔÚµÄĿ¼Ï£¬Ê¹ÓÃÒÔÏÂÏÂÁî¹¹½¨¾µÏñ:
docker build -t myapp .
µÇ¼ºó¸´ÖÆ
ÆäÖУ¬-t²ÎÊýÓÃÓÚÖ¸¶¨¾µÏñµÄ±êÇ©£¬.ÌåÏÖʹÓÃÄ¿½ñĿ¼ÏµÄDockerfile¾ÙÐй¹½¨¡£
ËÄ¡¢ÖÎÀí¾µÏñ
Éó²é¾µÏñÁбí:
docker images
µÇ¼ºó¸´ÖÆ
Õâ¸öÏÂÁ»áÁгöÍâµØËùÓеľµÏñ¡£
ÔËÐÐÈÝÆ÷:
docker run -p 3000:3000 myapp
µÇ¼ºó¸´ÖÆ
Õâ¸öÏÂÁ»áÔÚÍâµØÆô¶¯Ò»¸öеÄÈÝÆ÷£¬²¢½«ÈÝÆ÷µÄ3000¶Ë¿ÚÓ³Éäµ½Ö÷»úµÄ3000¶Ë¿Ú¡£
×èÖ¹ÈÝÆ÷:
docker stop <CONTAINER_ID>
µÇ¼ºó¸´ÖÆ
Õâ¸öÏÂÁ»á×èÖ¹Ö¸¶¨µÄÈÝÆ÷£¬Äã¿ÉÒÔͨ¹ýdocker psÏÂÁîÉó²éÈÝÆ÷µÄID¡£
ɾ³ý¾µÏñ:
docker rmi <IMAGE_ID>
µÇ¼ºó¸´ÖÆ
Õâ¸öÏÂÁ»áɾ³ýÖ¸¶¨µÄ¾µÏñ£¬Äã¿ÉÒÔͨ¹ýdocker imagesÉó²é¾µÏñµÄID¡£
Î塢ʹÓÃDocker¾µÏñ
¹¹½¨ºÍÖÎÀí¾µÏñÖ®ºó£¬ÎÒÃÇ¿ÉÒÔ½«¾µÏñ°²Åŵ½ÆäËû»úе»òÕß¹²Ïí¸øÆäËû¿ª·¢ÕßʹÓá£ÆäËû»úе¿ÉÒÔʹÓÃÒÔÏÂÏÂÁî»ñÈ¡¾µÏñ²¢ÔËÐÐÈÝÆ÷:
docker pull myrepo/myapp docker run -p 3000:3000 myrepo/myapp
µÇ¼ºó¸´ÖÆ
ÉÏÊö´úÂëÖУ¬myrepoÊÇÄãµÄ¾µÏñ¿ÍÕ»Ãû³Æ£¬myappÊǾµÏñµÄÃû³Æ¡£
×ܽá
±¾ÎļòÒªÏÈÈÝÁËÔõÑùʹÓÃDocker¹¹½¨ºÍÖÎÀí¾µÏñ£¬Í¬Ê±ÌṩÁËһЩÏêϸµÄ´úÂëʾÀý¡£Í¨¹ýʹÓÃDocker£¬¿ª·¢Õß¿ÉÒÔÀû±ãµØ´ò°üÓ¦ÓóÌÐòºÍÆäÒÀÀµÏ²¢¿ìËÙ°²Åź͹²Ïí¾µÏñ¡£¹ØÓÚʹÓÃDockerµÄ³õѧÕßÀ´Ëµ£¬ÕâЩ»ù´¡ÖªÊ¶ÊǺÜÊÇÖ÷ÒªµÄ£¬Ï£ÍûÄܶԸ÷ÈËÓÐËù×ÊÖú¡£
ÒÔÉϾÍÊÇÔõÑù¾ÙÐÐDocker¾µÏñµÄ¹¹½¨ºÍÖÎÀíµÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡