½ÌÄãÔõÑùÔÚDockerÖпìËٴSymfonyÇéÐÎ
½ÌÄãÔõÑùÔÚDockerÖпìËٴSymfonyÇéÐÎ
Ëæ×ÅÔÆÅÌËãºÍÈÝÆ÷ÊÖÒյĿìËÙÉú³¤£¬DockerÒѾ³ÉΪÁËÒ»¸öºÜÊÇÊ¢ÐкÍÓÐÓõŤ¾ß¡£ËüÌṩÁËÒ»ÖÖ¿ìËÙ¡¢¿É¿¿ºÍ±ã½ÝµÄ·½·¨À´´ò°ü¡¢·Ö·¢ºÍÔËÐÐÓ¦ÓóÌÐò¡£SymfonyÊÇÒ»¸öÓÅÒìµÄPHP¿ò¼Ü£¬ËüÄܹ»×ÊÖúÎÒÃÇ¿ìËÙ¹¹½¨¸ßÖÊÁ¿µÄWebÓ¦ÓóÌÐò¡£ÔÚ±¾ÎÄÖУ¬ÎÒ½«ÏòÄãչʾÔõÑùʹÓÃDocker¿ìËٴSymfonyÇéÐΡ£
°ì·¨1: ×°ÖÃDockerºÍDocker Compose
Ê×ÏÈ£¬ÄãÐèÒªÔÚÄãµÄÅÌËã»úÉÏ×°ÖÃDockerºÍDocker Compose¡£Äã¿ÉÒÔÔÚDocker¹Ù·½ÍøÕ¾ÉÏÕÒµ½ÊʺÏÄã²Ù×÷ϵͳµÄ×°Öðü£¬²¢Æ¾Ö¤¹Ù·½Îĵµ¾ÙÐÐ×°Öá£
°ì·¨2: ½¨ÉèDockerfile
ÔÚÄãµÄÏîÄ¿¸ùĿ¼Ï½¨ÉèÒ»¸öÃûΪDockerfileµÄÎļþ£¬²¢ÔÚÆäÖÐÌí¼ÓÒÔÏÂÄÚÈÝ£º
FROM php:7.4-apache # ×°ÖÃSymfonyËùÐèµÄÀ©Õ¹ RUN docker-php-ext-install pdo pdo_mysql # Ìí¼ÓApacheµÄÉèÖÃÎļþ COPY apache.conf /etc/apache2/sites-available/000-default.conf # ×°ÖÃComposer RUN curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer
µÇ¼ºó¸´ÖÆ
ÉÏÊöDockerfileÎļþÖУ¬ÎÒÃÇʹÓÃÁ˹ٷ½ÌṩµÄphp:7.4-apache×÷Ϊ»ù´¡¾µÏñ¡£È»ºó£¬ÎÒÃÇ×°ÖÃÁËSymfonyËùÐèµÄPDOºÍpdo_mysqlÀ©Õ¹£¬²¢½«ApacheµÄÉèÖÃÎļþ´ÓÍâµØ¸´ÖƵ½ÈÝÆ÷ÄÚµÄ׼ȷλÖá£×îºó£¬ÎÒÃÇ×°ÖÃÁËComposer£¬ÒÔ±ãÎÒÃÇÄܹ»Ê¹ÓÃComposerÖÎÀíSymfonyÏîÄ¿µÄÒÀÀµ¡£
°ì·¨3: ½¨ÉèApacheÉèÖÃÎļþ
ÔÚÄãµÄÏîÄ¿¸ùĿ¼Ï½¨ÉèÒ»¸öÃûΪapache.confµÄÎļþ£¬²¢ÔÚÆäÖÐÌí¼ÓÒÔÏÂÄÚÈÝ£º
<VirtualHost *:80> DocumentRoot /var/www/html/public <Directory /var/www/html/public> AllowOverride All Order allow,deny Allow from all </Directory> ErrorLog ${APACHE_LOG_DIR}/error.log CustomLog ${APACHE_LOG_DIR}/access.log combined </VirtualHost>
µÇ¼ºó¸´ÖÆ
ÉÏÊöapache.confÎļþÖУ¬ÎÒÃÇÖ¸¶¨ÁËSymfonyÏîÄ¿µÄpublicĿ¼×÷ΪDocumentRoot£¬²¢ÆôÓÃÁËËùÓеÄApacheÖØд¹æÔò¡£
°ì·¨4: ½¨Éèdocker-compose.ymlÎļþ
ÔÚÄãµÄÏîÄ¿¸ùĿ¼Ï½¨ÉèÒ»¸öÃûΪdocker-compose.ymlµÄÎļþ£¬²¢ÔÚÆäÖÐÌí¼ÓÒÔÏÂÄÚÈÝ£º
version: '3' services: web: build: . ports: - "8000:80" volumes: - .:/var/www/html links: - db db: image: mysql:8.0 ports: - "3306:3306" environment: - MYSQL_ROOT_PASSWORD=root - MYSQL_DATABASE=symfony volumes: - ./database:/var/lib/mysql
µÇ¼ºó¸´ÖÆ
ÉÏÊödocker-compose.ymlÎļþÖУ¬ÎÒÃǽç˵ÁËÁ½¸öЧÀÍ: webºÍdb¡£webЧÀÍʹÓÃÁËÎÒÃÇÉÏÊö½¨ÉèµÄDockerfile¹¹½¨£¬²¢½«ÈÝÆ÷ÄÚµÄ80¶Ë¿ÚÓ³Éäµ½Ö÷»úµÄ8000¶Ë¿Ú¡£ÎÒÃÇ»¹½«Ä¿½ñĿ¼ӳÉäΪÈÝÆ÷ÄÚµÄ/var/www/htmlĿ¼£¬ÒÔ±ãÎÒÃÇÄܹ»ÔÚÈÝÆ÷ÄÚ»á¼û×ðÁú¿Ê±SymfonyÏîÄ¿¡£dbЧÀÍʹÓÃÁ˹ٷ½ÌṩµÄmysql:8.0¾µÏñ£¬²¢½«ÈÝÆ÷ÄÚµÄ3306¶Ë¿ÚÓ³Éäµ½Ö÷»úµÄ3306¶Ë¿Ú¡£ÎÒÃÇ»¹Ö¸¶¨ÁËMySQLµÄ¸ùÃÜÂëºÍ½¨ÉèµÄÊý¾Ý¿â¡£
°ì·¨5: Æô¶¯SymfonyÈÝÆ÷
ÔÚÖÕ¶ËÖУ¬½øÈëÄãµÄÏîÄ¿¸ùĿ¼£¬²¢ÔËÐÐÒÔÏÂÏÂÁîÆô¶¯SymfonyÈÝÆ÷£º
docker-compose up -d
µÇ¼ºó¸´ÖÆ
¸ÃÏÂÁ»áÏÂÔØËùÐèµÄ¾µÏñ¡¢¹¹½¨ÈÝÆ÷²¢ÔËÐС£ÉÔµÈƬÉΣ¬Æô¶¯Àú³Ì¿ÉÄÜÐèҪһЩʱ¼ä¡£
°ì·¨6: ×°ÖÃSymfony
ÔÚSymfonyÈÝÆ÷Æô¶¯ºó£¬Äã¿ÉÒÔͨ¹ýÒÔÏÂÏÂÁî½øÈëÈÝÆ÷ÄÚ²¿£º
docker exec -it [CONTAINER_NAME] bash
µÇ¼ºó¸´ÖÆ
ÔÚ¸ÃÏÂÁîÖУ¬½«[CONTAINER_NAME]Ì滻ΪÄãSymfonyÈÝÆ÷µÄÃû³Æ¡£
ÔÚÈÝÆ÷ÄÚ²¿£¬Äã¿ÉÒÔʹÓÃComposer×°ÖÃSymfony¡£ÔÚÈÝÆ÷ÄÚÔËÐÐÒÔÏÂÏÂÁ
composer create-project symfony/website-skeleton .
µÇ¼ºó¸´ÖÆ
¸ÃÏÂÁî»áÔÚÄ¿½ñĿ¼Ï½¨ÉèÒ»¸öеÄSymfonyÏîÄ¿¡£
°ì·¨7: »á¼ûSymfonyÓ¦ÓóÌÐò
ÔÚ×°ÖÃÍê³Éºó£¬Äã¿ÉÒÔͨ¹ýÔÚä¯ÀÀÆ÷Öлá¼ûÒÔÏÂURLÀ´Éó²éSymfonyÓ¦ÓóÌÐò£º
http://localhost:8000
µÇ¼ºó¸´ÖÆ
¹§Ï²Ä㣡ÄãÏÖÔÚÒѾÀֳɵØÔÚDockerÖпìËٴÁËSymfonyÇéÐΡ£ÄãÏÖÔÚ¿ÉÒÔ×îÏÈʹÓÃSymfony¹¹½¨ÄãµÄÓ¦ÓóÌÐò£¬²¢ÏíÊÜÈÝÆ÷»¯´øÀ´µÄ±ãµ±ºÍÎÞаÐÔ¡£
×ܽá
ͨ¹ýʹÓÃDockerºÍDocker Compose£¬ÎÒÃÇ¿ÉÒÔ¿ìËÙµØÔÚÍâµØ´î½¨Ò»¸öSymfonyÇéÐΡ£ÕâʹµÃ¿ª·¢Ö°Ô±¿ÉÒÔÔ½·¢×¨×¢ÓÚÓ¦ÓóÌÐòµÄ¿ª·¢£¬¶ø²»ÐèÒª²ÙÐĵØÉèÖúÍÖÎÀí¿ª·¢ÇéÐΡ£Ï£Íû±¾ÎÄÄܹ»¶ÔÄãÓÐËù×ÊÖú£¬×£Äã¹¹½¨³ö¸ü¶à¾«²ÊµÄSymfonyÓ¦ÓóÌÐò£¡
ÒÔÉϾÍÊǽÌÄãÔõÑùÔÚDockerÖпìËٴSymfonyÇéÐεÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡