<?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 Federico Pugliese on Medium]]></title>
        <description><![CDATA[Stories by Federico Pugliese on Medium]]></description>
        <link>https://medium.com/@federicopugliese?source=rss-ee8a6ca563f7------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*ADZ835wOgHdWq_0ElTprIA.jpeg</url>
            <title>Stories by Federico Pugliese on Medium</title>
            <link>https://medium.com/@federicopugliese?source=rss-ee8a6ca563f7------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Wed, 18 Nov 2026 01:04:21 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@federicopugliese/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[Import Your Own Python Code Without Pythonpath Tricks]]></title>
            <link>https://levelup.gitconnected.com/import-your-own-python-code-without-pythonpath-tricks-9068495c1bba?source=rss-ee8a6ca563f7------2</link>
            <guid isPermaLink="false">https://medium.com/p/9068495c1bba</guid>
            <category><![CDATA[software-development]]></category>
            <category><![CDATA[technology]]></category>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[tech]]></category>
            <category><![CDATA[python]]></category>
            <dc:creator><![CDATA[Federico Pugliese]]></dc:creator>
            <pubDate>Wed, 10 Feb 2021 16:55:16 GMT</pubDate>
            <atom:updated>2021-02-22T01:13:40.405Z</atom:updated>
            <content:encoded><![CDATA[<h4>With a single one-liner and proper code structure</h4><blockquote><strong>“I can’t even import my own scripts!”</strong></blockquote><blockquote><strong>“But it worked on my laptop!”</strong></blockquote><p>Yes, the god of Suffering has many faces. <em>The last one is my favorite.</em></p><p><strong>Let me explain the issue.</strong><br>Your colleague has given you these wonderful, brand-new Python scripts to <em>end all the injustices in the world</em>. You are ready to go, eager to run them, but <strong>you can’t import any of their functions and classes</strong>.</p><p><strong>Not only you can’t import them from one script to another,</strong> but you can’t import them from your running Python interpreter as well.<br><strong>Or maybe you can,</strong> but only if <em>Jupyter </em>and Saturn are aligned. In that same folder. Or like a <em>PyCharm</em>. <strong>And nobody else can do the same</strong>.</p><p>So you <em>(or your kind colleague for you)</em> have chosen the patchy way.</p><p><em>The Way of Suffering.</em></p><p><strong>You have modified your </strong>PYTHONPATH<strong>. </strong>Or similar. You were forced to do so. <strong>And it’s a mess.</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*O_FcY4gAcwsUWNNF" /><figcaption>Let’s try to get it sorted. Photo by <a href="https://unsplash.com/@noorsethi?utm_source=medium&amp;utm_medium=referral">Noor Sethi</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><h3>The import chaos for your code</h3><p>As you might have noticed, I’m talking about the <strong>chaos that Python imports </strong>can generate. Or, better, the chaos that <strong>messy configuration </strong>can generate.</p><p>Because imports have no faults on their own, <em>poor little creatures</em> <em>(that’s not true, I do have some things to say on that… but not now).</em></p><p><strong>The real issue is the way they are used or supposed to be used.</strong></p><h3>From Tricks to Ghosts</h3><p>I’ve seen several hacks to avoid this import issue. <br>Just some special mentions here,<strong> to underline why they are bad in general </strong>and what risks they pose.</p><blockquote><strong>If you just want to know the final working solution, without knowing why it is better than these tricks, skip this part!</strong></blockquote><h4>The Ghost of PYTHONPATH</h4><p>The first one is the quickest: altering the PYTHONPATH environment variable.</p><p>Basically, it works like the system path variable.<strong> It tells Python where to search for additional scripts and modules.</strong> You have just to add the path of your containing folder in its list of values, and you are ready to go.</p><p><em>Nice trick.</em></p><p>But then you have to share your code, even with just your collaborators, or you have to move it onto other machines for some reason. <strong>And nothing works anymore.</strong></p><p><strong>Every single time</strong>, you have to set the<strong> </strong>PYTHONPATH<strong> manually.</strong> You have to write this warning everywhere, in caps lock, within your README.</p><p><em>This Ghost will haunt you forever.</em></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*osgNLR1arUzJkRS5" /><figcaption>The PYTHONPATH ghost. Photo by <a href="https://unsplash.com/@grimmwwald?utm_source=medium&amp;utm_medium=referral">Syarafina Yusof</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><p>Furthermore, you <strong>can’t create a generally-working script</strong> to avoid this, because you don’t know where your other colleagues could put this code, so you <strong>don’t know the absolute path</strong> to specify.<br>But ok, maybe it’s my DevOps side to talk, or my <em>what’s-this-please-automate-everything</em> paranoia. I admit it.</p><p>There’s another and worse issue, too. Maybe you <strong>exported </strong>that PYTHONPATH definition in your config files because you have no intention to set it every time you open a shell or IDE. <br>However, now you are working with<strong> several, distinct projects </strong>and the <strong>definitions start to conflict. There is no separation.</strong></p><p><em>Ouch.</em></p><p>I’m not saying it’s totally wrong <em>(or I am? — cough)</em>.<br><em>It surely works in simple cases</em>, but <strong>isn’t there another solution working in general?</strong> Maybe something that could be <strong>automated </strong>and could <strong>run everywhere</strong>?</p><blockquote>Besides, did you notice the “additional” definition above?<br>“<strong>It tells Python where to search for <em>additional </em>scripts and modules.”</strong><br>It comes from <a href="https://docs.python.org/3/using/cmdline.html#envvar-PYTHONPATH">Python documentation</a>.</blockquote><blockquote>How could my code be an <strong><em>addition</em></strong>? It is the main code to run!<br>Not to mention that, on that very page, there is also a <a href="https://docs.python.org/3/using/cmdline.html#command-line-and-environment">warning</a> about different behaviors of Python’s alternative implementations — altough I never noticed distinctions in this particular case.</blockquote><h4>The Master-Ghost of sys.path</h4><p><em>“But I can automate this configuration! I can modify </em><em>sys.path in my scripts!”</em></p><p>But… <em>Why?</em></p><p>Besides having almost the same issues as the previous Ghost, <strong>this solution is mixing application code with system configuration code</strong>.</p><p><em>When something smells bad, it’s often rotten.</em> This case isn’t different. Mixing scopes is always a bad idea.</p><p>Furthermore, you are <strong>playing with environment variables at runtime</strong>. <strong>Why waiting so long</strong> to set up your code? Setup is the <em>first thing to do</em>, not the last one! It makes you aware of errors as soon as possible, to exclude the following components from the list of possible causes.</p><p>And what happens when you have to fork your process, or when you use a library that does it without warning you? <strong>Is it inheriting these environment variables? Do you have to worry about it <em>every single time</em>?</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*hprMFTNLvo1I4o6x" /><figcaption>Let’s spot the right solution. Photo by <a href="https://unsplash.com/@cobblepot?utm_source=medium&amp;utm_medium=referral">Kit Suman</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><h3>A Solution in Plain Sight</h3><p>If you pay close attention, you will see it.<br>See the way you have to <strong>import functions and classes from other libraries</strong>, I mean. Besides the fact they work seamlessly, they work everywhere, with no PYTHONPATH changes.</p><p><strong>So why would you use a different mechanism?</strong></p><p>They work because these packages are put under the site-packages folder, which is inspected by Python by default.<br><strong>Now the point is: who the hell is moving them there?</strong></p><p>The answer is simple. <strong>It is your </strong>pip install <strong>command </strong>(or equivalent).</p><p>Yes, I’ll focus on the pip case where you also have a <strong>virtual environment</strong>. But of course it is the same for conda envs. <br><em>This is not the right place to list the advantages of having a virtual environment, right?</em></p><h3>Installing your own code</h3><p>So the idea is to install your code along with your other packages. This means that your code has to be structured as a package as well.</p><p><strong>Do not despair, it is very easy! </strong>Just keep in mind two things.</p><h4><strong>(1) You just have to collect your code in at least a folder with an empty </strong><strong>__init__.py file</strong>.</h4><p>These files are essential to mark their folders as <strong>Python modules. </strong>In this way, they are recognized for the installation (and therefore imports). They are mandatory.</p><p>Suppose your package is justice:</p><pre>justice\<br>    ...<br>    innerfolder\<br>        <strong>__init__.py</strong><br>        some_script.py<br>        ...<br>    ...<br>    <strong>__init__.py</strong><br>    one_of_your_scripts.py<br><strong>setup.py</strong></pre><p>So, an __init__.py for each folder.</p><h4><strong>(2) There is also another essential piece: the </strong><strong>setup.py file.</strong></h4><p>The setup.py file is searched and used by the pip install &lt;package&gt; command <strong>by default</strong>. It tells several things: metadata about your package, where to find scripts and <em>(optional but suggested)</em> what are the requirements.</p><pre>from setuptools import setup, find_packages</pre><pre># List of requirements<br>requirements = []  # This could be retrieved from requirements.txt</pre><pre># Package (minimal) configuration<br>setup(<br>    name=&quot;justice&quot;,<br>    version=&quot;1.0.0&quot;,<br>    description=&quot;End world injustices&quot;,<br>    packages=find_packages(),  # __init__.py folders search<br>    install_requires=requirements<br>)</pre><p>Writing the requirements there makes your pip install &lt;package&gt; command <strong>install your package dependencies as well</strong>. <em>Yes, just with a single command! </em>No need to run pip install -r requirements.txt too. Double benefit!</p><blockquote>There is a lot to say here, also about distributions (source, wheel). Maybe in the next article!</blockquote><p>So everything you need to do now is to install your code by using pip install &lt;path_to_setup.py_folder&gt;, or in this case: pip install .</p><p>Wow. Is everything alright? <strong>Just with that?</strong></p><p>Yeah, almost.</p><p>Because, actually, <strong>you can’t do that for your code</strong>. This installation would copy your scripts into the site-packages folder, and that’s it. What if your code changes?<strong> It is in development</strong>, so this is very likely. <strong>You would have to install it every time.</strong> Not handy at all.</p><h4>That’s why you should use<strong> the single, one-liner savior:</strong></h4><pre>pip install -e .</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*nTxbjgEeToSXM047" /><figcaption>This is the real trick! Photo by <a href="https://unsplash.com/@acharki95?utm_source=medium&amp;utm_medium=referral">Aziz Acharki</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><h3>The development mode install</h3><p>The -e option tells pip to install it in “development mode”. Basically, it puts <strong>just a reference </strong>to your package in the site-packages folder. So you can change it without worrying about installation anymore!</p><p><strong>You just have to do this once and for all, </strong>even if you change your code.</p><p>If you installed your package in the virtual environment, <strong>you can just import your code from anywhere</strong>, and from any shell, provided you activated that virtualenv.</p><p>For instance:</p><pre>from justice import one_of_your_scripts<br><br>one_of_your_scripts.a_function()</pre><p>And most importantly, <strong>you can run that exact installation command anywhere.</strong> Because it works for your colleague, too. You do not have to worry about the path of the installation. <em>That means easy automation.</em></p><p><strong>Quick, clean, general.</strong></p><p><strong>Everything a real solution should be.</strong></p><h3>Resources</h3><p><a href="https://docs.python.org/3/using/cmdline.html#envvar-PYTHONPATH">[1] Python documentation</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=9068495c1bba" width="1" height="1" alt=""><hr><p><a href="https://levelup.gitconnected.com/import-your-own-python-code-without-pythonpath-tricks-9068495c1bba">Import Your Own Python Code Without Pythonpath Tricks</a> was originally published in <a href="https://levelup.gitconnected.com/">Level Up Coding</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Stop this Microservices Madness]]></title>
            <link>https://medium.com/swlh/stop-this-microservices-madness-8e4e0695805b?source=rss-ee8a6ca563f7------2</link>
            <guid isPermaLink="false">https://medium.com/p/8e4e0695805b</guid>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[software-development]]></category>
            <category><![CDATA[microservices]]></category>
            <category><![CDATA[technology]]></category>
            <category><![CDATA[software-engineering]]></category>
            <dc:creator><![CDATA[Federico Pugliese]]></dc:creator>
            <pubDate>Tue, 19 Jan 2021 18:15:55 GMT</pubDate>
            <atom:updated>2021-02-23T12:15:50.835Z</atom:updated>
            <content:encoded><![CDATA[<p>I’m a big fan of Microservices.<br>Does it sound weird, given the title? Well, it should not.</p><p>Every now and then, a new paradigm or technology arises. It brings hope and hype. <em>This will save us all!<br></em>Yeah, it’s true. Maybe it is ground-breaking. Extremely beneficial. <strong>However, like everything in life, every solution has its scope</strong>.</p><p>Have you ever tried to apply Newtonian laws to subatomic particles? Or, <strong><em>have you ever tried to squeeze a television into a sock</em></strong>? Pretty much the same here. <br>Hype makes you forget this little clause at the end of the contract. And results are catastrophic.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*LFne5OH9NMGI29f6" /><figcaption>Proper usage of socks and televisions. Photo by <a href="https://unsplash.com/@molliesivaram?utm_source=medium&amp;utm_medium=referral">Mollie Sivaram</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><h3>Splitting is the way</h3><p>In a normal web application, you have a single process serving all requests. Well, you can replicate it for balancing reasons, but in any case there’s a single package/software you have to run. And it bundles all the logic together.</p><p>When you use Microservices, instead, you have <strong>several, distinct processes</strong>, each one serving just a subset of requests. Each process runs a distinct package, with just the code it needs.</p><h3>Is it really new?</h3><p>No, it isn’t.</p><p>The <strong>Microservice pattern is nothing new</strong>, actually, like almost every emergent design (besides it has been around for some years).<strong> <br>It is Object-Oriented Programming applied on a higher level: application architecture.</strong><br>No coincidence that it often makes use of HTTP APIs, particularly RESTful, which are in turn OOP principles applied to API design. <br><em>P.S. I hate acronyms.</em></p><p>Let me explain.</p><p>When you create a program using Object-Oriented Programming, you split it into several pieces, <em>Classes</em>. Each Class represents a component of the solution and has a specific role. It also works just on its own subset of data. <em>Does it sound familiar?</em></p><blockquote>This is not the right place to discuss about Object-Oriented Programming in general, but I have to point out some aspects. If you are not familiar with it, you can reference <a href="https://en.wikipedia.org/wiki/Object-oriented_programming">online documentation</a> and, soon, my other article about it!</blockquote><h3>The Two Principles — The Good Tailor</h3><p>When you design Classes, you must follow two main principles: <strong>loose coupling and high cohesion.</strong></p><blockquote>To be honest, these should be the guiding stars of each software project, not just OOP ones. But that’s another story. Or is it?</blockquote><p><a href="https://en.wikipedia.org/wiki/Cohesion_(computer_science)"><strong>High cohesion</strong></a><strong> </strong>is the situation where each component is strongly consistent. All its data and methods refer to a very specific area, or meaning, or role.</p><p><a href="https://en.wikipedia.org/wiki/Loose_coupling"><strong>Loose coupling</strong></a><strong> </strong>is the situation where each component has the least possible amount of interactions with other components. It can work on its own, hiding low-level details, and exposes the essential interfaces.</p><p><strong>For Microservices it is the same. <br></strong>I can’t stress this enough. <em>A good OOP design leads to an optimal Microservice design. </em>There is no middle ground.</p><p>This should be almost enough to answer why and when to use Microservices. But since there is more, let’s break it down.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*P3Z_s-fBBdHi5oqK" /><figcaption>Be a Good Tailor. Loosely sew highly-entangled (coherent) fabrics. Photo by <a href="https://unsplash.com/@jeffwade?utm_source=medium&amp;utm_medium=referral">Jeff Wade</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><h3>When to split?</h3><p>This is exactly as it is for Object-Oriented Programming.</p><p>When you can respect <strong>high cohesion</strong> and <strong>loose coupling</strong>.</p><p><strong><em>No differently.</em></strong></p><p>You know how to split code into Classes in the right way. Why would you split your Microservices randomly?<br>If a bad Class diagram is painful to maintain and manage, because of the sparsity of code in different files, think of randomly split Microservices application, where you have different <em>processes </em>altogether.</p><h3>But why on Earth would I split my application into sub-processes?</h3><p>Imagine how much it’s easy to make two Objects interact. You just have to call an Object’s method and you are good to go. All within a single program.</p><p>Microservices, instead, live in different processes. Possibly, on different machines.<strong> You have to communicate over a network, using APIs.</strong></p><p><strong>It adds complexity! <br></strong>You must have excellent reasons to do so. <br><strong>Not for trend, not for fun</strong>. Because, I assure you, if you use Microservices randomly, their management is <em>not fun at all</em>.</p><p>Actually, you must have the same reasons you’d have when you decide to use APIs. It’s the same.</p><p><strong>APIs let you hide all the implementation details behind them. </strong>That’s essential in some cases, and it offers incredible benefits.</p><h4>1. Scalability for non-uniform traffic</h4><p>Scenario: you have a Supermarket application. <br>You have a Stock Microservice, that just shows you the amounts of articles in stock, and a Vision Microservice, which uses GPUs to detect your articles given a picture of them.</p><p>If you receive <strong>thousands of calls for your Stock APIs </strong>and just a bunch of requests for your Vision APIs, you could<strong> just replicate your Stock Microservice</strong>.</p><p>No need to double your GPU resources!</p><p>Think of it. If you kept everything in a single machine or application, and you still wanted to scale to match all the incoming requests, you would have to replicate it entirely. A waste.</p><h4>2. Error resilience</h4><p>Suppose you have a Bank application. <strong>Your Transfer Microservice keeps crashing</strong>, maybe for a temporary error or, worse, for a newly introduced bug <em>(damn untested releases…).</em></p><p><strong>Why should the entire Bank application be down?</strong> Maybe some customers wouldn’t even notice, because they just want to see their movements or they want to use their cards.</p><h4>3. Separate deployments</h4><p>Similarly to the reason above, if you have a new feature to add or a bug to fix, you can <strong>just deploy the right Microservice</strong>. Your build and deployment times should be smaller.</p><p>Also, you could put your Microservices on different machines, in different locations, maybe.</p><h4>4. Complete isolation</h4><p>Maybe you need complete isolation between Microservices. In this way, you can use <strong>different DBs</strong> (SQL and noSQL) or different technologies altogether. This will give you complete freedom!<br>And it is of course strictly related to the other points as well, about error resilience or requirements.</p><h4>5. Different requirements</h4><p>Do you have a heavy computation to perform? Maybe a <strong>Machine Learning </strong>one, where you need GPUs, their libraries… <br>Maybe that part would be much better in <strong><em>Python</em></strong>, while the other one could benefit from <strong><em>Java</em></strong>. <br>Or maybe libraries for the same language would create some <strong>conflicts</strong>, but you desperately need two specific versions for two distinct tasks.<br>And finally, perhaps you want to use two different Cloud products to host your two distinct pieces.</p><p>There are plenty of reasons, some more common than others.</p><p>In those cases, split the application!<br>Probably it’s easier. <strong>But remember to respect the Two Principles! Be a Good Tailor,</strong> or you would have hard times sharing data between your Microservices…</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*vm-MCaL0fcjQgLZ1" /><figcaption>Even the highest of Monoliths is made by blocks. It might be atoms, but it is. Photo by <a href="https://unsplash.com/@zoltantasi?utm_source=medium&amp;utm_medium=referral">Zoltan Tasi</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><h3><strong>Is it not your case? Go for a Monolith.</strong></h3><p>If your system is tightly coupled, use a single application. A Monolith.</p><p>You’ll have cleaner dependency management, and you won’t need complex <strong>orchestrations </strong>or <strong>distributed systems</strong> to trace errors, share data, collect logs, sync calls, secure network interactions… and I can go on.</p><h4><strong>Is Object-Oriented Programming not suitable for your application architecture? Or, better, is RESTful design not suitable? Microservices won’t be either!</strong></h4><p>If your application serves a <strong>long sequence of API calls</strong>, maybe because an API requires the result of the previous ones, this is not ideal. It would introduce also <strong>network delays</strong>.<br>If your application is a <strong>single procedure</strong> that requires user interaction, well, the sentence is self-explaining. It is a Procedure! <br>You have to <strong>share a state </strong>between calls, each component is<strong> not independent </strong>and a single error in the sequence will <strong>block it entirely</strong>. There is hardly a benefit in splitting the application.</p><h3>And remember, Monolith doesn’t mean Mess</h3><p><em>Monolith</em>.</p><p><em>Mess</em>.</p><p>They are also spelled differently.</p><p><strong>Someone sees Monolith as a bunch of unordered code.<br></strong> They claim that Microservice pattern is the only way to have splits, order and all.</p><p>Well, I’ll reveal something.</p><p><strong>It’s — not — true.</strong></p><p>Your code design does not depend on process divisions. Object-Oriented Programming doesn’t either. If you split your code right, if you manage your dependencies in a clear hierarchy, as well as your files, <strong>you can achieve the same level of cohesion and decoupling.</strong></p><p>And another secret: if you divide your APIs into separate packages/modules/controllers/blueprints/<em>whatever </em>in your code, then you can decide to serve them in a single process or split them in Microservices at build-time, or at run-time too, for free.</p><p><em>Wow.</em></p><h3>Conclusions</h3><p>I’m a big fan of Microservices, as I said.</p><p>If used in the <strong>right context</strong>, for the <strong>right reasons</strong>, they <strong>solve issues and improve performances or costs.</strong></p><p>But please, stop making them the one and only solution for all problems.<br>Because they can also be the root of all evil.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=8e4e0695805b" width="1" height="1" alt=""><hr><p><a href="https://medium.com/swlh/stop-this-microservices-madness-8e4e0695805b">Stop this Microservices Madness</a> was originally published in <a href="https://medium.com/swlh">The Startup</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Ok, But… What is Docker?]]></title>
            <link>https://medium.com/swlh/ok-but-what-is-docker-150253785341?source=rss-ee8a6ca563f7------2</link>
            <guid isPermaLink="false">https://medium.com/p/150253785341</guid>
            <category><![CDATA[containers]]></category>
            <category><![CDATA[technology]]></category>
            <category><![CDATA[docker]]></category>
            <category><![CDATA[software-development]]></category>
            <category><![CDATA[software-engineering]]></category>
            <dc:creator><![CDATA[Federico Pugliese]]></dc:creator>
            <pubDate>Thu, 14 Jan 2021 18:59:10 GMT</pubDate>
            <atom:updated>2021-01-22T12:16:47.973Z</atom:updated>
            <content:encoded><![CDATA[<p>You have read the documentation. And again, after marvelous promises, incredible descriptions, <em>you know nothing</em>. Back to square, still drawing a blank.<br>The <em>“Ok, but… what is it?” </em>series tries to answer questions on products and libraries <em>honestly</em>, without your so-beloved catchy slogans. And it does so on three different levels of complexity.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*OqKc6_CGceHO3T64" /><figcaption>Please do not sink your Docker containers. Photo by <a href="https://unsplash.com/@enginakyurt?utm_source=medium&amp;utm_medium=referral">engin akyurt</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><h3>Containers, Images…</h3><p>Yes, we are talking about <a href="https://www.docker.com/">Docker</a>. And no, no Pods, or Clusters, that’s Kubernetes.</p><h4>This is the official answer:</h4><blockquote>We help developers and development teams build and ship apps.</blockquote><p>Well, thanks.<br>Docker is many things, and unfortunately you will see this term used to mean all of them. Let’s get it sorted.</p><p><strong>Level [1] — <em>The Baker (Simple — I just want a grasp)</em></strong><br><strong>Level [2] — <em>The Code (Intermediate — I want the basics)<br></em>Level [3] — <em>The Engineer (Detailed — I have the right background)</em></strong></p><h3><strong>[1] The Baker</strong> — Run once, run everywhere</h3><p>Docker lets you package a software to respect this philosophy: <em>“run once, run everywhere”</em>. Well, it is not magical on its own, but it is a good way to force you to do everything you should to make this happen.</p><p><strong>Imagine a pastry chef has to make a cake. </strong><br>Yes, same old metaphor, but with a grain of difference. <br><strong><em>That chef has to bake on a remote, unknown island. </em></strong><br>And you have to make sure he/she can.<br>The chef will be gracefully shipped to the island in a <strong>giant box</strong>, along with all the things he/she needs. You have to fill the box.</p><blockquote>Holy — <br>Ok, let’s start with a TO-DO list.</blockquote><blockquote>Well, of course we’ll need some eggs, flour, sugar… and all the rest. And the recipe. That’s perfect. Good.<br>Are we done?<br>Oh, yes, cups and bowls to mix the ingredients. And also the spoons, whisks, the flan pan… and the kitchen scale.<br>But is it a desert island? We’ll need an oven! And a timer.<br>…Wait, we’ll need an electric generator too, with its fuel!</blockquote><blockquote>Uh, right, the chef! Good luck!</blockquote><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*NAuFPu3GlVs81Jqm" /><figcaption>Oh dear, I forgot the table… Photo by <a href="https://unsplash.com/@fotolancaster?utm_source=medium&amp;utm_medium=referral">Pablo Lancaster Jones</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><h4><strong>A lot of things, right?</strong></h4><p>So, in this metaphor, you are the programmer. <br>The chef is the code, performing a task.<br>The island is the machine (computer, server…) where the code will run.</p><p>You have soon acknowledged <strong>that code on its own is useless without all the tools it requires to run</strong>. <em>Sorry, chef.</em><br>You need a certain Operating System, some libraries, files in the right positions… Yes, you could <em>manually </em>and <em>separately </em>configure them on the target machine. Or, simply, you could use <strong>Docker to pack them together</strong> in an image, deliver it to the server and forget about the rest. Well, at least that machine should have Docker installed, to interpret the packaging, but that’s very easy to remember.</p><p>The box where you put everything is the <strong>Docker image</strong>. When this image needs to run, a <strong>Docker container </strong>is created from it. The image is at rest, the container is at runtime. Terminology.</p><p>Does it matter now what is the island, what resources it has already? Of course not. You can ship the box anywhere, and you will have everything you need, whether you want to make a cake in Honolulu or Rapa Nui.</p><p>That’s what Docker primarily is: <strong>an application to package and run software everywhere.</strong></p><h3>[2] The Code — A Dockerfile to rule them all</h3><p>Do you remember the TO-DO list we prepared for the pastry chef? That’s the <strong>Dockerfile</strong>. It is the sequence of operations to be performed to start (almost) from scratch and collect all the requirements for an application.</p><p>Does it remind you of something? Of course it does. When you want to run an algorithm, you write the proper code to accomplish your task.<br>This is the same. You write the sequence of steps to achieve your goal — preparing the environment to run your application.<br>Nothing new. You are just automating a task.<br>It is a way to <strong>code the required configuration for a program</strong>.</p><p>Yes, we, software engineers, like to code and automate everything. Even if it means to stack layers of code upon layers of code.<br>Actually, <em>particularly if it means so</em>.</p><h4>It goes this way:</h4><p>1. You <strong>write a Dockerfile;</strong><br>2. You <strong>build </strong>the Docker image using the Dockefile;<br>3. You <strong>move </strong>your Docker image where you need it;<br>4. You <strong>instantiate a running</strong> Docker container from that Docker image.</p><p>This is an example of Dockerfile, so that we can understand it better. It is a Dockerfile to run a Python application.<br><strong>No worries</strong>, you don’t have to know Python.</p><pre><strong>FROM</strong> python:3.7.4-slim</pre><pre><em># Pre-install requirements<br></em><strong>COPY</strong> requirements.txt .<br><strong>RUN</strong> pip install -r requirements.txt</pre><pre><em># Prepare package installation copying sources<br></em><strong>COPY</strong> setup.py .<br><strong>COPY</strong> package package/</pre><pre><em># Install the package<br></em><strong>RUN</strong> pip install .</pre><pre><em># Define the default command to be run when running a container<br></em><strong>ENTRYPOINT</strong> [&quot;python&quot;, &quot;-m&quot;, &quot;package&quot;]</pre><p>You can see several kinds of instructions. These are the very basic ones.<br><strong>Every instruction is a step, executed one after the other</strong> <strong>when we build</strong> <strong>the Docker image</strong> using this Dockerfile.</p><pre><strong>FROM</strong> python:3.7.4-slim</pre><p>Docker is smart enough to let you start not from scratch, but something more. In this case, we are using a pre-built Docker image as a starting point that is publicly available on <a href="https://hub.docker.com/_/python">DockerHub</a>. <br>This image already has Python (along with Pip, the package manager to install dependencies). Handy!<br>Otherwise, we should have installed Python manually. Not a tragedy, but still.</p><pre><strong>COPY</strong> requirements.txt .</pre><p>This is to copy a file or a folder that is<strong> in your filesystem</strong> to a specific directory <strong>inside the Docker image</strong>. The syntax is simple: first the source, then the destination.<br>As you can see, we are copying the file with the list of requirements into the Docker image. By default, the path is relative to the folder where you run the Docker build command. <br>Then, we are using the “.” to mean the working directory in the Docker image.</p><pre><strong>RUN</strong> pip install -r requirements.txt</pre><p>This is to run a <strong>shell command</strong> inside the Docker image, to modify it. Well, sort of, but you asked for an intermediate answer!<br>In this case, we are installing all the requirements listed in that file. <br>Did you notice? We are using now the file that is inside the Docker image! Its path is relative to the image’s working directory.<br>We couldn’t have run this command without having copied the file first.</p><p>…Then we copy the source code of our package, which is in the <em>package </em>folder to avoid any <em>where-has-my-package-gone</em> panic, we install it in the image and then…</p><pre><strong>ENTRYPOINT</strong> [&quot;python&quot;, &quot;-m&quot;, &quot;package&quot;]</pre><p>We define the default command to be run when we create a container from this image. In this case, we’ll run the <em>main </em>of our package.</p><p>That’s it! Now we could execute the <strong>Docker build to make the image.</strong><br>In this way, we have packaged everything, once and for all, and now we just have to carry our image (the box in the previous metaphor) in the right place, then run it as a container. <br>A command like <em>docker run</em>. And nothing else.</p><p>But stop, too much code. Stop.<br>This is a <em>What is Docker</em>, not a <em>How to use Docker</em>.</p><p>And the answer is <strong>“an application to package your requirements and code in a image, once, and then run it countless of times and everywhere easily”.</strong> <br>It’s like a brute force approach. We stick everything we need, once and for all, so that we do not worry anymore, anywhere else. No need to re-download the requirements, re-install the package or Python itself.</p><h3>[3] The Engineer — Virtualization</h3><p>I know you are there. <br>I can sense you. You are the system engineer. This seems just black magic to you. You want to know what is really happening. What Docker really is.</p><p><strong>Docker is a lightweight virtualization tool</strong>. Do you remember the old-fashioned virtual machines? We are close. The concept is similar. <br>Docker<em> simulates an Operating System</em>, along with a file system, on top of another one. VMs simulate <em>hardware </em>instead, so they must have an entire OS in them. That’s why Docker images are lighter.</p><p><strong>A Docker image is a precise snapshot </strong>of an OS, with its file system (and files). <strong>A Docker container is a sort of little virtual machine</strong> whose specifications are the ones of the image, <strong>in which</strong> <strong>a process is running</strong>. It might be the default process specified by the <em>ENTRYPOINT </em>instruction or it might be another. The point is the same: a container runs as long as that process runs.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*vdIwnlJXq9MG0OAa" /><figcaption>Are we inside a Docker container? Photo by <a href="https://unsplash.com/@modernafflatusphotography?utm_source=medium&amp;utm_medium=referral">Ash from Modern Afflatus</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><p>Just like a real virtual machine <em>(sorry, not sorry)</em>, <strong>the Docker container is by default isolated from the hosting OS</strong>. You cannot reference files in the host, for instance, or write there.<br>However, with a proper configuration, you can <strong>map ports</strong> (linking a real port in the hosting OS to a port of the container) or <strong>mount volumes </strong>(to read from or write to the real file system). In this way you can run a web application or a general algorithm, maybe a test or a build that has to output a result, and see it from the outside.<br>You can do what you want. You could also, for instance, run an interactive bash. In that case, it would be as if you were connecting to a remote machine, but a virtual one, where you can execute “manual” commands in a pre-configured environment (specified by the image).</p><blockquote><strong>Oh, and just a final note. </strong>Because I know that some of you are thinking about this.</blockquote><blockquote><strong><em>Yeah, yeah, magnificent and all, but what about executables? What about compiled programs? They do run as standalone applications without Docker.</em></strong></blockquote><blockquote>That’s true. Some programs can run without any other dependency. But things may get harder in case you need configuration files or use interpreted languages, or external, heavy libraries, DLLs, environment variables, <em>whatever-you-want</em>. <br><strong>Docker lets you run your application everywhere without a <em>README</em></strong><em> </em>with the tedious “get this driver installed”, “move in this folder and run that”, “pray that it works”. <br>Also, you can run this application on <strong>your PC</strong>, then on <strong>Google Cloud</strong>, or maybe <strong>AWS</strong>, or on your super-secret <strong>server in your private network</strong>. All with a blink of an eye.</blockquote><blockquote>If you think about it, it is also a good way to pseudo-document all the steps required for your application to work. That’s because you are forced to write down all the instructions and operations you have to perform to make it happen. Win-win.</blockquote><blockquote><strong>You can also spawn many containers starting from a single image, with possibly different processes</strong>. Maybe you have built an image with all the libraries you need, and then you have to run several different tasks separately, but requiring the same dependencies. Or you have to replicate your microservice… but that’s another world. <strong>Docker is also an orchestrator for containers</strong>, if you want, although usually other solutions like Kubernetes are employed. However, they are based on Docker technology anyway.</blockquote><h4><strong>So, dear engineer, what is Docker?</strong></h4><p>Many things. It is an <em>application </em>to build portable code boxes, it is an <em>orchestrator </em>for containers, it is a <em>lightweight virtualization tool</em>. When you read <em>“Docker”</em>, the meaning of this word is usually one of these. <br>And to mess it up, you can also hear, improperly<em>, “I’ve built a docker”</em>, meaning a Docker image, or <em>“Run that docker”</em>, meaning a Docker container.</p><p>I hope this clarifies a bit instead.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=150253785341" width="1" height="1" alt=""><hr><p><a href="https://medium.com/swlh/ok-but-what-is-docker-150253785341">Ok, But… What is Docker?</a> was originally published in <a href="https://medium.com/swlh">The Startup</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>