ÔõÑùʹÓÃDocker¾ÙÐÐÈÝÆ÷µÄ×Ô¶¯»¯°²Åźͻعö
Ëæ×ÅÔÆÅÌËãºÍ΢ЧÀ͵ÄÆÕ¼°£¬Docker×÷ΪһÖÖÇáÁ¿¼¶µÄÐéÄ⻯ÊÖÒÕ±»ÆÕ±éÓ¦ÓÃÓÚÈÝÆ÷»¯°²ÅÅ¡£Docker¾ßÓпìËÙ°²ÅÅ¡¢×ÊÔ´¸ôÀë¡¢Ò×ÓÚÖÎÀíµÈÓŵ㣬Äܹ»´ó´ó¼ò»¯Ó¦ÓõÄά»¤ºÍÐû²¼Á÷³Ì¡£±¾ÎĽ«ÏÈÈÝÔõÑùʹÓÃDocker¾ÙÐÐÈÝÆ÷µÄ×Ô¶¯»¯°²Åźͻعö£¬²¢ÌṩһЩÏêϸµÄ´úÂëʾÀý¡£
Ò»¡¢Docker×Ô¶¯»¯°²ÅÅ
Docker×Ô¶¯»¯°²ÅÅÊÇÖ¸ÔÚÓ¦ÓõĿª·¢ºÍ²âÊÔÀú³ÌÖУ¬Í¨¹ý±àдDockerfileÎļþºÍDocker ComposeÎļþ£¬½«Ó¦ÓõÄÇéÐκÍÏà¹ØÒÀÀµ´ò°ü³ÉDocker¾µÏñ£¬²¢×Ô¶¯»¯µØ°²Åŵ½ÍâµØ»òÔ¶³ÌЧÀÍÆ÷ÉÏ¡£
1.±àдDockerfileÎļþ
DockerfileÊÇÒ»¸öÎı¾Îļþ£¬°üÀ¨ÁËÓ¦Óù¹½¨¾µÏñËùÐèµÄËùÓÐÖ¸ÁîºÍÏÂÁî¡£¿Éͨ¹ý±àдDockerfileÎļþ£¬½ç˵ӦÓÃËùÐèµÄÇéÐΡ¢ÒÀÀµºÍÆô¶¯ÏÂÁֻÐèÒªÖ´ÐÐÒ»ÌõÏÂÁ¼´¿É×Ô¶¯¹¹½¨³öÓ¦ÓÃËùÐèµÄDocker¾µÏñ¡£
ÏÂÃæÊÇÒ»¸öʹÓÃDockerfileÎļþ¹¹½¨Node.jsÓ¦ÓõÄʾÀý£º
FROM node:14-alpine WORKDIR /app COPY package*.json ./ RUN npm install COPY . . EXPOSE 3000 CMD ["npm", "start"]
µÇ¼ºó¸´ÖÆ
ÉÏÊöDockerfileÎļþÖ¸¶¨ÁË»ù´¡¾µÏñΪNode.js 14°æ±¾µÄalpine»ù´¡¾µÏñ£¬½ç˵ÁËÊÂÇéĿ¼Ϊ/app£¬½«package.jsonºÍpackage-lock.json¿½±´µ½ÊÂÇéĿ¼£¬Ö´ÐÐnpm install×°ÖÃÒÀÀµ£¬½«Ó¦ÓôúÂ븴ÖƵ½ÊÂÇéĿ¼£¬½ç˵ÁËÈÝÆ÷ÐèÒªÓ³ÉäµÄ¶Ë¿ÚΪ3000£¬×îºóÖ´ÐÐnpm startÆô¶¯Ó¦Óá£
2.±àдDocker ComposeÎļþ
Docker ComposeÊÇDocker¹Ù·½ÌṩµÄÒ»¸ö¹¤¾ß£¬ÓÃÓÚ½ç˵ºÍÔËÐжà¸öÈÝÆ÷×é³ÉµÄÓ¦Óá£Í¨¹ý±àдDocker ComposeÎļþ£¬¿ÉÒÔ½ç˵ӦÓÃËùÐèµÄЧÀÍ¡¢ÈÝÆ÷µÄ¾µÏñºÍÆô¶¯·½·¨¡¢ÈÝÆ÷Ö®¼äµÄÍøÂçºÍÒÀÀµ¹ØϵµÈÐÅÏ¢£¬ÒÔ¼°ÈÝÆ÷µÄ×ÊÔ´ÏÞÖƺÍÀ©Õ¹¡£
ÏÂÃæÊÇÒ»¸öʹÓÃDocker Compose°²ÅÅNode.jsÓ¦ÓõÄʾÀý£º
version: '3' services: app: build: . ports: - '8080:3000' environment: NODE_ENV: production restart: always
µÇ¼ºó¸´ÖÆ
ÉÏÊöDocker ComposeÎļþÖнç˵ÁËÒ»¸öÃûΪappµÄЧÀÍ£¬Ê¹ÓÃbuildÖ¸Áî¹¹½¨¾µÏñ£¬½«ÈÝÆ÷¶Ë¿ÚÓ³Éäµ½Ö÷»ú¶Ë¿Ú8080£¬½ç˵ÁËÇéÐαäÁ¿NODE_ENVΪproduction£¬Ö¸¶¨ÈÝÆ÷å´»úºó×Ô¶¯ÖØÆô¡£
3.×Ô¶¯»¯°²Åž籾
ΪÁËÈÃÓ¦ÓõݲÅÅÁ÷³ÌÔ½·¢×Ô¶¯»¯£¬¿ÉÒÔ±àдһЩ°²Åž籾£¬ÊµÏÖ×Ô¶¯¹¹½¨¡¢ÍÆËͺͰ²ÅÅDocker¾µÏñµÄÕû¸öÀú³Ì¡£ÏÂÃæÊÇÒ»¸öʹÓÃDocker Compose×Ô¶¯°²ÅÅNode.jsÓ¦ÓõÄʾÀý£º
#!/bin/bash # ½ç˵ӦÓÃÃû³ÆºÍ°æ±¾ºÅ APP_NAME='nodejs-app' TAG=$(git rev-parse --short HEAD) # ¹¹½¨Docker¾µÏñ docker build -t $APP_NAME:$TAG . # ÍÆËÍDocker¾µÏñµ½¿ÍÕ» docker push $APP_NAME:$TAG # ʹÓÃDocker Compose°²ÅÅÓ¦Óà docker-compose up -d
µÇ¼ºó¸´ÖÆ
ÉÏÊö¾ç±¾ÖУ¬Ê¹ÓÃgit rev-parseÖ¸Áî»ñÈ¡×îдúÂëµÄ°æ±¾ºÅ£¬¹¹½¨¾µÏñ²¢ÍÆË͵½Docker¿ÍÕ»£¬×îºóʹÓÃDocker ComposeÆô¶¯Ó¦Óã¨-d²ÎÊýÌåÏÖºǫ́ÔËÐУ©¡£
¶þ¡¢DockerÈÝÆ÷»Ø¹ö
ÔÚÓ¦ÓõĿª·¢ºÍ²âÊÔÀú³ÌÖУ¬ÄÑÃâ»á·ºÆðһЩ´úÂë¹ýʧ»òÕß²»¼æÈݵÄÎÊÌâ¡£Õâʱ¼ä£¬ÎÒÃÇÐèÒª»Ø¹öÓ¦Óõİ汾£¬½«Ó¦Óûָ´µ½Ö®Ç°µÄ״̬¡£
DockerÈÝÆ÷ÌṩÁËÒ»ÖÖ¼òÆӵĻعö·½·¨£¬¼´Í¨¹ý±êÇ©£¨Tag£©À´Ö¸¶¨°æ±¾ºÅ£¬¿ÉÒÔÔÚÈÝÆ÷Æô¶¯Ê±Ñ¡Ôñ²î±ðµÄ¾µÏñ°æ±¾£¬ÊµÏÖÓ¦Óð汾µÄ»Ø¹ö¡£
1.ʹÓñêÇ©£¨Tag£©
ÔÚDockerfileÎļþÖÐÖ¸¶¨°æ±¾ºÅʱ£¬¿ÉÒÔʹÓñêÇ©£¨Tag£©À´±êʶӦÓõİ汾¡£ÀýÈ磺
FROM node:14-alpine LABEL version="1.0.0" WORKDIR /app COPY package*.json ./ RUN npm install COPY . . EXPOSE 3000 CMD ["npm", "start"]
µÇ¼ºó¸´ÖÆ
ÉÏÊöDockerfileÎļþÖÐʹÓÃLABELÖ¸Áî±êʶӦÓð汾ºÅΪ1.0.0¡£
¹¹½¨Íê¾µÏñºó£¬¿ÉÒÔʹÓÃdocker tagÖ¸ÁÉèÒ»¸öеıêÇ©£¬ÀýÈ磺
docker tag nodejs-app:latest nodejs-app:1.0.0
µÇ¼ºó¸´ÖÆ
ÔÚDocker ComposeÎļþÖнç˵ЧÀÍʱ£¬¿ÉÒÔÖ¸¶¨ÐèҪʹÓõıêÇ©£¨Tag£©£¬ÀýÈ磺
version: '3' services: app: image: nodejs-app:1.0.0 ports: - '8080:3000' environment: NODE_ENV: production restart: always
µÇ¼ºó¸´ÖÆ
2.»Ø¹ö¾ç±¾
ΪÁËÀû±ã¾ÙÐа汾»Ø¹ö£¬¿ÉÒÔ±àдһ¸ö»Ø¹ö¾ç±¾£¬Í¨¹ýÖ¸¶¨ÐèҪʹÓõľµÏñ°æ±¾£¬ÊµÏÖÓ¦Óð汾µÄ»Ø¹ö¡£ÏÂÃæÊÇÒ»¸öʹÓÃDocker Compose»Ø¹öNode.jsÓ¦ÓõÄʾÀý£º
#!/bin/bash # ½ç˵ӦÓÃÃû³ÆºÍ°æ±¾ºÅ APP_NAME='nodejs-app' TAG='1.0.0' # ¸üÐÂDocker ComposeÎļþÖÐʹÓõľµÏñ°æ±¾ sed -i "s|$APP_NAME:.*|$APP_NAME:$TAG|g" docker-compose.yml # »Ø¹öÓ¦Óð汾 docker-compose up -d
µÇ¼ºó¸´ÖÆ
ÉÏÊö¾ç±¾ÖУ¬Ö±½ÓÐÞ¸ÄDocker ComposeÎļþÖÐʹÓõľµÏñ°æ±¾ºÅ£¬²¢Æô¶¯ÈÝÆ÷¼´¿É¡£
Èý¡¢×ܽá
±¾ÎÄÏÈÈÝÁËÔõÑùʹÓÃDocker¾ÙÐÐÈÝÆ÷µÄ×Ô¶¯»¯°²Åźͻعö£¬²¢ÌṩÁËһЩÏêϸµÄ´úÂëʾÀý¡£Í¨¹ý×Ô¶¯»¯°²ÅÅ£¬ÎÒÃÇ¿ÉÒÔ¿ìËٵذ²Åź͸üÐÂÓ¦Ó㬲¢ÔÚÐèҪʱ¾ÙÐа汾»Ø¹ö£¬Ìá¸ßÓ¦ÓõÄÎȹÌÐԺͿɿ¿ÐÔ¡£¿ÉÊÇ£¬ÔÚÏÖʵӦÓÃÖУ¬»¹ÐèҪƾ֤ÏêϸµÄÓªÒµÐèÇóºÍÇéÐÎÀ´ÓÅ»¯°²ÅÅÕ½ÂÔ£¬°ü¹ÜÓ¦ÓõÄÕý³£ÔËÐС£
ÒÔÉϾÍÊÇÔõÑùʹÓÃDocker¾ÙÐÐÈÝÆ÷µÄ×Ô¶¯»¯°²ÅźͻعöµÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡