LinuxЧÀÍÆ÷ÖÎÀí£ºÔõÑùʹÓÃDocker¾ÙÐпìËÙ°²ÅźÍÀ©Õ¹£¿
linuxЧÀÍÆ÷ÖÎÀí£ºÔõÑùʹÓÃdocker¾ÙÐпìËÙ°²ÅźÍÀ©Õ¹£¿
СÐò£º
Ëæ×ÅÔÆÅÌËãºÍÈÝÆ÷»¯ÊÖÒÕµÄÉú³¤£¬Docker×÷ΪһÖÖÇáÁ¿¼¶µÄÐéÄ⻯¹¤¾ß£¬ÒѾ³ÉΪÐí¶à¿ª·¢ÕߺÍÔËάְԱµÄÊ×Ñ¡¡£±¾ÎĽ«ÏÈÈÝÔõÑùʹÓÃDockerÔÚLinuxЧÀÍÆ÷ÉϾÙÐпìËÙ°²ÅźÍÀ©Õ¹£¬ÒÔÌá¸ßÓ¦ÓóÌÐòµÄÔËÐÐЧÂʺͿÉÀ©Õ¹ÐÔ¡£
×°ÖÃDocker
ÔÚ×îÏÈ֮ǰ£¬ÎÒÃÇÊ×ÏÈÐèÒªÔÚLinuxЧÀÍÆ÷ÉÏ×°ÖÃDocker¡£Çëƾ֤ÒÔÏ°취¾ÙÐÐ×°Öãº
°ì·¨Ò»£º¸üÐÂЧÀÍÆ÷Èí¼þ°ü
$ sudo apt-get update
µÇ¼ºó¸´ÖÆ
°ì·¨¶þ£º×°ÖÃDockerÒÀÀµ
$ sudo apt-get install apt-transport-https ca-certificates curl software-properties-common
µÇ¼ºó¸´ÖÆ
°ì·¨Èý£ºÌí¼ÓDocker¹Ù·½GPGÃÜÔ¿
$ curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -
µÇ¼ºó¸´ÖÆ
°ì·¨ËÄ£ºÌí¼ÓDocker´æ´¢¿â
$ sudo add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable"
µÇ¼ºó¸´ÖÆ
°ì·¨Î壺¸üÐÂÈí¼þ°üË÷Òý²¢×°ÖÃDocker
$ sudo apt-get update $ sudo apt-get install docker-ce
µÇ¼ºó¸´ÖÆ
±àдDockerfile
ÔÚʹÓÃDocker֮ǰ£¬ÎÒÃÇÐèÒª±àдһ¸öDockerfile£¬ÓÃÓÚ½ç˵ÔõÑù¹¹½¨Docker¾µÏñ¡£ÏÂÃæÊÇÒ»¸öʾÀýµÄDockerfile£º
# ʹÓûù´¡¾µÏñ FROM ubuntu:latest # ÉèÖÃ×÷ÕßÐÅÏ¢ MAINTAINER Your Name <your@email.com> # ×°ÖÃÏà¹ØÒÀÀµ RUN apt-get update && apt-get install -y python3 python3-pip # ÉèÖÃÊÂÇéĿ¼ WORKDIR /app # ½«Ó¦ÓóÌÐòÌí¼Óµ½¾µÏñÖÐ COPY . /app # ×°ÖÃÓ¦ÓóÌÐòÒÀÀµ RUN pip3 install -r requirements.txt # ÉèÖÃÈÝÆ÷Æô¶¯ÏÂÁî CMD ["python3", "app.py"]
µÇ¼ºó¸´ÖÆ
ÔÚÉÏÊöʾÀýÖУ¬ÎÒÃÇʹÓÃÁËÒ»¸ö»ùÓÚ×îеÄUbuntu¾µÏñ×÷Ϊ»ù´¡¾µÏñ¡£È»ºó£¬ÎÒÃÇ×°ÖÃÁËPython3ºÍpip3¹¤¾ß£¬²¢½«Ó¦ÓóÌÐòÎļþ¼Ð¸´ÖƵ½¾µÏñÖС£½ÓÏÂÀ´£¬ÎÒÃÇ×°ÖÃÓ¦ÓóÌÐòµÄÒÀÀµ£¬²¢ÉèÖÃÈÝÆ÷Æô¶¯ÏÂÁîΪÔËÐÐapp.pyÎļþ¡£
¹¹½¨¾µÏñ
Íê³ÉDockerfileµÄ±àдºó£¬ÎÒÃÇ¿ÉÒÔʹÓÃÒÔÏÂÏÂÁî¹¹½¨Docker¾µÏñ£º
$ docker build -t myapp:latest .
µÇ¼ºó¸´ÖÆ
ÉÏÊöÏÂÁƾ֤DockerfileÖеĽç˵£¬¹¹½¨Ò»¸öÃûΪmyappµÄ¾µÏñ£¬²¢ÇÒ±ê¼ÇΪ×îа汾¡£
ÔËÐÐÈÝÆ÷
¹¹½¨¾µÏñÍê³Éºó£¬ÎÒÃÇ¿ÉÒÔʹÓÃÒÔÏÂÏÂÁîÀ´ÔËÐÐÈÝÆ÷£º
$ docker run -d -p 80:5000 myapp:latest
µÇ¼ºó¸´ÖÆ
ÉÏÃæµÄÏÂÁÆô¶¯Ò»¸öÒÔºǫ́ģʽÔËÐеÄÈÝÆ÷£¬²¢½«Ö÷»úµÄ80¶Ë¿ÚÓ³Éäµ½ÈÝÆ÷µÄ5000¶Ë¿Ú¡£ÕâÑù£¬ÎÒÃǾͿÉÒÔͨ¹ýä¯ÀÀÆ÷»á¼ûÖ÷»úµÄ80¶Ë¿ÚÀ´»á¼ûÓ¦ÓóÌÐò¡£
À©Õ¹Ó¦ÓóÌÐò
ʹÓÃDocker¿ÉÒÔÇáËɵØÀ©Õ¹Ó¦ÓóÌÐò£¬¿ÉÒÔͨ¹ý¶à¸öÈÝÆ÷ʵÀýÀ´Ìá¸ßϵͳµÄ¿ÉÓÃÐÔºÍÍÌÍÂÁ¿¡£ÏÂÃæÊÇÒ»¸ö¼òÆÓµÄʵÏÖʾÀý£º
Ê×ÏÈ£¬ÎÒÃÇÐèҪʹÓÃDocker ComposeÀ´½ç˵ӦÓóÌÐòµÄÕû¸ö¼Ü¹¹¡£½¨ÉèÒ»¸öÃûΪdocker-compose.ymlµÄÎļþ£¬²¢Ìí¼ÓÒÔÏÂÄÚÈÝ£º
version: '3' services: app: build: context: . dockerfile: Dockerfile image: myapp:latest ports: - "80:5000" load_balancer: image: nginx:latest ports: - "80:80" volumes: - ./nginx.conf:/etc/nginx/conf.d/default.conf:ro depends_on: - app
µÇ¼ºó¸´ÖÆ
ÔÚÉÏÊöʾÀýÖУ¬ÎÒÃǽç˵ÁËÁ½¸öЧÀÍ£ºappºÍload_balancer¡£appЧÀͽ«Æ¾Ö¤Ä¿½ñĿ¼ÏµÄDockerfile¹¹½¨¾µÏñ£¬²¢½«ÈÝÆ÷µÄ5000¶Ë¿ÚÓ³Éäµ½Ö÷»úµÄ80¶Ë¿Ú¡£load_balancerЧÀͽ«Ê¹ÓÃNginx¾µÏñ£¬²¢½«Ö÷»úµÄ80¶Ë¿ÚÓ³Éäµ½ÈÝÆ÷µÄ80¶Ë¿Ú¡£
½ÓÏÂÀ´£¬ÎÒÃÇÐèÒª½¨ÉèÒ»¸öÃûΪnginx.confµÄÉèÖÃÎļþ£¬²¢Ìí¼ÓÒÔÏÂÄÚÈÝ£º
upstream app_servers { server app:5000; } server { listen 80; location / { proxy_pass http://app_servers; } }
µÇ¼ºó¸´ÖÆ
ÒÔÉÏÉèÖÃÎļþ½ç˵ÁËÒ»¸öÃûΪapp_serversµÄupstream£¬²¢Ê¹ÓÃproxy_passÀ´½«ÇëÇóת·¢µ½appЧÀÍ¡£
×îºó£¬Ê¹ÓÃÒÔÏÂÏÂÁîÀ´ÔËÐÐÓ¦ÓóÌÐòµÄ¶à¸öʵÀý£º
$ docker-compose up --scale app=3
µÇ¼ºó¸´ÖÆ
ÉÏÊöÏÂÁÔËÐÐ3¸öappÈÝÆ÷ʵÀý£¬²¢Ê¹ÓÃNginx×÷Ϊ¸ºÔØƽºâÆ÷¾ÙÐÐÁ÷Á¿·Ö·¢¡£
½áÂÛ£º
ͨ¹ýʹÓÃDocker£¬ÎÒÃÇ¿ÉÒÔÇáËɵؾÙÐпìËÙ°²ÅźÍÀ©Õ¹Ó¦ÓóÌÐò¡£Í¨¹ý±àдDockerfileÀ´½ç˵¾µÏñ¹¹½¨°ì·¨£¬Ê¹ÓÃdocker runÏÂÁîÀ´ÔËÐÐÈÝÆ÷£¬ÒÔ¼°Ê¹ÓÃDocker Compose¾ÙÐжàÈÝÆ÷ÖÎÀí£¬ÎÒÃÇ¿ÉÒÔÔ½·¢¸ßЧµØÖÎÀíºÍÀ©Õ¹Ó¦ÓóÌÐò¡£Ï£Íû±¾ÎÄÄܶÔÄãÔÚLinuxЧÀÍÆ÷ÉÏʹÓÃDocker¾ÙÐа²ÅźÍÀ©Õ¹Ìṩ×ÊÖú¡£
ÒÔÉϾÍÊÇLinuxЧÀÍÆ÷ÖÎÀí£ºÔõÑùʹÓÃDocker¾ÙÐпìËÙ°²ÅźÍÀ©Õ¹£¿µÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡