{"id":3001,"date":"2017-09-18T11:31:21","date_gmt":"2017-09-18T11:31:21","guid":{"rendered":"https:\/\/intelligentbee.com\/blog\/?p=3001"},"modified":"2024-12-19T09:42:22","modified_gmt":"2024-12-19T09:42:22","slug":"setup-docker-symfony-project","status":"publish","type":"post","link":"https:\/\/intelligentbee.com\/blog\/setup-docker-symfony-project\/","title":{"rendered":"How to Setup Docker for Your Symfony\u00a0Project"},"content":{"rendered":"<p class=\"graf graf--p\">As you probably know, I am a big <a class=\"markup--anchor markup--p-anchor\" href=\"http:\/\/symfony.com\/\" target=\"_blank\" rel=\"noopener\" data-href=\"http:\/\/symfony.com\/\">Symfony<\/a> fan\u00a0\ud83d\ude42 In the last few years I used <a class=\"markup--anchor markup--p-anchor\" href=\"https:\/\/www.vagrantup.com\/\" target=\"_blank\" rel=\"noopener\" data-href=\"https:\/\/www.vagrantup.com\/\">Vagrant<\/a> to set up my Symfony development environment more or less as described <a class=\"markup--anchor markup--p-anchor\" href=\"https:\/\/medium.com\/@dragosholban\/symfony-2-8-jobeet-day-1-starting-up-the-project-9e9c2e70c6ab\" target=\"_blank\" rel=\"noopener\" data-href=\"https:\/\/medium.com\/@dragosholban\/symfony-2-8-jobeet-day-1-starting-up-the-project-9e9c2e70c6ab\">here<\/a>. But we now have <a class=\"markup--anchor markup--p-anchor\" href=\"https:\/\/www.docker.com\/\" target=\"_blank\" rel=\"noopener\" data-href=\"https:\/\/www.docker.com\/\">Docker<\/a> and it\u2019s time to move forward with a new setup. Docker containers are generally more lightweight than Vagrant virtual machines, so starting and stopping them is extremely fast. They also take a lot less disk space.<\/p>\n<p class=\"graf graf--p\">To setup a Docker container you need a <code class=\"markup--code markup--p-code\">Dockerfile<\/code> file and to combine several containers we use the <code class=\"markup--code markup--p-code\">docker-compose.yml<\/code> file. For our environment we will need two containers, one with the latest Ubuntu LTS to host the web server and our project files and one for MySQL.<\/p>\n<p class=\"graf graf--p\">The first container is defined in the <code class=\"markup--code markup--p-code\">Dockerfile<\/code> file as below:<\/p>\n<pre class=\"lang:default decode:true \">FROM ubuntu:16.04\r\nADD . \/app\r\nRUN apt-get update\r\nRUN apt-get install -y php apache2 libapache2-mod-php7.0 php-mysql php-intl git git-core curl php-curl php-xml composer zip unzip php-zip\r\n# Configure Apache\r\nRUN rm -rf \/var\/www\/* \\\r\n    &amp;&amp; a2enmod rewrite \\\r\n    &amp;&amp; echo \"ServerName localhost\" &gt;&gt; \/etc\/apache2\/apache2.conf\r\nADD vhost.conf \/etc\/apache2\/sites-available\/000-default.conf\r\n# Install Symfony\r\nRUN mkdir -p \/usr\/local\/bin\r\nRUN curl -LsS https:\/\/symfony.com\/installer -o \/usr\/local\/bin\/symfony\r\nRUN chmod a+x \/usr\/local\/bin\/symfony\r\n# Add main start script for when image launches\r\nADD run.sh \/run.sh\r\nRUN chmod 0755 \/run.sh\r\nWORKDIR \/app\r\nEXPOSE 80\r\nCMD [\"\/run.sh\"]<\/pre>\n<p class=\"graf graf--p\">Add this file in a new folder. We also need the <code class=\"markup--code markup--p-code\">vhost.conf<\/code> and <code class=\"markup--code markup--p-code\">run.sh<\/code> files used in the code from above.<\/p>\n<p class=\"graf graf--p\">The <code class=\"markup--code markup--p-code\">vhost.conf<\/code> file is used to configure the apache virtual host:<\/p>\n<pre class=\"lang:default decode:true \">&lt;VirtualHost *:80&gt;\r\n    ServerName localhost\r\n    DocumentRoot \/app\/my_project\/web\r\n    &lt;Directory \/app\/my_project\/web&gt;\r\n        Options -Indexes\r\n        AllowOverride All\r\n        Require all granted\r\n    &lt;\/Directory&gt;\r\n&lt;\/VirtualHost&gt;<\/pre>\n<p class=\"graf graf--p\">The <code class=\"markup--code markup--p-code\">run.sh<\/code> file runs when the container starts and just makes sure that the <code class=\"markup--code markup--p-code\">cache<\/code> and <code class=\"markup--code markup--p-code\">logs<\/code> folders are set up before launching the apache web server:<\/p>\n<pre class=\"lang:default decode:true \">#!\/bin\/bash\r\nmkdir -p my_project\/app\/cache my_project\/app\/logs\r\ntouch my_project\/app\/logs\/prod.log\r\ntouch my_project\/app\/logs\/dev.log\r\nchgrp -R www-data .\r\nchmod -R g+w my_project\/app\/cache my_project\/app\/logs\r\nsource \/etc\/apache2\/envvars\r\ntail -F \/var\/log\/apache2\/* my_project\/app\/logs\/prod.log my_project\/app\/logs\/dev.log &amp;\r\nexec apache2 -D FOREGROUND<\/pre>\n<p class=\"graf graf--p\">That\u2019s all for our main Ubuntu container, we now need to create the <code class=\"markup--code markup--p-code\">docker-compose.yml<\/code> file:<\/p>\n<pre class=\"lang:default decode:true\">version: \"2\"\r\nservices:\r\n    mysql:\r\n        image: mysql:5.7\r\n        container_name: mysqldb\r\n        ports:\r\n            - \"4000:3306\"\r\n        expose:\r\n            - \"3306\"\r\n        environment:\r\n            MYSQL_ROOT_PASSWORD: yourrootpassword\r\n    site:\r\n        build: .\r\n        container_name: myproject\r\n        ports:\r\n            - \"8080:80\"\r\n        expose:\r\n            - \"80\"\r\n        depends_on:\r\n            - mysql\r\n        volumes:\r\n          - .:\/app<\/pre>\n<p class=\"graf graf--p\">This will tell Docker to first start an MySQL 5.7 container, then our Ubuntu container that will be able to access the MySQL container using the <code class=\"markup--code markup--p-code\">mysql<\/code> host name.<\/p>\n<p class=\"graf graf--p\">Start everything with the <code class=\"markup--code markup--p-code\">docker-compose up<\/code> command.<\/p>\n<p class=\"graf graf--p\">When it\u2019s done, open a new terminal (let the latest docker compose command run, do not stop it) and use the <code class=\"markup--code markup--p-code\">docker ps<\/code> command to see the running containers:<\/p>\n<p><img decoding=\"async\" class=\"alignnone size-large wp-image-3002\" src=\"https:\/\/intelligentbee.com\/blog\/wp-content\/uploads\/2017\/09\/Screen-Shot-2017-09-15-at-19.11.37-1024x54.png\" alt=\"\" width=\"525\" height=\"28\" srcset=\"https:\/\/intelligentbee.com\/blog\/wp-content\/uploads\/2017\/09\/Screen-Shot-2017-09-15-at-19.11.37-1024x54.png 1024w, https:\/\/intelligentbee.com\/blog\/wp-content\/uploads\/2017\/09\/Screen-Shot-2017-09-15-at-19.11.37-300x16.png 300w, https:\/\/intelligentbee.com\/blog\/wp-content\/uploads\/2017\/09\/Screen-Shot-2017-09-15-at-19.11.37-768x41.png 768w, https:\/\/intelligentbee.com\/blog\/wp-content\/uploads\/2017\/09\/Screen-Shot-2017-09-15-at-19.11.37.png 1692w\" sizes=\"(max-width: 525px) 100vw, 525px\" \/><\/p>\n<p class=\"graf graf--p\">Now, you can take the <code class=\"markup--code markup--p-code\">id<\/code> of the Ubuntu container and ssh into it:<\/p>\n<pre class=\"lang:default decode:true \">docker exec -t -i [ID] bash<\/pre>\n<p class=\"graf graf--p\">Here you will start a new Symfony project as always (you will have to delete the <code class=\"markup--code markup--p-code\">my_project<\/code> folder created by Docker first):<\/p>\n<pre class=\"lang:default decode:true \">rm -rf my_project\r\nsymfony new my_project 2.8<\/pre>\n<p class=\"graf graf--p\">If you name you project something else (yes, you will probably do that) just replace the all <code class=\"markup--code markup--p-code\">my_folder<\/code> occurrences in the files and commands above with the actual name of your project.<\/p>\n<p class=\"graf graf--p\">After you created the new Symfony project, it is better to rebuild the Docker containers so the setup of the <code class=\"markup--code markup--p-code\">cache<\/code> and <code class=\"markup--code markup--p-code\">logs<\/code> folders will be as intended. In the terminal window where you launched Docker, press <code class=\"markup--code markup--p-code\">Ctrl+C<\/code> to stop it then run <code class=\"markup--code markup--p-code\">docker-compose up<\/code> again.<\/p>\n<p class=\"graf graf--p\">That\u2019s it! You can now access your new Symfony project using the following URL: http:\/\/localhost:8080\/. To connect to MySQL you will use the mysql host name:<\/p>\n<pre class=\"lang:default decode:true \">parameters:\r\n    database_host: mysql\r\n    database_port: null\r\n    database_name: symfony\r\n    database_user: root\r\n    database_password: yourrootpassword<\/pre>\n<p>&nbsp;<\/p>\n<p class=\"graf graf--p\">Thank you! Please let me know in the comments if you have any suggestions to improve this setup or problems running it.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>As you probably know, I am a big Symfony fan\u00a0\ud83d\ude42 In the last few years I used Vagrant to set [&hellip;]<\/p>\n","protected":false},"author":3,"featured_media":3004,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[73,82],"tags":[],"yst_prominent_words":[1010,1396,1018,1017,1016,1015,1014,1013,1012,1011,348,1009,1008,1007,1006,1005,1004,977,564,426],"post_mailing_queue_ids":[],"_links":{"self":[{"href":"https:\/\/intelligentbee.com\/blog\/wp-json\/wp\/v2\/posts\/3001"}],"collection":[{"href":"https:\/\/intelligentbee.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/intelligentbee.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/intelligentbee.com\/blog\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/intelligentbee.com\/blog\/wp-json\/wp\/v2\/comments?post=3001"}],"version-history":[{"count":5,"href":"https:\/\/intelligentbee.com\/blog\/wp-json\/wp\/v2\/posts\/3001\/revisions"}],"predecessor-version":[{"id":133367,"href":"https:\/\/intelligentbee.com\/blog\/wp-json\/wp\/v2\/posts\/3001\/revisions\/133367"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/intelligentbee.com\/blog\/wp-json\/wp\/v2\/media\/3004"}],"wp:attachment":[{"href":"https:\/\/intelligentbee.com\/blog\/wp-json\/wp\/v2\/media?parent=3001"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/intelligentbee.com\/blog\/wp-json\/wp\/v2\/categories?post=3001"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/intelligentbee.com\/blog\/wp-json\/wp\/v2\/tags?post=3001"},{"taxonomy":"yst_prominent_words","embeddable":true,"href":"https:\/\/intelligentbee.com\/blog\/wp-json\/wp\/v2\/yst_prominent_words?post=3001"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}