{
    "componentChunkName": "component---src-templates-blog-post-js",
    "path": "/docker-setup-for-laravel-application/",
    "result": {"data":{"site":{"siteMetadata":{"title":"CrewCode Solutions"}},"markdownRemark":{"id":"770d20bd-f14d-5c05-9bbd-1b714b97f518","excerpt":"In this article i will explain you step by step to dockerize your Laravel application We need to create three Application container MySql Database, Nginx Web…","html":"<p>In this article i will explain you step by step to dockerize your Laravel application</p>\n<p>We need to create three Application container MySql Database, Nginx Web Server, PHP Interpreter and utility container composer, laravel artisan and npm</p>\n<h4>Step 1: Adding a Nginx container</h4>\n<p><strong><em>Create docker-compose.yaml file in the root path</em></strong> of your project folder and start adding services first of all we will add nginx server services</p>\n<p><img src=\"https://crew-code-images.s3.us-east-1.amazonaws.com/blog_images/docker-laravel.png\" alt=\"nginx-server\"></p>\n<p>This is the container which will communicate with php container it acts as interpreter for php</p>\n<pre><code class=\"language-js\">//copy paste following code inside docker-compose.yaml file\n\nversion: \"3.8\"\nservices:\n  server:\n    image: \"nginx:stable-alpine\"\n    ports:\n      - \"8000:80\"\n    volumes:\n      - ./src:/var/www/html\n      - ./nginx/nginx.conf:/etc/nginx/conf.d/default.conf:ro\n</code></pre>\n<p><strong><em>Add a nginx folder</em></strong> and inside that folder <strong><em>create a file named nginx.conf</em></strong> and paste the following code</p>\n<pre><code class=\"language-js\">\n  //copy paste following code inside nginx.conf\n\n  server {\n    listen 80;\n    index index.php index.html;\n    server_name localhost;\n    root /var/www/html/public;\n    location / {\n        try_files $uri $uri/ /index.php?$query_string;\n    }\n    location ~ \\.php$ {\n        try_files $uri =404;\n        fastcgi_split_path_info ^(.+\\.php)(/.+)$;\n        fastcgi_pass php:9000;\n        fastcgi_index index.php;\n        include fastcgi_params;\n        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;\n        fastcgi_param PATH_INFO $fastcgi_path_info;\n    }\n  }\n</code></pre>\n<h4>Step 2: Adding a php container</h4>\n<p><strong><em>Add a folder name dockerfiles</em></strong> and in that folder create a docker file with the name <strong><em>php.dockerfile</em></strong> for the php container in order to build the image also create a src folder at root path</p>\n<p><img src=\"https://crew-code-images.s3.us-east-1.amazonaws.com/blog_images/docker-file.png\" alt=\"docker-file\"></p>\n<pre><code class=\"language-js\">//paste the following code inside php.dockerfile which is created inside dockerfiles folder\nFROM php:8.0-fpm\n\nWORKDIR /var/www/html\n\nRUN docker-php-ext-install pdo pdo_mysql\n</code></pre>\n<p>When using <strong><em>Docker on Linux</em></strong>, you might face <strong><em>permission errors</em></strong> when adding a bind mount as shown in the next lecture. If you do, try these steps:</p>\n<p><strong><em>Change the php.dockerfile</em></strong> so that it looks like that:</p>\n<pre><code class=\"language-js\">FROM php:8.0-fpm\n\nWORKDIR /var/www/html\n\nCOPY src .\n\nRUN docker-php-ext-install pdo pdo_mysql\n\nRUN addgroup -g 1000 laravel &#x26;&#x26; adduser -G laravel -g laravel -s /bin/sh -D laravel\n\nUSER laravel\n</code></pre>\n<p><strong><em>Update your docker-compose.yaml</em></strong> file with following code</p>\n<pre><code class=\"language-js\">//update your docker-compose.yaml here container for php is added with bind volume which is mapped to the local folder src\nversion: \"3.8\"\nservices:\n  server:\n    image: \"nginx:stable-alpine\"\n    ports:\n      - \"8000:80\"\n    volumes:\n      - ./src:/var/www/html\n      - ./nginx/nginx.conf:/etc/nginx/conf.d/default.conf:ro\n    depends_on:\n      - php\n      - mysql\n  php:\n    build:\n      context: ./dockerfiles\n      dockerfile: php.dockerfile\n    volumes:\n      - ./src:/var/www/html:delegated\n</code></pre>\n<h4>Step 2: Adding mysql container</h4>\n<p>Here we will add mysql container inside docker-compose.yaml file also we will <strong><em>create</em></strong> <strong><em>env folders</em></strong> and inside that env folders <strong><em>create file with name</em></strong> <strong><em>mysql.env</em></strong> for the required configuration i'e username , password for mysql container</p>\n<p><img src=\"https://crew-code-images.s3.us-east-1.amazonaws.com/blog_images/mysql-env-file.png\" alt=\"mysql-env-file\"></p>\n<p><strong><em>Copy paste following code inside env file</em></strong> which you created</p>\n<pre><code class=\"language-js\">MYSQL_DATABASE = homestead;\nMYSQL_USER = homestead;\nMYSQL_PASSWORD = secret;\nMYSQL_ROOT_PASSWORD = secret;\n</code></pre>\n<p><strong><em>Update your docker-compose.yaml file</em></strong> with mysql container</p>\n<pre><code class=\"language-js\">version: \"3.8\"\nservices:\n  server:\n    image: \"nginx:stable-alpine\"\n    ports:\n      - \"8000:80\"\n    volumes:\n      - ./src:/var/www/html\n      - ./nginx/nginx.conf:/etc/nginx/conf.d/default.conf:ro\n    depends_on:\n      - php\n      - mysql\n  php:\n    build:\n      context: ./dockerfiles\n      dockerfile: php.dockerfile\n    volumes:\n      - ./src:/var/www/html:delegated\n  mysql:\n    image: mysql:5.7\n    env_file:\n      - ./env/mysql.env\n\n</code></pre>\n<h4>Step 3: Adding a composer utility container to create laravel project</h4>\n<p><strong><em>Create composer.dockerfile inside dockerfiles folder</em></strong> in which we will be writing executable command to create laravel project and bind mount with src folder in our local folder</p>\n<p><img src=\"https://crew-code-images.s3.us-east-1.amazonaws.com/blog_images/mysql-env-2.png\" alt=\"mysql-env-file\"></p>\n<p><strong><em>Copy paste the following code</em></strong> inside composer.dockerfile</p>\n<pre><code class=\"language-js\">//copy paste following code inside composer.dockerfile\n\nFROM composer:latest\n\nWORKDIR /var/www/html\n\nENTRYPOINT [ \"composer\", \"--ignore-platform-reqs\" ]\n\n</code></pre>\n<p>When using <strong><em>Docker on Linux</em></strong>, you might face <strong><em>permission errors</em></strong> when adding a bind mount as shown in the next lecture. If you do</p>\n<p><strong><em>Change the compose.dockerfile</em></strong> so that it looks like that:</p>\n<pre><code class=\"language-js\">FROM composer:latest\n\nRUN addgroup -g 1000 laravel &#x26;&#x26; adduser -G laravel -g laravel -s /bin/sh -D laravel\n\nUSER laravel\n\nWORKDIR /var/www/html\n\nENTRYPOINT [ \"composer\", \"--ignore-platform-reqs\" ]\n</code></pre>\n<p><strong><em>Update docker.compose.yaml file</em></strong> with composer container</p>\n<pre><code class=\"language-js\">version: \"3.8\"\nservices:\n  server:\n    image: \"nginx:stable-alpine\"\n    ports:\n      - \"8000:80\"\n    volumes:\n      - ./src:/var/www/html\n      - ./nginx/nginx.conf:/etc/nginx/conf.d/default.conf:ro\n    depends_on:\n      - php\n      - mysql\n  php:\n    build:\n      context: ./dockerfiles\n      dockerfile: php.dockerfile\n    volumes:\n      - ./src:/var/www/html:delegated\n  mysql:\n    image: mysql:5.7\n    env_file:\n      - ./env/mysql.env\n  composer:\n    build:\n      context: ./dockerfiles\n      dockerfile: composer.dockerfile\n    volumes:\n      - ./src:/var/www/html\n\n</code></pre>\n<h4>Step 4: Create laravel application via composer utility container</h4>\n<p>After finishing the setup for composer utility container <strong><em>we will run docker composer command to create laravel project</em></strong> using composer utility container we can run any specific container inside docker-compose.yaml via docker-compose command</p>\n<p>Go to your project folder and run following command in your terminal also <strong><em>make sure your docker is running in your computer</em></strong></p>\n<pre><code class=\"language-js\">//make sure you run the whole command including . at the end\ndocker-compose run --rm composer create-project --prefer-dist laravel/laravel .\n</code></pre>\n<p><img src=\"https://crew-code-images.s3.us-east-1.amazonaws.com/blog_images/docker-compose-create.png\" alt=\"docker-compose-create\"></p>\n<p>After executing above command you will see that laravel project has been created inside your local src folder</p>\n<h4>Step 5: Updating .env file of the project inside src folder, updating docker-compose.yaml and running server container</h4>\n<p>We should <strong><em>update the .env file for the project which is created inside src folder</em></strong> with the same exact configuration that we have <strong><em>mentioned in mysql.env file inside env folders</em></strong></p>\n<p>Change the following configuration</p>\n<pre><code class=\"language-js\">DB_HOST = mysql;\nDB_DATABASE = homestead;\nDB_USERNAME = homestead;\nDB_PASSWORD = secret;\n</code></pre>\n<p><img src=\"https://crew-code-images.s3.us-east-1.amazonaws.com/blog_images/Docker-setup-laravel-app-env-1.png\" alt=\"env-file\"></p>\n<p><strong><em>Update docker-compose.yaml file</em></strong> which look similar to below</p>\n<pre><code class=\"language-js\">version: \"3.8\"\nservices:\n  server:\n    image: \"nginx:stable-alpine\"\n    ports:\n      - \"8000:80\"\n    volumes:\n      - ./src:/var/www/html\n      - ./nginx/nginx.conf:/etc/nginx/conf.d/default.conf:ro\n    depends_on:\n      - php\n      - mysql\n  php:\n    build:\n      context: ./dockerfiles\n      dockerfile: php.dockerfile\n    volumes:\n      - ./src:/var/www/html:delegated\n  mysql:\n    image: mysql:5.7\n    env_file:\n      - ./env/mysql.env\n  composer:\n    build:\n      context: ./dockerfiles\n      dockerfile: composer.dockerfile\n    volumes:\n      - ./src:/var/www/html\n</code></pre>\n<p>Run the below command to <strong><em>start php, mysql and server container</em></strong></p>\n<pre><code class=\"language-js\"> docker-compose up -d --build server\n</code></pre>\n<p>You may <strong><em>face issue</em></strong> while pulling the mysql image from docker hub on <strong><em>Apple M1 processor</em></strong> so <strong><em>add this line</em></strong> under mysql services\nbelow image</p>\n<pre><code class=\"language-js\">image: mysql: 5.7;\nplatform: linux / amd64;\n</code></pre>\n<p><img src=\"https://crew-code-images.s3.us-east-1.amazonaws.com/blog_images/Docker-setup-laravel-app-env-2.png\" alt=\"env-file\"></p>\n<p>Open the url localhost:8000 to see the application running</p>","fields":{"slug":"/docker-setup-for-laravel-application/"},"frontmatter":{"title":"Docker setup for Laravel application","date":"August 01, 2022","description":"In this article i will explain you step by step to setup docker container for your Laravel application","bannerimage":"https://crew-code-images.s3.us-east-1.amazonaws.com/blog_images/docker.jpg"}},"previous":{"fields":{"slug":"/docker-setup-for-express-react-mongodb-application/"},"frontmatter":{"title":"Docker setup for your MERN (MongoDB, Node, Express, React) application","date":"July 25, 2022","bannerimage":"https://crew-code-images.s3.us-east-1.amazonaws.com/blog_images/docker.jpg"}},"next":{"fields":{"slug":"/docker-wordpress-setup-with-mysql-phpmyadmin/"},"frontmatter":{"title":"Wordpress setup with MySql and PhpMyAdmin using docker | Docker wordpress tutorial","date":"August 10, 2022","bannerimage":"https://crew-code-images.s3.us-east-1.amazonaws.com/blog_images/WordpressDocker.jpg"}}},"pageContext":{"id":"770d20bd-f14d-5c05-9bbd-1b714b97f518","previousPostId":"c7eae7dc-7c82-5b16-b22a-ed80c892c31e","nextPostId":"43e66af1-2091-5ed9-98ad-83c6da548d47"}},
    "staticQueryHashes": ["3860684146"]}