<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Kumar Ravi on Medium]]></title>
        <description><![CDATA[Stories by Kumar Ravi on Medium]]></description>
        <link>https://medium.com/@kumarravisingh?source=rss-52277df79043------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*-3WBESBmdOcsXsGwjr19cw.png</url>
            <title>Stories by Kumar Ravi on Medium</title>
            <link>https://medium.com/@kumarravisingh?source=rss-52277df79043------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sun, 06 Dec 2026 00:52:52 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@kumarravisingh/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[PHP Match Expression Is The New And Improved Switch Statement]]></title>
            <link>https://kumarravisingh.medium.com/php-match-expression-is-the-new-and-improved-switch-statement-9b1d0b53431d?source=rss-52277df79043------2</link>
            <guid isPermaLink="false">https://medium.com/p/9b1d0b53431d</guid>
            <category><![CDATA[php]]></category>
            <category><![CDATA[switch]]></category>
            <category><![CDATA[matche]]></category>
            <category><![CDATA[package]]></category>
            <category><![CDATA[laravel]]></category>
            <dc:creator><![CDATA[Kumar Ravi]]></dc:creator>
            <pubDate>Fri, 03 Mar 2023 13:15:49 GMT</pubDate>
            <atom:updated>2023-03-07T11:01:15.293Z</atom:updated>
            <content:encoded><![CDATA[<p>Let’s admit it, we mostly do not prefer to write Switch statements. No matter how many if else, if else, we write, unless it is very much needed we ignore the switch statement in the PHP world.</p><p>The reason can be many, for me the reason it its confusing structure and the fact that after every case I have to write a break to stop it from further going into the statement.</p><p>I believe the PHP team recognised it and now in PHP 8, we have a match expression which is an enhancement over the traditional switch statement.</p><p>I have published a video to show you some examples of how it works so If you prefer to watch a video you can watch it here.</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fwww.youtube.com%2Fembed%2FBobgZ-ygM78%3Ffeature%3Doembed&amp;display_name=YouTube&amp;url=https%3A%2F%2Fwww.youtube.com%2Fwatch%3Fv%3DBobgZ-ygM78&amp;image=https%3A%2F%2Fi.ytimg.com%2Fvi%2FBobgZ-ygM78%2Fhqdefault.jpg&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=youtube" width="854" height="480" frameborder="0" scrolling="no"><a href="https://medium.com/media/ef8375062a2912fdf3353c24ca491741/href">https://medium.com/media/ef8375062a2912fdf3353c24ca491741/href</a></iframe><p>Or if you prefer to read it let&#39;s begin our journey here.</p><p>Let’s say there is an evil AI that wanted to check if there is any human present on the planet.</p><p>if yes then return human present else return no human present, we can write it with a match expression like this.</p><pre>&lt;?php<br><br>class EvilAI{<br><br>public function checkHuman(){<br> $speciesPresentNow = &#39;human&#39;;<br> return match($speciesPresnetNow){<br>      &#39;human&#39; =&gt; &#39;human present&#39;,<br>      default =&gt; &#39;no species found&#39;<br>    };<br>  }<br>}</pre><p>now if we make an object of EvilAI and call its function checkHuman it will return us <em>human present.</em></p><pre>$evilAI = new EvilAI();<br>$result = $evilAI-&gt;checkHuman();</pre><p>There are some things to talk about it here.</p><p>First, the match expression uses === ( identity check), which means it will compare boolean with boolean and string with string, we can not mix different types.</p><p>so if(true === “true”) will not pass since one is of type boolean and one is of type string.</p><p>second, the <strong>default case </strong>match expression throws UnhandledMatchError exception if there is no default case provided, so if no default case then we will have to put it inside try catch and handle the exception by ourselves.</p><p>Now let&#39;s say this AI wanted to perform some action and call the nukeThePlanetAgain function if there are any humans found, we can write it like this.</p><pre>&lt;?php<br><br>class EvilAI{<br><br>public function checkHuman(){<br> $speciesPresentNow = &#39;human&#39;;<br> return match($speciesPresnetNow){<br>      &#39;human&#39; =&gt; $this-&gt;nukeThePlanetAgain(),<br>      default =&gt; &#39;no human species found&#39;<br>    };<br>  }<br><br>public function nukeThePlanetAgain(){<br>    return &#39;making sure no human survived&#39;;<br>  }<br>}</pre><p>match expression returns the value of whatever match is found so in the above case it will return <strong>making sure no human survived.</strong></p><p>Now the matching condition can also be a function and above can be rewritten like this.</p><pre>&lt;?php<br><br>class EvilAI{<br><br>public function checkHuman(){<br> return match(true){<br>      $this-&gt;areYouHuman() =&gt; $this-&gt;nukeThePlanetAgain(),<br>      default =&gt; &#39;no human species found&#39;<br>    };<br>  }<br><br>public function nukeThePlanetAgain(){<br>    return &#39;making sure no human survived&#39;;<br>  }<br><br>public function areYouHuman(){<br>    return true;<br>  }<br>}</pre><p>Look if we are returning true to an AI that is asking us that are we human then for sure we deserve to be nuked 😂.</p><p>We can also use multiple comma-separated calls which will work as or condition like this.</p><pre>&lt;?php<br><br>class EvilAI{<br><br>public function checkHuman(){<br> return match(true){<br>      $this-&gt;areYouHuman(),$this-&gt;isThisEarth() =&gt; $this-&gt;nukeThePlanetAgain(),<br>      default =&gt; &#39;no human species found&#39;<br>    };<br>  }<br><br>public function nukeThePlanetAgain(){<br>    return &#39;making sure no human survived&#39;;<br>  }<br><br>public function areYouHuman(){<br>    return true;<br>  }<br>public function isThisEarth(){<br>    return true;<br>  }<br>}</pre><p>we can also use comparisons for the same like this.</p><pre>&lt;?php<br><br>class EvilAI{<br><br>public function checkHuman(){<br> return match(true){<br>      $this-&gt;howManyTimesThisPlanetIsNuked() &lt; 5 =&gt; $this-&gt;nukeThePlanetAgain(),<br>      default =&gt; &#39;no human species found&#39;<br>    };<br>  }<br><br>public function nukeThePlanetAgain(){<br>    return &#39;making sure no human survived&#39;;<br>  }<br><br>public function howManyTimesThisPlanetIsNuked(){<br>    return 2;<br>  }<br>}</pre><p>let&#39;s be honest, no human can survive 5 times nuking of the entire planet 🤣</p><p><a href="https://www.php.net/manual/en/control-structures.match.php">There are plenty of other ways of using the match expression and I see a lot of its usage in the latest PHP packages, so please get yourself familiar with this function, you will thank me later.</a></p><p>Let me know if it helped you by clapping the article, Thanks for reading 🙏</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=9b1d0b53431d" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Write Cleaner Code With Laravel Tap Helper Function]]></title>
            <link>https://kumarravisingh.medium.com/write-cleaner-code-with-laravel-tap-helper-function-23c001faa044?source=rss-52277df79043------2</link>
            <guid isPermaLink="false">https://medium.com/p/23c001faa044</guid>
            <category><![CDATA[laravel]]></category>
            <category><![CDATA[helpers]]></category>
            <category><![CDATA[clean-code]]></category>
            <dc:creator><![CDATA[Kumar Ravi]]></dc:creator>
            <pubDate>Thu, 23 Feb 2023 07:04:33 GMT</pubDate>
            <atom:updated>2023-02-23T07:04:33.854Z</atom:updated>
            <content:encoded><![CDATA[<p>Laravel tap helper function provides you with a way of writing more cleaner and one-liner code.</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fwww.youtube.com%2Fembed%2FF5ZpnyQ8L3Y%3Ffeature%3Doembed&amp;display_name=YouTube&amp;url=https%3A%2F%2Fwww.youtube.com%2Fwatch%3Fv%3DF5ZpnyQ8L3Y&amp;image=https%3A%2F%2Fi.ytimg.com%2Fvi%2FF5ZpnyQ8L3Y%2Fhqdefault.jpg&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=youtube" width="854" height="480" frameborder="0" scrolling="no"><a href="https://medium.com/media/47a277401ee361d0670885391c578b36/href">https://medium.com/media/47a277401ee361d0670885391c578b36/href</a></iframe><p>Let&#39;s see with an example. Let’s say you need to write a function to save data into laravel Cache and access it later.<br>One way of doing this is like this</p><pre>&lt;?php<br><br>namespace App\Http\Controllers;<br><br>use Illuminate\Support\Facades\Cache;<br><br><br>class UserController extends Controller<br>{<br><br>    public function saveUserName($name){<br>        Cache::set(&#39;name&#39;,$name);<br>    }<br><br>    public function getUserName(){<br>        <br>        $userName = Cache::get(&#39;name&#39;);<br>                Cache:forget(&#39;name&#39;);<br>        return $userName;<br>    }<br><br>}</pre><p>Here in the first function, we are setting the user name; in the second function, we are getting that user name from the cache.<br>Let&#39;s write using it with the Tap helper function</p><pre>&lt;?php<br><br>namespace App\Http\Controllers;<br><br>use Illuminate\Support\Facades\Cache;<br><br><br>class UserController extends Controller<br>{<br><br>    public function saveUserName($name){<br>        Cache::set(&#39;name&#39;,$name);<br>    }<br><br>    public function getUserName(){<br><br>        return tap(Cache::get(&#39;name&#39;), fn()=&gt; Cache::forget(&#39;name&#39;));<br>    }<br><br>}</pre><p>Let&#39;s talk about the tap helper function, It has two parts one is the callback and the second part is the function it executes on that callback.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=23c001faa044" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Laravel Pennant Feature Flag package (Tutorial Part 1: Basics)]]></title>
            <link>https://kumarravisingh.medium.com/laravel-pennant-feature-flag-package-tutorial-part-1-basics-6b0947be50dc?source=rss-52277df79043------2</link>
            <guid isPermaLink="false">https://medium.com/p/6b0947be50dc</guid>
            <category><![CDATA[development]]></category>
            <category><![CDATA[features]]></category>
            <category><![CDATA[laravel]]></category>
            <category><![CDATA[php]]></category>
            <category><![CDATA[package]]></category>
            <dc:creator><![CDATA[Kumar Ravi]]></dc:creator>
            <pubDate>Wed, 22 Feb 2023 04:09:23 GMT</pubDate>
            <atom:updated>2023-02-22T04:12:34.848Z</atom:updated>
            <content:encoded><![CDATA[<p>Have you faced a scenario when you made some releases on production only to find you later that it needed some more changes?</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fwww.youtube.com%2Fembed%2F1r7aWBmvHB0&amp;display_name=YouTube&amp;url=https%3A%2F%2Fwww.youtube.com%2Fwatch%3Fv%3D1r7aWBmvHB0&amp;image=http%3A%2F%2Fi.ytimg.com%2Fvi%2F1r7aWBmvHB0%2Fhqdefault.jpg&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=youtube" width="854" height="480" frameborder="0" scrolling="no"><a href="https://medium.com/media/cf5314ef73f33e2903cc53b2901c175d/href">https://medium.com/media/cf5314ef73f33e2903cc53b2901c175d/href</a></iframe><p>You can watch this video tutorial to see the implementation of this post.</p><p>Sometimes we want changes to go slowly. Sometimes for a very small set of users, sometimes we are just testing new changes and want users randomly to test them.</p><p>Even let&#39;s say you made some release and now your users are shouting that this is not intended behaviour can we quickly turn that feature off somehow?</p><p>In a small system, when it is not constantly you can just make another release but as you acquire more and more users this remains not an option.</p><p>The new addition to the laravel core package solves all these problems with the newly added package Pennant, of course since it is not a small function but a whole package there is a lot to cover.<br>Let&#39;s try to understand the basics in the first part of this tutorial series.</p><p>The Setup:</p><p>I have created a public repository with all setup here if you want to try things out:</p><pre>https://github.com/thekrkv/amazon<br><br>Branch: Part1</pre><p>let&#39;s start:</p><p>First, let&#39;s add the Pennant package to our project:</p><pre>composer require laravel/pennant</pre><p>after this let&#39;s publish pennant configuration and migration files by running this command:</p><pre>php artisan vendor:publish --provider=&quot;Laravel\Pennant\PennantServiceProvider&quot;</pre><p><em>Now change your database credentials in your .env file and if you have already done that let&#39;s proceed to the next step.</em></p><p>Run migrate command:</p><pre>php artisan migrate</pre><p>Now, these steps can also be found on the official docs of laravel Pennant so if you want to read about the package first I will advise you to go threw them first so you have some understanding of the package basics. <a href="https://laravel.com/docs/10.x/pennant">Docs are Here</a></p><p>First, we need different types of users ( just for the scenario I am assuming where we have 3 types of users internal, business and normal.</p><p>Update your user&#39;s migration (at folder: database/migrations/) up() method like this</p><pre>public function up(): void<br>    {<br>        Schema::create(&#39;users&#39;, function (Blueprint $table) {<br>            $table-&gt;id();<br>            $table-&gt;string(&#39;name&#39;);<br>            $table-&gt;string(&#39;email&#39;)-&gt;unique();<br>            $table-&gt;timestamp(&#39;email_verified_at&#39;)-&gt;nullable();<br>            $table-&gt;string(&#39;password&#39;);<br>            $table-&gt;string(&#39;customer&#39;)-&gt;nullable();<br>            $table-&gt;rememberToken();<br>            $table-&gt;timestamps();<br>        });<br>    }</pre><p>Now edit your database seeder file like this:</p><pre>public function run(): void<br>    {<br><br>         User::factory(1)-&gt;create([<br>             &#39;email&#39; =&gt; &#39;test@amazon.test&#39;,<br>             &#39;customer&#39; =&gt; &#39;internal&#39;,<br>         ]);<br>        User::factory(1)-&gt;create([<br>            &#39;email&#39; =&gt; &#39;user@amazon.test&#39;,<br>            &#39;customer&#39; =&gt; &#39;normal&#39;,<br>        ]);<br>        User::factory(1)-&gt;create([<br>            &#39;email&#39; =&gt; &#39;business@amazon.test&#39;,<br>            &#39;customer&#39; =&gt; &#39;business&#39;,<br>        ]);<br>    }</pre><p>This will create 3 users with 3 different customers type, by default user factory has a password hash added for “password” so with test@amazon.test and password as “password” you will be able to login into your application.</p><p><strong>Till here we were doing some basic setups in our project if you have an application already and you want to apply some pennant logic to it, you can totally ignore some parts of it like editing migration and seed files.</strong></p><p>So now we have our migration setup, seed setup and pennant library ready to use in our application.</p><p>The first step is to have a condition or function or a scenario based on which you want your feature to be enabled.</p><p>For example, this can be like:</p><ol><li>A function in your model like if the user type is business you want some features not to be enabled to them by default</li><li>A condition based on the environment like if this app is in the testing environment you want some feature to be enabled</li><li>A feature based on payment or user type like if a user signed up after today you want them to be using v2 of your API with the latest feature</li></ol><p>These are some examples you can have any example based on your requirement on how you want to roll out your feature to your customers.</p><h3><strong>IMPORTANT NOTE: DO NOT ASSUME THIS PACKAGE TO ACT LIKE SOME ACCESS MANAGEMENT PACKAGE THIS BEHAVES VERY DIFFERENTLY AND SHOULD ONLY BE USED TO RELEASE NEW FEATURES</strong></h3><p>Let&#39;s add one condition in our User model to check if the user type is business.</p><p>open your User model and add this function</p><pre>public function isBusiness(){<br>      return  $this-&gt;customer == &#39;business&#39;;<br>    }</pre><p>this function will return true if the customer type is business else will return false. we will use this function in our feature registration.</p><p>Now open your AppServiceProvider file which is in app/providers directory and write this feature definition. your AppServiceProvider file should look like this:</p><pre>&lt;?php<br><br>namespace App\Providers;<br><br>use App\Models\User;<br>use Illuminate\Support\Lottery;<br>use Illuminate\Support\ServiceProvider;<br>use Laravel\Pennant\Feature;<br><br>class AppServiceProvider extends ServiceProvider<br>{<br>    /**<br>     * Register any application services.<br>     */<br>    public function register(): void<br>    {<br>        //<br>    }<br><br>    /**<br>     * Bootstrap any application services.<br>     */<br>    public function boot(): void<br>    {<br>        Feature::define(&#39;new-dashboard&#39;, fn (User $user) =&gt; match (true) {<br>            $user-&gt;isBusiness() =&gt; true,<br>            default =&gt; Lottery::odds(1/100),<br>        });<br>    }<br>}</pre><p>Notice here that I have used the classes at the top before calling their functions, many times when we start we forget to do that and our code stops working so make sure to use these classes like this at the top.</p><pre>use App\Models\User;<br>use Illuminate\Support\Lottery;<br>use Illuminate\Support\ServiceProvider;<br>use Laravel\Pennant\Feature;</pre><p><em>The “default =&gt; Lottary::odds(1 / 100)” is here to say that I will also return true for 1 out of 100 customers who are not business. What this means is in our 1 in 100 users will get an old dashboard even if they are not business, you can play with this by changing it, or you can just commit this fn part if all you want to do is have a lottery in all your users to decide who will have the feature, In such case, you can write your feature definition like this</em></p><pre>Feature::define(&#39;new-dashboard&#39;, Lottery::odds(1, 1000));</pre><p>Now that we have defined our feature we can use it in our application logic wherever we want. I am going to show one below more we will talk about in future posts when we will explore this package further.</p><p>Let&#39;s say that I want our business users to not have a new dashboard design since they have not been informed yet and we will do it only after we are sure that there is no bug on our new dashboard and our free users can be a ginny pig for testing our new feature. We can do that like this:</p><pre>&lt;?php<br><br>use App\Http\Controllers\ProfileController;<br>use Illuminate\Foundation\Application;<br>use Illuminate\Support\Facades\Route;<br>use Inertia\Inertia;<br>use \Laravel\Pennant;<br><br><br><br>Route::get(&#39;/&#39;, function () {<br>    if(Feature::active(&#39;new-dashboard&#39;)){<br>        return Inertia::render(&#39;Welcome&#39;, [<br>            &#39;canLogin&#39; =&gt; Route::has(&#39;login&#39;),<br>            &#39;canRegister&#39; =&gt; Route::has(&#39;register&#39;),<br>            &#39;laravelVersion&#39; =&gt; Application::VERSION,<br>            &#39;phpVersion&#39; =&gt; PHP_VERSION,<br>        ]);<br>    }else{<br>        return Inertia::render(&#39;NewWelcome&#39;, [<br>            &#39;canLogin&#39; =&gt; Route::has(&#39;login&#39;),<br>            &#39;canRegister&#39; =&gt; Route::has(&#39;register&#39;),<br>            &#39;laravelVersion&#39; =&gt; Application::VERSION,<br>            &#39;phpVersion&#39; =&gt; PHP_VERSION,<br>        ]);<br>    }<br>});</pre><p>Here inside if condition we are checking if the Feature new-dashboard is active which will check what the condition returns when registering in the AppServiceProvider class if it returns true we will return true and we will serve them the old Welcome page else we will return them NewWelcome page.</p><p>This can also be written like this</p><pre>&lt;?php<br><br>use App\Http\Controllers\ProfileController;<br>use Illuminate\Foundation\Application;<br>use Illuminate\Support\Facades\Route;<br>use Inertia\Inertia;<br>use \Laravel\Pennant;<br><br>Route::get(&#39;/&#39;, function () {<br>    if(Feature::active(&#39;new-dashboard&#39;)){<br>        return // whatever you want to return to business users<br>    }else{<br>        return // whatever you want to return to other users<br>    }<br>});</pre><p>This covers the basics of the Pennant package for managing releases, there is a lot to explore into it which we will talk about in the upcoming part of this series.</p><p>Feel free to follow me on various platforms to get updates:<br>Twitter: <a href="https://twitter.com/kumartheravi">@kumartheravi</a> <br>Linkedin: <a href="https://www.linkedin.com/in/kumarravisingh/">KumarRaviSingh</a><br>Github: <a href="https://github.com/kumarravisingh">KumarRaviSingh</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=6b0947be50dc" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Command Aliases, Let’s Save Time And Not Repeat Our Commands]]></title>
            <link>https://kumarravisingh.medium.com/command-aliases-lets-save-time-and-not-repeat-our-commands-858bef651e63?source=rss-52277df79043------2</link>
            <guid isPermaLink="false">https://medium.com/p/858bef651e63</guid>
            <category><![CDATA[alias]]></category>
            <category><![CDATA[laravel]]></category>
            <category><![CDATA[ubuntu]]></category>
            <category><![CDATA[bash]]></category>
            <dc:creator><![CDATA[Kumar Ravi]]></dc:creator>
            <pubDate>Mon, 20 Feb 2023 07:13:51 GMT</pubDate>
            <atom:updated>2023-02-20T14:16:16.481Z</atom:updated>
            <content:encoded><![CDATA[<p>You can watch this video if you want to see step by step execution of this tutorial.</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fwww.youtube.com%2Fembed%2FG1W3yloQmM0%3Ffeature%3Doembed&amp;display_name=YouTube&amp;url=https%3A%2F%2Fwww.youtube.com%2Fwatch%3Fv%3DG1W3yloQmM0&amp;image=https%3A%2F%2Fi.ytimg.com%2Fvi%2FG1W3yloQmM0%2Fhqdefault.jpg&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=youtube" width="854" height="480" frameborder="0" scrolling="no"><a href="https://medium.com/media/623e1a9dd6c977763c5c912053cbdc5d/href">https://medium.com/media/623e1a9dd6c977763c5c912053cbdc5d/href</a></iframe><p>or you can keep reading, totally up to you. 😄</p><p>Let’s take a scenario, you created a laravel project to work on and named it my-awesome-application</p><pre>composer create-project laravel/laravel my-awesome-application</pre><p>which will create a directory with name <strong>my-awesome-application</strong></p><p>Then you went inside that directory with the command</p><pre>cd my-awesome-application</pre><p>Then you ran a command to start some server there.</p><p>In my case I use the command:<strong> sail up -d</strong></p><p>but let&#39;s say you need the same command. Then we need to compile our JS libraries so we use one more command.</p><pre>npm run dev<br>or<br>yarn run dev</pre><p><em>I am leaving the npm install or yarn command since those are needed only once.</em></p><p>So now every time I start my computer I need to run this combination of commands.</p><pre>cd my-awesome-application<br>sail up -d<br>yarn run dev</pre><p><em>And you might need some more if you start exploring like cleaning docker images etc which is a discussion of a different day.</em></p><p>or assume any repetitive command in your development work.</p><p>you can shorthand this to just writing <strong>sma</strong> (start my application) or whatever shorthand you wish, totally up to you.</p><p>let&#39;s see how.</p><p>start terminal by pressing ctrl + shift + T<br><br>Then here type:</p><pre>nano ~/.bashrc</pre><p>Press Enter.<br>Now scroll all the way down with the down arrow key.<br>here at bottom of the file type:</p><pre>alias sma=&#39;cd ~/my-awesome-application &amp;&amp; sail up -d &amp;&amp; yarn run dev&#39;</pre><p>you can just add whatever combination of commands you need, just keep adding them with &amp;&amp;.</p><p>Then press CTRL + O (write out)</p><p>Press Enter</p><p>Then press CTRL + X ( To exit)</p><p>now in the terminal type this:</p><pre>source ~/.bashrc</pre><p>we are all set.<br>Now try your alias by typing <strong>sma</strong> in the terminal and pressing Enter.</p><p>you will see it executes your commands one by one.</p><p>now whenever you turn on your computer just type <strong>sma</strong> in the terminal press Enter and you are ready to code.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=858bef651e63" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Laravel Sail Stuck In npm: not found]]></title>
            <link>https://kumarravisingh.medium.com/laravel-sail-stuck-in-npm-not-found-40ee501ccabe?source=rss-52277df79043------2</link>
            <guid isPermaLink="false">https://medium.com/p/40ee501ccabe</guid>
            <category><![CDATA[laravel-issue]]></category>
            <category><![CDATA[laravel-development]]></category>
            <category><![CDATA[laravel-framework]]></category>
            <category><![CDATA[laravel-sail]]></category>
            <category><![CDATA[laravel]]></category>
            <dc:creator><![CDATA[Kumar Ravi]]></dc:creator>
            <pubDate>Thu, 02 Feb 2023 23:07:38 GMT</pubDate>
            <atom:updated>2023-02-07T18:13:57.589Z</atom:updated>
            <content:encoded><![CDATA[<p>I don’t want to waste your time but this issue is so unique that it needs some of your time or you will remain stuck in this issue forever and no one will help you since It happens on very few people&#39;s computers.<br>If you don’t believe me <a href="https://github.com/laravel/sail/issues/481">here is an ongoing discussion about it</a>.</p><p>If you want to save yourself from some trouble of figuring out and fixing this issue, here is a video on how to get Laravel Sail up and running in a newly installed Ubuntu 22.04 with Docker an Composer. I highly recommend you check this out.</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fwww.youtube.com%2Fembed%2FvdvpVrKP24s%3Ffeature%3Doembed&amp;display_name=YouTube&amp;url=https%3A%2F%2Fwww.youtube.com%2Fwatch%3Fv%3DvdvpVrKP24s&amp;image=https%3A%2F%2Fi.ytimg.com%2Fvi%2FvdvpVrKP24s%2Fhqdefault.jpg&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=youtube" width="854" height="480" frameborder="0" scrolling="no"><a href="https://medium.com/media/e7770a702a63d2d7bf39ef0b7a6f6ef1/href">https://medium.com/media/e7770a702a63d2d7bf39ef0b7a6f6ef1/href</a></iframe><p>So now that you are back and believe me I am going to take my time and after my solution, this issue will never happen again in any of your projects, It just vanishes in thin air.</p><p>First, tell me why the hell are you so unlucky or are you that daring that you decided to get the red pill? Yes, this issue is like taking that red pill and no one is gonna help you, wherever you will go for help they will say “Oh..but it is not happening in my system”</p><p>Anyways here is the solution</p><p>I have a fork of laravel sail so you will have to configure your project temporarily to use it, Yes temporarily, don’t worry, I got your back.<br>This is the fork: <a href="https://github.com/kumarravisingh/sail/tree/first-time-sail-up-npm-install-issue-fix-in-ubuntu-22"><strong>Here</strong></a></p><p>Here is how to configure your composer to use my fork rather than laravel/sail official repository code.</p><p><strong>Before that let&#39;s do some cleanup:</strong></p><ol><li><strong>clear your docker first so we have a clean slate to work with, follow these commands:</strong></li></ol><pre>docker system df<br>docker system prune -f<br>docker system prune --volumes -f<br>docker builder prune --all -f<br>docker image prune -f<br>docker image prune -a -f<br>docker volume prune -f<br>docker network prune -f<br>docker builder prune -f<br><br>run these commands one by one<br><br>docker system df <br>shows you status of your docker containers images volums etc<br>keep running these command one by one till you see all values<br>se to 0 as output of docker system df</pre><p>2.<strong> delete the docker-compose.yml your current sail has generated, or else you will keep scratching your head that what you did wrong, remember this is important. That’s why I am writing this in bold.</strong></p><p>open your composer.json and just above require add this code.</p><pre>&quot;repositories&quot;: [<br>    {<br>        &quot;type&quot;: &quot;vcs&quot;,<br>        &quot;url&quot;: &quot;https://github.com/kumarravisingh/sail.git&quot;<br>    }<br>]</pre><p>one more change needed in our composer file is where you mention which branch to use.<br>so in your comopser.json file, there is a require-dev section here you need to add this:</p><pre>&quot;laravel/sail&quot;: &quot;dev-first-time-sail-up-npm-install-issue-fix-in-ubuntu-22 as 1.19.0&quot;</pre><blockquote>Important: My branch name is<strong> first-time-sail-up-npm-install-issue-fix-in-ubuntu-22 But I added </strong><em>dev-</em><strong> at the begining <br>so my complete version name here became : </strong>dev-first-time-sail-up-npm-install-issue-fix-in-ubuntu-22</blockquote><p>So if you are still confused here is what the composer.json file will look like.</p><pre>{<br>    &quot;name&quot;: &quot;laravel/laravel&quot;,<br>    &quot;type&quot;: &quot;project&quot;,<br>    &quot;description&quot;: &quot;The Laravel Framework.&quot;,<br>    &quot;keywords&quot;: [&quot;framework&quot;, &quot;laravel&quot;],<br>    &quot;license&quot;: &quot;MIT&quot;,<br>    &quot;repositories&quot;: [<br>        {<br>            &quot;type&quot;:&quot;vcs&quot;,<br>            &quot;url&quot;: &quot;https://github.com/kumarravisingh/sail.git&quot;<br>        }<br>    ],<br>    &quot;require&quot;: {<br>        &quot;php&quot;: &quot;^7.3|^8.0&quot;,<br>        &quot;fruitcake/laravel-cors&quot;: &quot;^2.0&quot;,<br>        &quot;guzzlehttp/guzzle&quot;: &quot;^7.0.1&quot;,<br>        &quot;laravel/framework&quot;: &quot;^8.75&quot;,<br>        &quot;laravel/sanctum&quot;: &quot;^2.11&quot;,<br>        &quot;laravel/tinker&quot;: &quot;^2.5&quot;<br>    },<br>    &quot;require-dev&quot;: {<br>        &quot;facade/ignition&quot;: &quot;^2.5&quot;,<br>        &quot;fakerphp/faker&quot;: &quot;^1.9.1&quot;,<br>        &quot;laravel/sail&quot;: &quot;dev-first-time-sail-up-npm-install-issue-fix-in-ubuntu-22 as 1.19.0&quot;,<br>        &quot;mockery/mockery&quot;: &quot;^1.4.4&quot;,<br>        &quot;nunomaduro/collision&quot;: &quot;^5.10&quot;,<br>        &quot;phpunit/phpunit&quot;: &quot;^9.5.10&quot;<br>    },<br>    &quot;autoload&quot;: {<br>        &quot;psr-4&quot;: {<br>            &quot;App\\&quot;: &quot;app/&quot;,<br>            &quot;Database\\Factories\\&quot;: &quot;database/factories/&quot;,<br>            &quot;Database\\Seeders\\&quot;: &quot;database/seeders/&quot;<br>        }<br>    },<br>    &quot;autoload-dev&quot;: {<br>        &quot;psr-4&quot;: {<br>            &quot;Tests\\&quot;: &quot;tests/&quot;<br>        }<br>    },<br>    &quot;scripts&quot;: {<br>        &quot;post-autoload-dump&quot;: [<br>            &quot;Illuminate\\Foundation\\ComposerScripts::postAutoloadDump&quot;,<br>            &quot;@php artisan package:discover --ansi&quot;<br>        ],<br>        &quot;post-update-cmd&quot;: [<br>            &quot;@php artisan vendor:publish --tag=laravel-assets --ansi --force&quot;<br>        ],<br>        &quot;post-root-package-install&quot;: [<br>            &quot;@php -r \&quot;file_exists(&#39;.env&#39;) || copy(&#39;.env.example&#39;, &#39;.env&#39;);\&quot;&quot;<br>        ],<br>        &quot;post-create-project-cmd&quot;: [<br>            &quot;@php artisan key:generate --ansi&quot;<br>        ]<br>    },<br>    &quot;extra&quot;: {<br>        &quot;laravel&quot;: {<br>            &quot;dont-discover&quot;: []<br>        }<br>    },<br>    &quot;config&quot;: {<br>        &quot;optimize-autoloader&quot;: true,<br>        &quot;preferred-install&quot;: &quot;dist&quot;,<br>        &quot;sort-packages&quot;: true<br>    },<br>    &quot;minimum-stability&quot;: &quot;dev&quot;,<br>    &quot;prefer-stable&quot;: true<br>}</pre><p>Now I am hoping you deleted your docker-compose.yml file from your project root folder, I am counting on you don’t forget to do that.</p><p>now the fun part</p><p>cd to your project root folder.<br>if you run “pwd” command it should show you the path to your project.</p><pre>From here run this command<br><br>composer update<br><br>it will pull latest code from my fork on your project<br><br>php artisan sail:install <br><br>this will ask you to choose some options like this<br>[0] mysql<br>[1] pqsql<br>etc<br>enter 0 in your terminal and hit enter<br><br>your new docker-compose.yml file will get generated</pre><p>now the main part:</p><pre>Run this command now<br><br>vendor/bin/sail up -d<br><br>now wait till it starts It will take a little long to get up and running</pre><p>If you have followed it correctly so far entering localhost in your browser should take it to your project<br>if still you are not able to see it<br>sometimes default nginx might be running<br>so run this command</p><pre>sudo service nginx stop</pre><p>This will stop ubuntu’s nginx and now <br>localhost in your browser will show you laravle project homepage</p><p>if it is asking you for a key generation then run the following command</p><pre>php artisan key:generate</pre><p>I should declare it here that part of my solution contains making an entry at the end of your .env file with these two values:</p><pre>WWWGROUP=1000  <br>WWWUSER=1000</pre><p>so if you see this don’t get confused, some users face issues if their docker is running as root user in ubuntu so this is a solution for that.</p><p>Now after your project is running you can remove my changes from your composer.json file and change your library name back to</p><pre>&quot;laravel/sail&quot;: &quot;^1.0.1&quot;,</pre><p>And from now on you will not face this issue again. This is the uniqueness of this issue, once gon it is gon forever.</p><p>I hope some way I was able to help you and have saved you some hours.</p><p><strong>If yes then please consider buying me a coffee at my GitHub sponsor page:</strong> <a href="https://github.com/sponsors/kumarravisingh">Kumar Ravi Github Sponsor Page</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=40ee501ccabe" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Awesome World of Cloud, Containers, and Kubernetes]]></title>
            <link>https://kumarravisingh.medium.com/awesome-world-of-cloud-containers-and-kubernetes-a045353e9b13?source=rss-52277df79043------2</link>
            <guid isPermaLink="false">https://medium.com/p/a045353e9b13</guid>
            <category><![CDATA[kubernetes]]></category>
            <category><![CDATA[cloud-computing]]></category>
            <dc:creator><![CDATA[Kumar Ravi]]></dc:creator>
            <pubDate>Mon, 08 Mar 2021 10:16:24 GMT</pubDate>
            <atom:updated>2021-03-08T10:16:24.474Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*WGTA_AsvePOa3wxF2xVKYQ.png" /></figure><p>Do you know AWS made up a large portion of Amazon’s operating income, making it the key to the company’s overall profitability?<br>All thanks to the rise of the cloud and the flexibility it provides to large organizations in orchestrating their infrastructure.</p><p>Working with some of the major US clients I have seen firsthand how these organizations are able to quickly deploy and scrap environments on the cloud with help of automation tools like terraform, TeamCity, etc (which itself is a talk of another day).</p><p>It helps them in saving millions of dollars annually, Let’s take an example when we talk about large organizations, they are in the market for decades and have many libraries and small applications interacting with each other, when they deploy them with the help of cloud-based API and automation tools they have a freedom of taking them down and redeploying them whenever needed for different purposes, they can automate up and downtime based on need, e.g on the testing environment which is being used by day time only have no use of being online 24 hours they can be only up when testing is being done and scrapped after working hours, this processing power can be used somewhere else or if not It will save some money to the organization. And based on the complexity of applications and the number of services these applications use it can be a big amount If we look into the big picture.</p><p>Now we know that cloud is good and it saves millions to organizations in long run, next question is how much we can optimize these savings and how robust we can make these applications, decreasing our downtime to almost 0% or how close we can go to this benchmark, this is where containerization of application infrastructure comes in picture.</p><p>Let’s make it clear here that docker or Kubernetes can’t be learned by reading one article, what my aim here is to introduce you to these tools and give you a big picture and raise your interest, and since most of the technologies are large organization backed you can find tons of tools online and slowly grow your knowledge. Don’t get frustrated and be in hurry, learning docker or Kubernetes is a slow path and takes time.</p><p>The first question which every student should ask is why?<br>Although we have gained some basics information about the cloud let’s know why we need Kubernetes and containers when we have cloud instances on AWS or Google Cloud.</p><p>And the answer to this is flexibility, uptime, robustness, cost-saving, and utilization of resources.</p><p>Imagine that you have an application that you want to deploy on the cloud, so what do you need, In its simplest form, you need a Linux instance to host your server, you need some storage to save files and you need a database server to store the application data.</p><p>so<br>Instance: 4 GB RAM, Storage 16 GB, and a Different instance to run your Database server</p><p>You can always argue that why this or why not that but hey My job here is to give you a usability advantage and I will pick an example that suits my narrative. So If you have a different configuration in mind then good luck with that make your own example. e.g we can say why not run the database engine on the same instance and my answer to that is to say goodbye to scalability.</p><p>so let’s see how can we make a configuration which can contribute to zero downtime deployment means how to make sure my application stack is always UP? Let’s put a load balancer over this combination and scale this combination by 3. Yeyyy… we reached zero downtime, but wait… are we getting the best deal to our buck?</p><p>so let&#39;s see how can we make a configuration which can contribute to zero downtime deployment means how to make sure my application stack is always UP? Let&#39;s put a load balancer over this combination and scale this combination by 3. Yeyy we reached zero downtime, but wait are we getting the best deal to our buck?</p><p>I mean by 3 we are paying for 12 GB of RAM, 48 GB of storage, and 1 DB instance of whatever size we choose, <br>dang it… Let&#39;s do autoscaling so based on a certain amount of CPU and Memory usage we will decide how many instances to run which can not be applied to the database since it needs data consistency but let&#39;s leave that for a minute and say we can save in terms of ram and memory. You see we started hitting limitations even in our imaginary scenario.</p><p>But still is our 4 GB RAM and 16 GB storage server 24 hours at full throttle and its reserved capacity is in use always, almost always the answer is no, In day time usage might go at the higher side but at night even this server configuration is losing money, but we can’t take it down fully, who knows which of your customer will find it down for 1 second and go on to Twitter and cry about it. we are a large organization and can’t afford such negative publicity.</p><p>Look I am committed here to not pay a penny to Bezos more than he is providing me service of so let&#39;s think about something, how can we achieve such a level of flexibility. let&#39;s break it into smaller instances.</p><p>But how?<br>This is where docker comes into the picture.<br>what is it?<br>By official definition: Docker is a set of platforms as service products that use OS-level virtualization to deliver software in packages called containers.</p><p>Ok, so what is a container?</p><p>well, it&#39;s the smallest possible Linux kernel (mostly using alpine distro of Linux) with a size as small as 13 MB nothing else, rest you can install as needed.</p><p>so how can it help in our scenario?</p><p>Have you seen a beehive? If you deploy your application using Docker and Kubernetes that is what your application infrastructure is.</p><p>Let&#39;s talk technical and try to understand our components and wrap our heads around them.</p><p>In short, the smallest part of our infrastructure is containers but Kubernetes doesn’t count it as a single unit,</p><p>well then what is the smallest unit?<br>a collection of these containers which we will call pods. you see it&#39;s all a sci-fi movie here.</p><p>A pod is the smallest unit in Kubernetes term made up of multiple containers local to each other.</p><p>In the theoretical level above pods are nodes, now what is a node?<br>To understand that we will have to go one more level up which is called a cluster, <br>so what is a cluster?<br>A cluster is your entire collection of machines which can be like 5 machines with 3 GB Ram totaling 15 GB, and each of these machines is your node.<br>which pod will run on which node is decided by Kubernetes.</p><p>so now if we look again at our hierarchy, we have pods above which is a node, and above which is a cluster.</p><p>There are two more things that complete our theoretical discussion,</p><ol><li>Interaction with Kubernetes</li><li>Services to do different works like load balancing, networking, etc</li></ol><p>Let&#39;s talk about how to interact with Kubernetes?<br>For interaction we use kubectl, you see rather than providing any specific tools to manage your infrastructure with Kubernetes it gives you an API, now this API can be utilized by many tools, e.g in CLI(command-line interface) we have kubectl and in GUI we have Kubernetes dashboard and different companies can have different tools thanks to the API based communication approach. (An API is a request-response-based architecture used widely for web-based communication by applications mainly of two types REST and SOAP. Kubernetes uses REST, almost all your mobile applications are using API)</p><p>Now let&#39;s talk about services, to provide some other different tasks Kubernetes provide services, for example, we have LoadBalancer service which exposes your service and maps itself to cluster IP controlling your web traffic based on pod health and availability.</p><p>now with all these, we can decide the size of our application as small as 13mb+your code base and also decide what amount of RAM and processor you want to allocate to your containers based on your application requirements, and then just tell Kubernetes how many of pods you want to run.</p><p>now you have more control since these are the smallest possible units, let me help you in counting the flexibility</p><ol><li>we can decide how many machines cluster should run based on your current pod health and CPU or memory utilization which will save cost If not a lot of user then just run 1 node in the cluster and as the usage rise and pod number increases increase my cluster, you can set max and min in node count or pod count.</li><li>we can decide how to distribute your new changes, it provides you many ways of pushing your changes on your cluster by default it is a rolling update, and if the deployment fails your application won’t go down just one pod in many so no worries.</li><li>Till your pod is in a healthy state Kubernetes won’t send traffic to it, so no unhappy customer.</li><li>we can share storage between pods so cost saving here, if you run one Linux machine in AWS you have to allocate 16 GB of storage to it most of the time but here all your pods can share the same storage so more saving here</li><li>we have zero downtime since if any pod fail Kubernetes will delete it slowly and spinup new pod and you have to do nothing, your users are not gonna notice a thing.</li><li>And most important we are always using the maximum amount of server capacity that we paid for.</li></ol><p>There are many more advantages, a lot can be talked about Kubernetes and docker and there are numbers of books you can read to sharpen your knowledge, I would recommend you to go to the official documentation site provided by Google (<a href="https://kubernetes.io/docs/tutorials/">https://kubernetes.io/docs/tutorials/</a>) and do hands-on practice which is the best approach of learning it.<br>If you have any question you can reach out to me at<br><a href="http://kumarravi.me/">http://kumarravi.me</a><br>from here you can get my Twitter, Linkedin, and Github contact</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=a045353e9b13" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Docker Based Deployment  Of Legacy Application With Different PHP Versions In Windows/Mac]]></title>
            <link>https://kumarravisingh.medium.com/docker-based-deployment-of-legacy-application-with-different-php-versions-in-windows-mac-c9bb0fa17137?source=rss-52277df79043------2</link>
            <guid isPermaLink="false">https://medium.com/p/c9bb0fa17137</guid>
            <category><![CDATA[docker]]></category>
            <category><![CDATA[learn-php]]></category>
            <category><![CDATA[legacy-code]]></category>
            <category><![CDATA[php]]></category>
            <category><![CDATA[php-development]]></category>
            <dc:creator><![CDATA[Kumar Ravi]]></dc:creator>
            <pubDate>Sun, 23 Feb 2020 07:03:39 GMT</pubDate>
            <atom:updated>2020-02-23T12:06:43.301Z</atom:updated>
            <content:encoded><![CDATA[<h3>Docker Based Deployment Of Legacy Application With Different PHP Versions In Windows/Mac</h3><p>I recently saw a tweet saying we should consider changing the name of legacy application to <strong>revenue-generating application</strong> and, to be honest, it is a fact, no matter what we are working on the tech industry is constantly changing with new versions of packages coming every day. Whatever we are working on is going to become a legacy application someday and will have a different set of software and package requirement then the latest one installed in your system.</p><p><a href="https://github.com/laravel/framework/issues/27052">PHP 7.3 Compatibility Notice · Issue #27052 · laravel/framework</a></p><p><a href="https://github.com/laravel/framework/issues/27052">Take the example of Laravel 5.6</a>, So if we have one project in Laravel 5.6 and other in Laravel 5.4 or 5.5 we have to switch versions on our local system.</p><p>Now the question is how to keep our latest setup separate with all these different legacy applications and provide them their needed set of PHP and Package versions to run?</p><p><strong>To be clear here Laravel is a very mature framework right now and since it is latest, there are lots of option available to run different framework versions, For local deployment best one is </strong><a href="https://laradock.io/"><strong>Laradock</strong></a><strong>, What I am talking about is applications which are in core PHP 5.3 or have frameworks like Codeigniter older versions or Cake PHP Older versions.</strong></p><p>Today we will be looking into a solution to this problem for the windows/MAC operating system.</p><p>To begin If you have some basic understanding of what docker is and how it works it will be a lot easier for you to grasp If not then you do not have to worry we will go step by step doing exactly what needed to be done for getting your system ready.</p><p><strong>Docker Basics Detailing And Some Useful Commands:</strong> In a nutshell docker is a better version of VirtualBox providing you lightweight operating systems on your current OS which you can quickly destroy and modify based on your need and run your application inside them. Being a developer you must have heard about Virtualbox.</p><p>So in the virtualization concept, there is an image that you can download, customize them and build run OS (or containers) based on those images. From an expert perspective, there can be a lot of differentiation but for a starting point about docker, this is all that you need to know.</p><p>Now let’s get started with docker commands and practices, just the basics one’s.</p><ol><li>Install Docker for windows/Mac</li><li>Open PowerShell or Terminal</li><li>Run the command</li></ol><pre>docker run hello-world</pre><p>you will see docker downloading hello-world container image(since it doesn’t exist locally) and running it.</p><pre>Hello from Docker!</pre><pre>This message shows that your installation appears to be working correctly.</pre><p>Since after printing hello world the container exited you can see it by running the command:</p><pre>docker ps -a</pre><p>above command lists all running and stopped containers with their details.</p><figure><img alt="docker run hello-world output" src="https://cdn-images-1.medium.com/max/1024/1*YvjE3l0wW0LYsycoNdou_A.png" /></figure><p>Remember container is just a small OS running inside docker virtual environment.</p><p>So now we have reached a point where we know what container is and how to run it locally.</p><p>Now let’s get familiar with some small useful docker commands before we proceed further which will come handy in our journey.</p><ol><li>docker ps: It shows you your currently running containers</li><li>docker ps -a: It shows you all your running and stopped containers</li><li>docker stop &lt;container-id&gt;: It will stop the container whose ID you will pass <strong>(In the above screenshot you can see the container id in the leftmost column)</strong></li><li>docker rm &lt;container-id&gt;: It will remove the container whose id you will pass</li><li>docker rmi &lt;container-id&gt;: It will remove the image whose id you will pass</li><li>docker images: shows you all your local downloaded images based on which you can build an image, If you request a new image first it will be downloaded in your system then the running instance will be built on it. The difference between image and container is that an Image is just a description but a container is an actual object based on any image description which can be started and stopped.</li></ol><p>Although we won’t be using any image-related operations in our journey, still some knowledge might come in handy if you want to play around.</p><p>Now let’s start building our setup</p><ol><li>First, we need a project</li><li>Let’s create a project folder with a named legacy anywhere on our computer.</li><li>Let’s create an infrastructure folder in our legacy folder</li><li>Inside your infrastructure folder create a file with the name <strong>Dockerfile</strong></li><li>Write below code in your <strong>Dockerfile</strong></li></ol><pre>FROM orsolin/docker-php-5.3-apache</pre><pre>COPY . /var/www/html</pre><pre>COPY /infrastructure/start.sh  /usr/local/bin/</pre><pre>RUN chmod +x /usr/local/bin/start.sh</pre><pre>ENTRYPOINT service apache2 restart &amp;&amp; bash</pre><pre>RUN /usr/local/bin/start.sh</pre><pre>#<em> remove already running container</em></pre><pre>#<em> docker rm $(docker stop $(docker ps -q))</em></pre><p>Let&#39;s explain above file line by line</p><pre>FROM orsolin/docker-php-5.3-apache</pre><p>This is the location you are telling docker to pull image from, it is like a unique name same as your username on git / your public repository name. Docker hub follows the same for docker images. You can create your own image and place them on the docker hub and use them in your file. Be creative here and use an image that suits your project requirement, try to find an image with the setup as close as possible to your project configuration.</p><pre>COPY . /var/www/html</pre><p>here we are copying our parent project folder into the /var/www/html folder of the project, now this means you will not be able to make changes outside your container if you do this you will have to ssh inside your container and make changes there in the file. There is another way of just referencing your outside project inside your container image which I will talk about when we will start our container.</p><pre>COPY /infrastructure/start.sh  /usr/local/bin/</pre><p>We will be copying a start.sh file inside our container and running it when the project starts for setup purposes. In case we need additional programs to be installed in your container.</p><pre>RUN chmod +x /usr/local/bin/start.sh</pre><p>here we are changing the permission of copied start.sh, file to be executable.</p><pre>ENTRYPOINT service apache2 restart &amp;&amp; bash</pre><p>Here we are defining a command to start apache2 when the container starts</p><pre>RUN /usr/local/bin/start.sh</pre><p>After our container starts we are running our start.sh file.</p><pre><em>docker rm $(docker stop $(docker ps -q))</em></pre><p>This command is chaining of all the docker commands we need to use from our terminal, what it does is <br>* First takes all the running containers ID<br>* Stops them<br>* Then remove them</p><p>I have done this to make sure when you are running and making changes in your setup all the already containers get removed to avoid confusion.</p><p>The way docker workes is it utilizes a lot of caching features which makes it faster which is good for quick deployments but If I don’t disable it while we are doing our setup, sometimes in process of learning and trying different command we forget to remove cache and don’t see our changes in output and waste lot of time only to realize later that the cache needs to be cleared. <br>For small local setup, this none caching mechanism will work without wasting much time.</p><p>Now that we have our docker file ready, let&#39;s create a start.sh file which will be copied in our /usr/local/bin directory to run when the container starts.<br>For this create a start.sh file in the infrastructure folder.</p><pre>#!<em>/bin/bash</em></pre><pre>set -e</pre><pre>echo ‘welcome’ &gt;&gt; /var/www/html/welcome.php</pre><p>copy above code inside it, there is nothing much going on here just echoing welcome in a file and placing it in our html directory inside docker which we can test later when our container starts. You can add as many installation commands along with further customization.<br>just remember to add -y when you install a software in your start.sh file so that it doesn’t askes you for y/n each time.</p><pre>e.g apt-get install -y vim</pre><p>Now let&#39;s have a look at image creation and docker startup.<br>For this, we have two paths to continue.<br>1. .sh file: For Mac<br>2. .bat file: For windows<br>Let’s go with both one by one.</p><ol><li>Create a DeployLocal.sh File in your infrastructure directory</li></ol><pre>docker build -t legacy -f ../infrastructure/Dockerfile ../ --no-cache</pre><pre>docker run -ti -d -p 8765:80 legacy:latest /bin/bash</pre><pre>#<em>start firefox.exe http://localhost:8765/</em></pre><pre>#<em>start chrome http://localhost:8765/</em></pre><pre>open http://localhost:8765/ #<em> open default browser in Mac</em></pre><p>Copy the above code in that file.</p><p>Now let&#39;s explain our DeployLocal.sh file line by line.</p><pre>docker build -t legacy -f ../infrastructure/Dockerfile ../ --no-cache</pre><p>here we are building an image from our Docker file, this can be done faster if you remove —no-cache but for the first-time aspect and avoid confusion I have added that option.</p><pre>docker run -ti -d -p 8765:80 legacy:latest /bin/bash</pre><p>This command will start a container based on our latest image build.</p><p><em>As I was talking that in our Dockerfile copy command is copying our code to our docker container and we were not able to modify that file without going inside our container. What that means is you can comment that copy line and then add -v option in this run command and specify our local to container mounting like -v . : /var/www/html which will mount our project root to html folder. In such case this command will become:<br>docker run -ti -d -p 8765:80 -v .:/var/www/html legacy:latest /bin/bash</em></p><pre>open http://localhost:8765/</pre><p>This command will open our project root folder on the port we mapped in our docker run command.</p><p>2. Create a DeployLocal.bat File in your infrastructure</p><pre>docker build -t legacy -f ../infrastructure/Dockerfile ../ --no-cache</pre><pre>docker run -ti -d -p 8765:80 legacy:latest /bin/bash</pre><pre>::<em>start firefox.exe http://localhost:8765/</em></pre><pre>start chrome http://localhost:8765/</pre><pre>::<em>open http://localhost:8765/</em></pre><p>This is the same as our .sh file which we can run inside Powershell, just changing the way of opening the browser.</p><h3>Now let’s talk about some issues you might face on following this tutorial</h3><p>1. Port already in use issue in the terminal when running DeployLocal.bat or .sh: what this means is there is already a container running and using the port means you have run DeployLocal.bat or .sh twice without destroying the previous container. Just run below command on your terminal and deploy it again.</p><pre><em>docker rm $(docker stop $(docker ps -q))</em></pre><p>2. My application can’t access my localhost:3306 MySql: For this docker has provided URL’s for windows and Mac, what you need to do is on the place of localhost put below URL in your configuration file:<br><strong>in windows case: host.docker.internal<br>in Mac case: docker.for.mac.localhost</strong><br>and on the run time it will translate to the localhost of your host OS and you will be able to connect to your host machine localhost.</p><p><a href="https://github.com/kumarravisingh/docker-legacy">kumarravisingh/docker-legacy</a></p><p>I will be improving the code further Feel free to give PR or raise Issues on Github if you face any problem in running it locally or you have any suggestions, I will be more then happy to help you out further.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=c9bb0fa17137" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Easily Deploy Laravel Application Using Bitbucket Pipelines On Each Commit]]></title>
            <link>https://kumarravisingh.medium.com/easily-deploy-laravel-application-using-bitbucket-pipelines-on-each-commit-8161a60d79f5?source=rss-52277df79043------2</link>
            <guid isPermaLink="false">https://medium.com/p/8161a60d79f5</guid>
            <category><![CDATA[deployment]]></category>
            <category><![CDATA[forge]]></category>
            <category><![CDATA[bitbucket-pipelines]]></category>
            <category><![CDATA[laravel]]></category>
            <dc:creator><![CDATA[Kumar Ravi]]></dc:creator>
            <pubDate>Sat, 02 Feb 2019 05:05:50 GMT</pubDate>
            <atom:updated>2019-02-02T16:09:00.252Z</atom:updated>
            <content:encoded><![CDATA[<p>After wandering around the internet for hours, I was not able to find a simple tutorial for someone who is fairly new to Continuous deployment so I decided to implement it and write down the steps as I go.</p><p>This is not a full implementation which includes test and stuff, This is just optional of forge like deployment where you commit your code and it gets pushed on the server.</p><p>Goal: <br>1. Push your master branch on bitbucket</p><p>2. Run tasks on the server like:</p><p>git pull origin master</p><p>composer install</p><p>yarn</p><p>yarn run production</p><p>chmod 777 -R storage</p><p>chmod 777 -R bootstrap</p><p>And save as much time as we can since we only get 1 hour of free usage.</p><p>first, install node js in your droplet</p><pre>sudo apt-get install curl python-software-properties<br>curl -sL https://deb.nodesource.com/setup_11.x | sudo -E bash -</pre><pre>sudo apt-get install nodejs</pre><p>install yarn</p><pre>curl -sS https://dl.yarnpkg.com/debian/pubkey.gpg | sudo apt-key add -<br>echo &quot;deb https://dl.yarnpkg.com/debian/ stable main&quot; | sudo tee /etc/apt/sources.list.d/yarn.list</pre><pre>sudo apt-get update &amp;&amp; sudo apt-get install yarn</pre><p>install composer</p><pre>sudo apt-get install composer</pre><p>having node and yarn in your droplet is necessary for the deploy.sh file to run somehow it was not working by nvm’s node setup so make sure your install it with above commands.</p><p>install PHP and common extensions</p><pre>sudo add-apt-repository -y ppa:ondrej/php</pre><pre>sudo apt-get update</pre><pre>sudo apt-get install php7.2 php7.2-cli php7.2-common</pre><pre>sudo apt-get install php7.2-curl php7.2-gd php7.2-json php7.2-mbstring php7.2-intl php7.2-mysql php7.2-xml php7.2-zip</pre><p>also, install Git if you haven’t</p><p>first, you need to enable pipelines on your bitbucket account</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/359/1*XZcpjHCAffrk9SR0Axp-5w.png" /></figure><p>second, you have to create an ssh key for your pipelines so that your pipeline can connect to your server</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*d8nKsS6Jb9ZwnXawA4AEnQ.png" /></figure><p>copy your public key and paste into your droplet authorized_keys file.</p><p>you will find this file in the ~/.ssh folder of your digital ocean droplet or any other file depending on your hosting.</p><p>then enter your server IP in known hosts and click fetch, this will update the fingerprint from your server.</p><p>now our goal here is that we have to connect to our server and run a deploy.sh file each time we have a commit on our repo.</p><p>so let&#39;s create a deploy.sh, file in the root folder of our project</p><pre>#!/bin/sh<br><br># run deploy specific commands<br>git pull origin master &amp;&amp;<br>composer install &amp;&amp;<br>yarn &amp;&amp;<br>yarn run production &amp;&amp;<br>chmod 777 -R storage &amp;&amp;<br>chmod 777 -R bootstrap<br></pre><p>now for bitbucket to run pipelines on your project on each commit you need the bitbucket-pipelines.yml file in the root folder of your repository.</p><pre><strong>image</strong>: php:7.2-fpm<br><br><strong>pipelines</strong>:<br>  <strong>default</strong>:<br>    - <strong>step</strong>:<br>        <strong>script</strong>:<br>          - apt-get update -y<br>          - apt-get install -y ssh<br>          - ssh root@$serverIpAddress &#39;cd ~/your-project/ &amp;&amp; sh deploy.sh&#39;</pre><p>notice the $ sign before serverIpAddress you can create a variable in your pipeline setting and assign a value to that variable for better flexibility.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*xMH9OoamyYyDcbEYipAkIQ.png" /></figure><p>After this, you can make the commit on your project and it will deploy the code to your server.</p><p>This tutorial covers the tough part, of course, you can run PHPUnit and stuff by following bitbucket tutorial on laravel deployment here: <a href="https://confluence.atlassian.com/bitbucket/laravel-with-bitbucket-pipelines-913473967.html">https://confluence.atlassian.com/bitbucket/laravel-with-bitbucket-pipelines-913473967.html</a></p><p>what they don’t cover is the deployment part, rest you can find many tutorials on the web.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=8161a60d79f5" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>