<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Nathan Dauda</title>
    <description>The latest articles on DEV Community by Nathan Dauda (@naterus).</description>
    <link>https://dev.to/naterus</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F520819%2F7e744499-cf3a-4279-99e5-45c28a89d35d.png</url>
      <title>DEV Community: Nathan Dauda</title>
      <link>https://dev.to/naterus</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/naterus"/>
    <language>en</language>
    <item>
      <title>Docker Concepts And Containerizing Laravel/PHP Applications.</title>
      <dc:creator>Nathan Dauda</dc:creator>
      <pubDate>Thu, 14 Jan 2021 09:36:08 +0000</pubDate>
      <link>https://dev.to/naterus/docker-concepts-and-containerizing-laravel-php-applications-14gn</link>
      <guid>https://dev.to/naterus/docker-concepts-and-containerizing-laravel-php-applications-14gn</guid>
      <description>&lt;p&gt;In this post, i will explain docker images, containers, why you should use them, and how to use them in a laravel application.&lt;/p&gt;

&lt;h1&gt;
  
  
  What is docker?
&lt;/h1&gt;

&lt;p&gt;Docker is a container based technology that helps in software delivery and infrastructure management in form of packages called containers.&lt;/p&gt;

&lt;h1&gt;
  
  
  What is an image
&lt;/h1&gt;

&lt;p&gt;A docker image consists of layers of instructions on how to create a container.&lt;/p&gt;

&lt;p&gt;You can think of a docker image as the source code and container as the source code in execution or in a running state.&lt;/p&gt;

&lt;h1&gt;
  
  
  What is a container
&lt;/h1&gt;

&lt;p&gt;A Container is a running instance of an image. So a running docker image is called a container.&lt;/p&gt;

&lt;h1&gt;
  
  
  Why use docker in a laravel application
&lt;/h1&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Docker streamlines the setup process of running a LEMP stack application like Laravel, mysql, php, nginx.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;It makes it easier for new team members to run application without worrying about infrastructural set up and configuration. &lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;It also makes your application deployment ready.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h1&gt;
  
  
  Containerizing laravel/php application
&lt;/h1&gt;

&lt;p&gt;Traditionally when you clone or download a php or laravel application, you need to configure your web server, database server and any other service needed to run the app. But with docker-compose, you can run your app with just a single command and all those services would be readily available with no configuration required.&lt;/p&gt;

&lt;p&gt;Here is an example of a project i created that uses a shell script to run a laravel application in a docker container.&lt;/p&gt;

&lt;p&gt;The shell script checks for docker and docker-compose installations and installs them if not found.&lt;/p&gt;

&lt;p&gt;Next, It creates and runs nginx, mysql and php image using docker-compose.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Clone the project from github
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;    git clone https://github.com/Naterus/docker-compose-laravel.git
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;&lt;p&gt;verify you have all files and folders matching with the repository.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;navigate to the root directory of the cloned project in your terminal and type&lt;br&gt;
&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;  &lt;span class="nb"&gt;sudo&lt;/span&gt; ./launch.sh
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Look out for prompts and select Y to all.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;You should see nginx-service,mysql-service and php-service all started. &lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;open &lt;a href="https://dev.tolocalhost:8088"&gt;localhost:8088&lt;/a&gt; in your browser, you should see &lt;a href="https://restfulcountries.com"&gt;https://restfulcountries.com&lt;/a&gt; app running.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h1&gt;
  
  
  Project Structure
&lt;/h1&gt;

&lt;p&gt;Here is a short description of the files and directories of  the project.&lt;/p&gt;

&lt;h3&gt;
  
  
  launch.sh file
&lt;/h3&gt;

&lt;p&gt;This file builds nginx,mysql,php as docker images using &lt;code&gt;docker-compose build&lt;/code&gt; command and then run them as containers using  &lt;code&gt;docker-compose up -d&lt;/code&gt;. Then lastly runs laravel database migration.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;  #!/bin/bash

#Check for docker installation
if [ -x "$(command -v docker)" ]; then
    echo "docker already installed, skipping"
else
    echo "Installing docker"

    apt-get update

    apt-get install docker-ce docker-ce-cli containerd.io

    echo "docker installed successfully"
fi

#Check for docker-compose installation
if [ -x "$(command -v docker-compose)" ]; then
    echo "docker-compose already installed, skipping"
else
    apt install docker-compose
    echo "docker-compose installed successfully"
fi

cd public_html

#change storage permission to enable nginx read and write files
chmod -R 755 storage

cd ../

docker-compose build &amp;amp;&amp;amp; docker-compose up -d

docker-compose exec php php /var/www/html/artisan migrate
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  docker-compose.yml file
&lt;/h3&gt;

&lt;p&gt;This file builds all services and mounts them as docker images.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;version: '3'

networks:
  appnetwork:

services:

  nginx:
    image: nginx:stable-alpine
    container_name: nginx-service
    ports:
      - "8088:80"
    volumes:
      - ./public_html:/var/www/html
      - ./nginx/default.conf:/etc/nginx/conf.d/default.conf
      - ./nginx/log/error.log:/var/log/nginx/error.log
      - ./nginx/log/access.log:/var/log/nginx/access.log
    depends_on:
      - php
      - mysql
    networks:
      - appnetwork

  mysql:
    image: mysql:5.7.32
    container_name: mysql-service
    restart: unless-stopped
    tty: true
    ports:
      - "4306:3306"
    volumes:
      - ./mysql:/var/lib/mysql
      - ./mysql/my.cnf:/etc/mysql/my.cnf
    environment:
       MYSQL_DATABASE: webapp
       MYSQL_USER: root
       MYSQL_PASSWORD: password1
       MYSQL_ROOT_PASSWORD: password1
       SERVICE_TAGS: dev
       SERVICE_NAME: mysql
    networks:
      - appnetwork

  php:
    build:
      context: .
      dockerfile: Dockerfile
    container_name: php-service
    volumes:
      - ./public_html:/var/www/html
    ports:
      - "9000:9000"
    networks:
      - appnetwork

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;volumes are symbolic links to be used by nginx to write and read files from project directory.&lt;/p&gt;

&lt;h3&gt;
  
  
  Dockerfile
&lt;/h3&gt;

&lt;p&gt;The Dockerfile retrieves php alpine image from dockerhub and installs pdo mysql when the &lt;code&gt;docker-compose up -d&lt;/code&gt; command executes in &lt;strong&gt;launch.sh&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;FROM php:7.4-fpm-alpine

RUN docker-php-ext-install pdo_mysql

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  public_html Directory
&lt;/h3&gt;

&lt;p&gt;The laravel application is found in public_html directory which is a symlink to var/www/html on nginx server. &lt;/p&gt;

&lt;p&gt;If you want to run your own laravel or custom php application, you can replace the content of public_html with your application files. &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Note&lt;/strong&gt; that custom php applications must be placed in public_html/public directory, because the document root for nginx is the public folder.&lt;/p&gt;

&lt;h3&gt;
  
  
  nginx directory
&lt;/h3&gt;

&lt;p&gt;This directory is a symlink used by nginx to read configuration (&lt;code&gt;default.conf&lt;/code&gt;) and writes access and error logs (&lt;code&gt;access.log&lt;/code&gt; , &lt;code&gt;error.log&lt;/code&gt;). &lt;/p&gt;

&lt;h3&gt;
  
  
  mysql directory
&lt;/h3&gt;

&lt;p&gt;This directory is also a symlink used by mysql to read configurations (my.cnf).&lt;/p&gt;

&lt;p&gt;Your custom application mysql credentials should match the ones in mysql-service container as seen in &lt;strong&gt;docker-compose.yml&lt;/strong&gt;. here is an example of my laravel .env database credential&lt;/p&gt;

&lt;p&gt;DB_CONNECTION=mysql&lt;br&gt;
DB_HOST=mysql&lt;br&gt;
DB_PORT=3306&lt;br&gt;
DB_DATABASE=webapp&lt;br&gt;
DB_USERNAME=root&lt;br&gt;
DB_PASSWORD=password1&lt;/p&gt;

&lt;h1&gt;
  
  
  Conclusion
&lt;/h1&gt;

&lt;p&gt;The use of containers in development is one of the techniques adopted by DevOps engineers to improve productivity. It makes the workflow seamless, But not compulsory to adopt. &lt;/p&gt;

&lt;p&gt;For more information on this project, check out the github repository &lt;a href="https://github.com/Naterus/docker-compose-laravel"&gt;docker-compose-laravel&lt;/a&gt;&lt;/p&gt;

</description>
      <category>docker</category>
      <category>laravel</category>
      <category>nginx</category>
      <category>mysql</category>
    </item>
    <item>
      <title>Populate html select fields with countries and states from api using jquery</title>
      <dc:creator>Nathan Dauda</dc:creator>
      <pubDate>Wed, 02 Dec 2020 10:12:17 +0000</pubDate>
      <link>https://dev.to/naterus/populate-html-select-fields-with-countries-and-states-from-api-using-jquery-3d89</link>
      <guid>https://dev.to/naterus/populate-html-select-fields-with-countries-and-states-from-api-using-jquery-3d89</guid>
      <description>&lt;p&gt;This post shows you how to easily populate countries and their states in  html select field using &lt;a href="https://restfulcountries.com"&gt;restful countries&lt;/a&gt;  api and jquery.&lt;/p&gt;

&lt;p&gt;I assume you already know the basics of Jquery so i won't be giving line by line code details. just copy and paste the code below in your html and you are good to go.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"&amp;gt;&amp;lt;/script&amp;gt;
&amp;lt;script&amp;gt;
        //Populate countries select on page load
        $(document).ready(function(){
            //Call restful countries country endpoint
            $.get('https://restfulcountries.com/api/v1/countries?fetch_type=slim',function(countries){

                //Loop through returned result and populate countries select
                $.each(countries.data,function(key,value){
                    $('#country-select')
                        .append($("&amp;lt;option&amp;gt;&amp;lt;/option&amp;gt;")
                            .attr("value", value.name)
                            .text(value.name));
                });
            });
        });

        //Function to fetch states
        function initStates(){
            //Get selected country name
            let country=$("#country-select").val();

            //Remove previous loaded states
            $('#state-select option:gt(0)').remove();
            $('#district-select option:gt(0)').remove();

            //Call restful countries states endpoint
            $.get('https://restfulcountries.com/api/v1/countries/'+country+'/states?fetch_type=slim',function(states){

                //Loop through returned result and populate states select
                $.each(states.data,function(key,value){
                    $('#state-select')
                        .append($("&amp;lt;option&amp;gt;&amp;lt;/option&amp;gt;")
                            .attr("value", value.name)
                            .text(value.name));
                });
            });
        }
    &amp;lt;/script&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Then finally add 2 select fields in your html form with IDs &lt;code&gt;country-select&lt;/code&gt; and &lt;code&gt;state-select&lt;/code&gt; which would be referenced in the script. The country select has an onchange event listener to populate the states based on the country selected.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;
&amp;lt;select onchange="initStates()" id="country-select" class="form-control"&amp;gt;
    &amp;lt;option selected disabled&amp;gt;Country&amp;lt;/option&amp;gt;
&amp;lt;/select&amp;gt;

&amp;lt;select  id="state-select" name="" class="form-control"&amp;gt;
    &amp;lt;option selected disabled&amp;gt;State&amp;lt;/option&amp;gt;
&amp;lt;/select&amp;gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Notice the &lt;code&gt;fetch_type=slim&lt;/code&gt; on the api call urls, That indicates an option to only fetch the country name flag and calling code, That's because restful countries api returns other details like presidents,covid 19, population and others. and you don't need all that to populate your select.&lt;/p&gt;

&lt;p&gt;You can add your own styling like a preloader on the select while it loads data, or the selected countries flag display on the country select, you can get the flag url from the country result as &lt;code&gt;value.href.flag&lt;/code&gt; .&lt;/p&gt;

&lt;p&gt;If you want to explore restful countries for other use cases, you may visit the website &lt;a href="https://restfulcountries.com/"&gt;https://restfulcountries.com/&lt;/a&gt; or follow twitter page &lt;a href="https://twitter.com/restfulcountrie"&gt;@restfulcountrie&lt;/a&gt; .&lt;/p&gt;

&lt;p&gt;Thank you for viewing this post. lets connect on twitter &lt;a href="https://twitter.com/NateDauda"&gt;@NateDauda&lt;/a&gt;&lt;/p&gt;

</description>
      <category>jquery</category>
      <category>countries</category>
      <category>states</category>
      <category>restfulcountries</category>
    </item>
  </channel>
</rss>
