Docker¿ìËÙ×°ÖÃSymfony¿ò¼ÜµÄ°ì·¨ÓëÖ¸ÄÏ
Docker¿ìËÙ×°ÖÃSymfony¿ò¼ÜµÄ°ì·¨ÓëÖ¸ÄÏ
SymfonyÊÇÒ»¿îÊ¢ÐеÄPHP¿ò¼Ü£¬±»ÆÕ±éÓ¦ÓÃÓÚ¿ª·¢Ã÷´úµÄWebÓ¦ÓóÌÐò¡£¹ØÓÚ¿ª·¢Õ߶øÑÔ£¬¿ìËÙ°²ÅźÍÉèÖÃSymfonyÇéÐÎÊÇÒ»¸öÖ÷ÒªµÄʹÃü¡£Ê¹ÓÃDockerÀ´×°ÖÃSymfony¿ÉÒÔ´ó´ó¼ò»¯Õâ¸öÀú³Ì£¬±¾ÎĽ«ÎªÄúÌṩһ·ÝÏêϸµÄ°ì·¨ÓëÖ¸ÄÏ¡£
×°ÖÃDocker
Ê×ÏÈ£¬È·±£ÄúÒѾÔÚÍâµØ»úеÉÏ×°ÖÃÁËDocker¡£ÈôÊÇ»¹Ã»ÓÐ×°Öã¬Äú¿ÉÒÔ»á¼ûDocker¹Ù·½ÍøÕ¾(https://www.docker.com/)£¬Æ¾Ö¤ÄúµÄ²Ù×÷ϵͳÏÂÔز¢×°ÖÃ×îа汾µÄDocker¡£
½¨ÉèDocker¾µÏñ
Symfony¿ò¼ÜÐèÒªPHPºÍWebЧÀÍÆ÷£¨ÈçNginx»òApache£©µÄÖ§³Ö¡£ÎªÁ˽¨ÉèÒ»¸ö°üÀ¨ÕâЩ×é¼þµÄDocker¾µÏñ£¬Äú¿ÉÒÔʹÓÃÒ»¸ö»ù´¡¾µÏñÀ´¹¹½¨¡£
½¨ÉèÒ»¸öÃûΪDockerfileµÄÎļþ£¬²¢ÔÚÆäÖнç˵ÄúµÄ¾µÏñ£º
# ʹÓùٷ½µÄPHP¾µÏñ×÷Ϊ»ù´¡ FROM php:7.4-fpm # ×°ÖÃSymfonyËùÐèµÄÒÀÀµ°ü RUN apt-get update && apt-get install -y git libicu-dev zip unzip # ×°ÖÃSymfonyÏÂÁîÐй¤¾ß RUN curl -sS https://get.symfony.com/cli/installer | bash && mv /root/.symfony/bin/symfony /usr/local/bin/ # ×°ÖÃPHPÀ©Õ¹ RUN docker-php-ext-configure intl && docker-php-ext-install intl # ÉèÖÃÊÂÇéĿ¼ WORKDIR /var/www/symfony # ×°ÖÃComposer COPY --from=composer /usr/bin/composer /usr/bin/composer # ¸´ÖÆSymfonyÏîÄ¿µ½ÊÂÇéĿ¼ COPY . . # ×°ÖÃÒÀÀµ°ü RUN composer install # ̻¶WebЧÀÍÆ÷¶Ë¿Ú EXPOSE 80 # ÔËÐÐSymfonyÓ¦Óà CMD ["symfony", "server:start"]
µÇ¼ºó¸´ÖÆ
ÔÚÕâ¸öDockerfileÖУ¬ÎÒÃÇÊ×ÏÈʹÓùٷ½µÄPHP¾µÏñ×÷Ϊ»ù´¡¡£È»ºó×°ÖÃSymfonyËùÐèµÄÒÀÀµ°ü£¬°üÀ¨ICU¡¢git¡¢zipºÍunzip¡£½ÓÏÂÀ´£¬ÎÒÃÇ×°ÖÃSymfonyÏÂÁîÐй¤¾ßºÍPHPÀ©Õ¹¡£
È»ºó£¬ÎÒÃÇÉèÖÃÊÂÇéĿ¼Ϊ/var/www/symfony£¬²¢½«SymfonyÏîÄ¿¸´ÖƵ½ÊÂÇéĿ¼ÖС£È»ºó£¬ÎÒÃÇʹÓÃComposer×°ÖÃSymfonyÏîÄ¿ËùÐèµÄÒÀÀµ°ü¡£×îºó£¬ÎÒÃÇ̻¶WebЧÀÍÆ÷¶Ë¿Ú²¢ÔËÐÐSymfonyÓ¦ÓóÌÐò¡£
¹¹½¨Docker¾µÏñ
ÔÚÖÕ¶ËÖе¼º½µ½°üÀ¨DockerfileµÄĿ¼£¬²¢ÔËÐÐÒÔÏÂÏÂÁîÀ´¹¹½¨Docker¾µÏñ£º
docker build -t symfony-app .
µÇ¼ºó¸´ÖÆ
Õ⽫»ùÓÚDockerfile¹¹½¨Ò»¸öÃûΪsymfony-appµÄ¾µÏñ¡£È·±£ÔÚ¹¹½¨Àú³ÌÖÐûÓйýʧ±¬·¢¡£
ÔËÐÐSymfonyÓ¦ÓóÌÐò
ÏÖÔÚ£¬Äú¿ÉÒÔʹÓÃÒÔÏÂÏÂÁîÀ´ÔËÐÐSymfonyÓ¦ÓóÌÐò£º
docker run -p 80:80 symfony-app
µÇ¼ºó¸´ÖÆ
Õ⽫ÔÚÍâµØ»úеµÄ80¶Ë¿ÚÉÏÔËÐÐSymfonyÓ¦ÓóÌÐò¡£
»á¼ûSymfonyÓ¦ÓóÌÐò
·¿ªÄúϲ»¶µÄWebä¯ÀÀÆ÷£¬²¢ÔڵصãÀ¸ÖÐÊäÈëhttp://localhost¡£ÈôÊÇÒ»ÇÐ˳Ë죬Äú½«¿´µ½SymfonyµÄ½Ó´ýÒ³Ã棬Åú×¢ÄúÒÑÀÖ³É×°ÖúÍÉèÖÃÁËSymfony¿ò¼Ü¡£
ÏÖÔÚ£¬Äú¿ÉÒÔ×îÏÈ¿ª·¢ºÍ°²ÅÅSymfonyÓ¦ÓóÌÐòÁË¡£Çë¼Ç×Å£¬ÔÚÔËÐÐSymfonyÓ¦ÓóÌÐò֮ǰ£¬ÒªÈ·±£ÄúµÄ¾µÏñºÍÈÝÆ÷ÊÇ×îеġ£Äú¿ÉÒÔʹÓÃÒÔÏÂÏÂÁîÀ´¸üÐÂÍâµØµÄ¾µÏñºÍÈÝÆ÷£º
docker pull symfony-app docker stop <container_id> docker rm <container_id> docker run -p 80:80 symfony-app
µÇ¼ºó¸´ÖÆ
×ܽá
ͨ¹ýʹÓÃDocker×°ÖÃSymfony¿ò¼Ü£¬Äú¿ÉÒÔÇáËɽ¨ÉèºÍÖÎÀíSymfonyÇéÐΡ£Ê¹ÓÃÉÏÊö°ì·¨ºÍÖ¸ÄÏ£¬Äú½«Äܹ»¿ìËٴһ¸ö¿É¿¿µÄSymfony¿ª·¢ÇéÐΣ¬²¢×¨×¢ÓÚ¿ª·¢³ö¾«²ÊµÄWebÓ¦ÓóÌÐò¡£×£ÄúʹÓÃSymfonyÓä¿ì£¡
ÒÔÉϾÍÊÇDocker¿ìËÙ×°ÖÃSymfony¿ò¼ÜµÄ°ì·¨ÓëÖ¸ÄϵÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡