ÔõÑùÔÚLinuxÉϴÈÝÆ÷»¯µÄ¸ßÐÔÄÜWebЧÀÍÆ÷£¿
ÔõÑùÔÚlinuxÉϴÈÝÆ÷»¯µÄ¸ßÐÔÄÜwebЧÀÍÆ÷£¿
µ¼ÓËæ×ÅÔÆÅÌËãµÄ¿ìËÙÉú³¤£¬ÈÝÆ÷»¯ÊÖÒÕ³ÉΪÁËÏÖ´úÓ¦Óð²ÅŵÄÈÈÃÅÑ¡Ôñ¡£ÔÚLinuxÉϴÈÝÆ÷»¯µÄ¸ßÐÔÄÜWebЧÀÍÆ÷£¬²»µ«ÄÜÌá¸ßÓ¦ÓõĿÉÉìËõÐԺͿɿ¿ÐÔ£¬»¹ÄܽÚÔ¼Ó²¼þ×ÊÔ´µÄʹÓ㬱¾ÎĽ«Ïò¸÷ÈËÏÈÈÝÔõÑùʹÓÃDockerºÍNginx´î½¨ÈÝÆ÷»¯µÄ¸ßÐÔÄÜWebЧÀÍÆ÷¡£
Ò»¡¢×°ÖÃDocker
ÔÚLinuxÉϴÈÝÆ÷»¯µÄWebЧÀÍÆ÷£¬Ê×ÏÈÐèҪװÖÃDocker¡£ÔÚÖÕ¶ËÖÐÖ´ÐÐÒÔÏÂÏÂÁÒÔ×°ÖÃDocker£º
sudo apt-get update sudo apt-get install docker.io
µÇ¼ºó¸´ÖÆ
×°ÖÃÍê±Ïºó£¬¿ÉÒÔÖ´ÐÐÒÔÏÂÏÂÁî¼ì²éDockerÊÇ·ñ×°ÖÃÀֳɣº
docker --version
µÇ¼ºó¸´ÖÆ
¶þ¡¢ÖÆ×÷Docker¾µÏñ
½¨ÉèĿ¼ºÍÎļþ
½¨ÉèÒ»¸öеÄĿ¼£¬ÓÃÓÚ´æ·ÅÎÒÃǵÄÓ¦ÓÃÎļþ¡£ÔÚÖÕ¶ËÖÐÖ´ÐÐÒÔÏÂÏÂÁ
mkdir myapp cd myapp
µÇ¼ºó¸´ÖÆ
ÔÚmyappĿ¼Ï½¨ÉèÒ»¸öÃûΪDockerfileµÄÎļþ£¬²¢Ê¹ÓÃÎı¾±à¼Æ÷·¿ª¸ÃÎļþ£º
touch Dockerfile
µÇ¼ºó¸´ÖÆ
±àдDockerfile
ÔÚDockerfileÖÐдÈëÒÔÏÂÄÚÈÝ£º
# ʹÓûù´¡¾µÏñ FROM ubuntu:latest # ¸üÐÂÈí¼þÔ´ RUN apt-get update # ×°ÖÃNginx RUN apt-get install nginx -y # ¸´ÖÆÉèÖÃÎļþµ½¾µÏñ COPY nginx.conf /etc/nginx/nginx.conf # ¸´ÖÆÓ¦ÓÃÎļþµ½¾µÏñ COPY app /var/www/html # ̻¶¶Ë¿Ú EXPOSE 80 # Æô¶¯NginxЧÀÍ CMD ["nginx", "-g", "daemon off;"]
µÇ¼ºó¸´ÖÆ
±àдÉèÖÃÎļþºÍÓ¦ÓÃÎļþ
ÔÚÉÏÒ»²½½¨ÉèµÄmyappĿ¼Ï£¬ÔÙ½¨ÉèÁ½¸öÎļþ£¬Ò»¸öÃûΪnginx.conf£¬Ò»¸öΪapp¡£
ÔÚnginx.confÖÐдÈëÒÔÏÂÄÚÈÝ£º
worker_processes auto; pid /run/nginx.pid; events { worker_connections 1024; } http { server { listen 80; server_name localhost; root /var/www/html; index index.html; location / { try_files $uri $uri/ =404; } } }
µÇ¼ºó¸´ÖÆ
ÔÚappÖÐдÈëÒ»¸ö¼òÆÓµÄHTMLÎļþ£¬ÀýÈçindex.html¡£
¹¹½¨Docker¾µÏñ
»Øµ½Öնˣ¬Ö´ÐÐÒÔÏÂÏÂÁîÀ´¹¹½¨Docker¾µÏñ£º
docker build -t myapp .
µÇ¼ºó¸´ÖÆ
ÆäÖУ¬.ÌåÏÖDockerfileËùÔÚµÄĿ¼£¬myappÊÇÎÒÃÇΪ¾µÏñÈ¡µÄÃû×Ö¡£
Èý¡¢ÔËÐÐÈÝÆ÷»¯µÄWebЧÀÍÆ÷
ÔÚDocker¾µÏñ¹¹½¨Íê±Ïºó£¬¾Í¿ÉÒÔÔËÐÐÈÝÆ÷»¯µÄWebЧÀÍÆ÷ÁË¡£Ö´ÐÐÒÔÏÂÏÂÁ
docker run -d -p 80:80 myapp
µÇ¼ºó¸´ÖÆ
ÆäÖУ¬-dÌåÏÖÒÔºǫ́ģʽÔËÐÐÈÝÆ÷£¬-p 80:80ÌåÏÖ½«Ö÷»úµÄ80¶Ë¿ÚÓ³Éäµ½ÈÝÆ÷µÄ80¶Ë¿Ú£¬myappÊÇÎÒÃÇ֮ǰ¹¹½¨µÄ¾µÏñÃû×Ö¡£
ÖÁ´Ë£¬ÎÒÃǾÍÀÖ³ÉÔÚLinuxÉϴÁËÒ»¸öÈÝÆ÷»¯µÄ¸ßÐÔÄÜWebЧÀÍÆ÷¡£¿ÉÒÔͨ¹ý»á¼ûÖ÷»úµÄIPµØµã»òÕßÓòÃû£¬À´»á¼ûÕâ¸öЧÀÍÆ÷ÉϵÄÓ¦Óá£
½áÓ
±¾ÎÄÏò¸÷ÈËÏÈÈÝÁËÔõÑùʹÓÃDockerºÍNginx´î½¨ÈÝÆ÷»¯µÄ¸ßÐÔÄÜWebЧÀÍÆ÷¡£Í¨¹ýÈÝÆ÷»¯µÄ·½·¨£¬ÎÒÃÇÄܹ»Ô½·¢Àû±ãµØ°²ÅźÍÖÎÀíÓ¦ÓóÌÐò£¬Ìá¸ßÓ¦ÓõĿÉÉìËõÐԺͿɿ¿ÐÔ¡£Ï£Íû±¾ÎÄÄܶԸ÷ÈËÔÚLinuxÉϴÈÝÆ÷»¯µÄWebЧÀÍÆ÷ÓÐËù×ÊÖú¡£
ÒÔÉϾÍÊÇÔõÑùÔÚLinuxÉϴÈÝÆ÷»¯µÄ¸ßÐÔÄÜWebЧÀÍÆ÷£¿µÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡