ʹÓÃDocker°²ÅÅSymfony£º¿ìËÙ×îÏÈ¿ª·¢
ʹÓÃDocker°²ÅÅSymfony£º¿ìËÙ×îÏÈ¿ª·¢
СÐò£º
Ëæ×ÅÔÆÅÌËãºÍÈÝÆ÷»¯ÊÖÒյĿìËÙÉú³¤£¬DockerÒѾ³ÉΪÁË¿ª·¢Ö°Ô±°²ÅźÍÖÎÀíÓ¦ÓóÌÐòµÄÊ×Ñ¡¹¤¾ßÖ®Ò»¡£Symfony×÷Ϊһ¸öÊ¢ÐеÄPHP¿ò¼Ü£¬Ò²¿ÉÒÔͨ¹ýDockerÀ´¾ÙÐа²ÅÅ£¬¼«´óµØ¼ò»¯ÁË¿ª·¢ºÍ°²ÅŵÄÀú³Ì¡£±¾ÎĽ«ÏÈÈÝÔõÑùʹÓÃDockerÀ´°²ÅÅSymfonyÓ¦ÓóÌÐò£¬²¢ÌṩÏêϸµÄ´úÂëʾÀý¡£
°ì·¨1£º×°ÖÃDockerºÍDocker Compose
Ê×ÏÈ£¬ÎÒÃÇÐèÒªÔÚÍâµØ»úеÉÏ×°ÖÃDockerºÍDocker Compose¡£DockerµÄ×°Öð취¿ÉÒÔÔÚ¹Ù·½ÍøÕ¾ÉÏÕÒµ½£¬²¢ÇÒƾ֤²Ù×÷ϵͳ¾ÙÐÐÏìÓ¦µÄ×°Öá£Docker ComposeÔò¿ÉÒÔͨ¹ýpipÏÂÁîÀ´×°Öã¬ÀýÈçÔËÐÐpip install docker-compose¡£
°ì·¨2£º½¨ÉèSymfonyÓ¦ÓóÌÐò
ÔÚ×°ÖÃÁËDockerºÍDocker ComposeÖ®ºó£¬ÎÒÃÇ¿ÉÒÔ½¨ÉèÒ»¸öеÄSymfonyÓ¦ÓóÌÐò¡£Ê¹ÓÃÒÔÏÂÏÂÁîÀ´½¨ÉèÒ»¸öеÄSymfonyÏîÄ¿£º
docker run --rm -v $(pwd):/app composer create-project symfony/website-skeleton my-project
µÇ¼ºó¸´ÖÆ
Õâ¸öÏÂÁÔÚÄ¿½ñĿ¼Ï½¨ÉèÒ»¸öÃûΪmy-projectµÄSymfonyÓ¦ÓóÌÐò¡£
°ì·¨3£º±àдDocker ComposeÎļþ
½ÓÏÂÀ´£¬ÎÒÃÇÐèÒª±àдһ¸öDocker ComposeÎļþÀ´½ç˵ÎÒÃǵÄÓ¦ÓóÌÐòµÄЧÀͺÍÒÀÀµ¹Øϵ¡£ÔÚÏîÄ¿µÄ¸ùĿ¼Ï½¨ÉèÒ»¸öÃûΪdocker-compose.ymlµÄÎļþ£¬²¢Ìí¼ÓÒÔÏÂÄÚÈÝ£º
version: '3' services: app: build: context: . dockerfile: Dockerfile ports: - 8000:8000 volumes: - .:/app working_dir: /app depends_on: - mysql mysql: image: mysql:5.7 environment: - MYSQL_ROOT_PASSWORD=root - MYSQL_DATABASE=my_database
µÇ¼ºó¸´ÖÆ
Õâ¸öDocker ComposeÎļþ½ç˵ÁËÁ½¸öЧÀÍ£ºÒ»¸öÊÇSymfonyÓ¦ÓóÌÐò£¬ÁíÒ»¸öÊÇMySQLÊý¾Ý¿â¡£SymfonyЧÀÍʹÓÃÁË×Ô½ç˵µÄDockerfileÀ´¹¹½¨¾µÏñ£¬²¢ÇÒ½«8000¶Ë¿ÚÓ³Éäµ½Ö÷»úµÄ8000¶Ë¿ÚÉÏ¡£MySQLЧÀÍÔòʹÓùٷ½µÄMySQL 5.7¾µÏñ£¬²¢ÉèÖÃÁ˸ùÃÜÂëºÍÊý¾Ý¿âÃû¡£
°ì·¨4£º±àдDockerfile
ΪÁ˹¹½¨SymfonyÓ¦ÓóÌÐòµÄDocker¾µÏñ£¬ÎÒÃÇÐèÒª±àдһ¸öDockerfile¡£ÔÚÏîÄ¿µÄ¸ùĿ¼Ï½¨ÉèÒ»¸öÃûΪDockerfileµÄÎļþ£¬²¢Ìí¼ÓÒÔÏÂÄÚÈÝ£º
FROM php:7.4-apache RUN apt-get update && apt-get install -y git unzip libonig-dev libxml2-dev RUN docker-php-ext-install pdo_mysql mbstring xml COPY . /var/www/html WORKDIR /var/www/html RUN curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer RUN composer install --no-interaction --optimize-autoloader RUN chown -R www-data:www-data /var/www/html
µÇ¼ºó¸´ÖÆ
Õâ¸öDockerfileʹÓÃÁ˹ٷ½µÄPHP 7.4¾µÏñ×÷Ϊ»ù´¡¾µÏñ£¬²¢×°ÖÃÁËһЩÐëÒªµÄÒÀÀµ¡£È»ºó£¬½«SymfonyÓ¦ÓóÌÐòµÄ´úÂ븴ÖƵ½ÈÝÆ÷µÄÖ¸¶¨Ä¿Â¼ÖУ¬²¢Ê¹ÓÃComposer×°ÖÃÓ¦ÓóÌÐòµÄÒÀÀµ¡£×îºó£¬ÎÒÃǽ«ApacheЧÀÍÆ÷µÄÎĵµ¸ùĿ¼ÉèÖÃΪSymfonyÓ¦ÓóÌÐòµÄĿ¼£¬²¢½«ËùÓÐÎļþµÄȨÏÞÐÞ¸ÄΪ¶ÔApache¿Éд¡£
°ì·¨5£º¹¹½¨ºÍÔËÐÐÈÝÆ÷
Ò»ÇÐ×¼±¸Í£µ±ºó£¬ÎÒÃÇ¿ÉÒÔʹÓÃDocker ComposeÀ´¹¹½¨ºÍÔËÐÐÈÝÆ÷¡£ÔÚÏîÄ¿µÄ¸ùĿ¼ÏÂÔËÐÐÒÔÏÂÏÂÁ
docker-compose up -d
µÇ¼ºó¸´ÖÆ
Õâ¸öÏÂÁ»á×Ô¶¯¹¹½¨SymfonyÓ¦ÓóÌÐòµÄDocker¾µÏñ£¬²¢½¨Éè²¢ÔËÐÐÁ½¸öÈÝÆ÷£ºÒ»¸öÊÇSymfonyÓ¦ÓóÌÐòÈÝÆ÷£¬ÁíÒ»¸öÊÇMySQLÊý¾Ý¿âÈÝÆ÷¡£Ó¦ÓóÌÐòÏÖÔÚ¿ÉÒÔͨ¹ýlocalhost:8000À´»á¼û¡£
½áÂÛ£º
ͨ¹ýʹÓÃDockerÀ´°²ÅÅSymfonyÓ¦ÓóÌÐò£¬ÎÒÃÇÄܹ»¿ìËÙµØ×îÏÈ¿ª·¢ºÍ°²ÅÅÀú³Ì£¬²¢ÇÒ×èÖ¹ÁËÓëÍâµØÇéÐεÄÒÀÀµ¹Øϵ¡£Ê¹ÓÃDocker ComposeÀ´½ç˵ЧÀͺÍÒÀÀµ¹Øϵ£¬Äܹ»Ê¹Õû¸ö¿ª·¢ºÍ°²ÅŵÄÀú³ÌÔ½·¢¼ò»¯ºÍ¿É¿¿¡£Ï£Íû±¾ÎÄÌṩµÄ´úÂëʾÀýÄÜ×ÊÖú¶ÁÕßÃǸüºÃµØÃ÷È·ºÍʹÓÃDockerÀ´°²ÅÅSymfonyÓ¦ÓóÌÐò¡£
ÒÔÉϾÍÊÇʹÓÃDocker°²ÅÅSymfony£º¿ìËÙ×îÏÈ¿ª·¢µÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡