Docker×°ÖÃSymfony£ºÏêϸ½Ì³ÌÓë°ì·¨
Docker×°ÖÃSymfony£ºÏêϸ½Ì³ÌÓë°ì·¨
СÐò£º
SymfonyÊÇÒ»¿îÊ¢ÐеÄPHP WebÓ¦ÓóÌÐò¿ª·¢¿ò¼Ü£¬ËüÌṩÁËÇ¿Ê¢µÄ¹¦Ð§ºÍÎÞаµÄ¼Ü¹¹£¬Ê¹¿ª·¢Ö°Ô±Äܹ»¿ìËÙ¹¹½¨¸ßÖÊÁ¿µÄWebÓ¦Óá£DockerÊÇÒ»ÖÖÇáÁ¿¼¶µÄÈÝÆ÷»¯ÊÖÒÕ£¬ËüÄܹ»ÈÃÎÒÃÇÇáËÉ°²ÅźÍÖÎÀíÓ¦ÓóÌÐò¡£±¾ÎĽ«ÏêϸÏÈÈÝÔõÑùʹÓÃDocker×°ÖÃSymfony£¬²¢ÌṩÏêϸµÄ´úÂëʾÀý¡£
°ì·¨Ò»£º×°ÖÃDockerºÍDocker Compose
Ê×ÏÈ£¬ÎÒÃÇÐèÒªÔÚÍâµØÇéÐÎÖÐ×°ÖÃDockerºÍDocker Compose¡£Äã¿ÉÒÔƾ֤×Ô¼º²Ù×÷ϵͳµÄ°æ±¾ºÍÒªÇ󣬴ÓDocker¹Ù·½ÍøÕ¾ÉÏÏÂÔضÔÓ¦µÄ×°Öðü£¬²¢Æ¾Ö¤¹Ù·½µÄ×°ÖÃÖ¸ÄϾÙÐÐ×°Öá£
°ì·¨¶þ£º½¨ÉèSymfonyÏîÄ¿
ÔÚ×°ÖúÃDockerÖ®ºó£¬ÎÒÃÇ¿ÉÒÔ´ÓDocker HubÉÏÏÂÔØSymfonyµÄ¹Ù·½¾µÏñ£¬¸Ã¾µÏñ°üÀ¨ÁËËùÓÐÎÒÃÇÐèÒªµÄÇéÐκÍÒÀÀµÏî¡£·¿ªÖն˲¢Ö´ÐÐÒÔÏÂÏÂÁîÀ´½¨ÉèSymfonyÏîÄ¿µÄÈÝÆ÷£º
$ docker run -it --rm -v $(pwd):/app -w /app symfony/symfony composer create-project symfony/skeleton my_project
µÇ¼ºó¸´ÖÆ
Õâ¸öÏÂÁî»á½¨ÉèÒ»¸öÃûΪ”my_project”µÄSymfonyÏîÄ¿£¬²¢½«ÆäÉúÑÄÔÚÄ¿½ñĿ¼¡£
°ì·¨Èý£º±àдDockerfile
ÔÚÏîÄ¿¸ùĿ¼Ï½¨ÉèÒ»¸öÃûΪDockerfileµÄÎļþ£¬²¢Ìí¼ÓÒÔÏÂÄÚÈÝ£º
FROM php:7.4-fpm RUN apt-get update && apt-get install -y curl git unzip libpq-dev libzip-dev && docker-php-ext-install pdo pdo_pgsql zip RUN curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer WORKDIR /app COPY . /app RUN composer install --no-scripts --no-interaction EXPOSE 8000 CMD ["php", "-S", "0.0.0.0:8000", "-t", "public"]
µÇ¼ºó¸´ÖÆ
Õâ¸öDockerfileÎļþÐÎòÁËÎÒÃÇÈÝÆ÷µÄ¹¹½¨Àú³Ì¡£Ëü»ùÓÚ¹Ù·½µÄPHP 7.4¾µÏñ£¬²¢ÔÚ´Ë»ù´¡ÉÏ×°ÖÃÁËһЩÐëÒªµÄÒÀÀµÏȻºó£¬Ëü½«Ä¿½ñĿ¼ÖеÄËùÓÐÎļþ¸´ÖƵ½ÈÝÆ÷µÄ/appĿ¼ÖУ¬²¢Ê¹ÓÃcomposer×°ÖÃSymfonyÓ¦ÓóÌÐòµÄÒÀÀµÏî¡£×îºó£¬ÎÒÃǽ«ÈÝÆ÷µÄ8000¶Ë¿Ú̻¶³öÀ´£¬²¢½ç˵ÁËÔËÐÐSymfonyÓ¦ÓóÌÐòµÄÏÂÁî¡£
°ì·¨ËÄ£º±àдdocker-compose.ymlÎļþ
ÔÚÏîÄ¿¸ùĿ¼Ï½¨ÉèÒ»¸öÃûΪdocker-compose.ymlµÄÎļþ£¬²¢Ìí¼ÓÒÔÏÂÄÚÈÝ£º
version: '3' services: web: build: context: . ports: - 8000:8000 volumes: - .:/app
µÇ¼ºó¸´ÖÆ
Õâ¸ödocker-compose.ymlÎļþÐÎòÁËÎÒÃÇÈÝÆ÷µÄ±àÅÅÀú³Ì¡£Ëü½ç˵ÁËÒ»¸öÃûΪ”web”µÄЧÀÍ£¬¸ÃЧÀÍ»ùÓÚÎÒÃÇÔÚ°ì·¨ÈýÖн¨ÉèµÄDockerfile¹¹½¨¡£Ëü½«ÈÝÆ÷µÄ8000¶Ë¿ÚÓ³Éäµ½ÍâµØµÄ8000¶Ë¿Ú£¬²¢½«ÍâµØĿ¼ÓëÈÝÆ÷µÄ/appĿ¼¾ÙÐÐÁ˹ÒÔØ¡£
°ì·¨Î壺Æô¶¯SymfonyÓ¦ÓóÌÐò
ÔÚÖÕ¶ËÖÐÖ´ÐÐÒÔÏÂÏÂÁÆô¶¯SymfonyÓ¦ÓóÌÐòµÄÈÝÆ÷£º
$ docker-compose up -d
µÇ¼ºó¸´ÖÆ
Õâ¸öÏÂÁî»áÆô¶¯Ò»¸öÊØ»¤Àú³ÌģʽµÄÈÝÆ÷£¬Ê¹SymfonyÓ¦ÓóÌÐò¿ÉÒÔÔÚºǫ́ÔËÐС£ÉÔµÈƬÉΣ¬SymfonyÓ¦ÓóÌÐò½«»áÔÚÍâµØµÄ8000¶Ë¿Ú¿É»á¼û¡£
½áÂÛ£º
ͨ¹ýʹÓÃDocker£¬ÎÒÃÇ¿ÉÒÔ¿ìËÙÀû±ãµØ×°ÖúͰ²ÅÅSymfonyÓ¦ÓóÌÐò¡£±¾ÎÄÌṩÁËÏêϸµÄ½Ì³ÌºÍ°ì·¨£¬ÅäºÏÏêϸµÄ´úÂëʾÀý£¬Ï£ÍûÄÜ×ÊÖú¶ÁÕßÇáËɵØʹÓÃDockerÀ´×°ÖÃSymfony£¬²¢ÏíÊÜÓÅÒìµÄ¿ª·¢ÌåÑé¡£ÈÃÎÒÃÇÒ»Æð̽Ë÷DockerºÍSymfonyµÄÎÞÏÞDZÁ¦£¡
ÒÔÉϾÍÊÇDocker×°ÖÃSymfony£ºÏêϸ½Ì³ÌÓë°ì·¨µÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡