<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Hazem Alabiad</title>
    <description>The latest articles on DEV Community by Hazem Alabiad (@hazemalabiad).</description>
    <link>https://dev.to/hazemalabiad</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F359625%2F8f6bfc47-d258-496d-846a-9a70da892d31.jpeg</url>
      <title>DEV Community: Hazem Alabiad</title>
      <link>https://dev.to/hazemalabiad</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/hazemalabiad"/>
    <language>en</language>
    <item>
      <title>"Hello World!" using Docker Containers</title>
      <dc:creator>Hazem Alabiad</dc:creator>
      <pubDate>Sat, 13 Mar 2021 13:13:41 +0000</pubDate>
      <link>https://dev.to/hazemalabiad/simple-hello-world-using-docker-containers-1lfl</link>
      <guid>https://dev.to/hazemalabiad/simple-hello-world-using-docker-containers-1lfl</guid>
      <description>&lt;h2&gt;
  
  
  What is a Docker container?
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;A standardized unit of software&lt;/strong&gt;, &lt;a href="https://www.docker.com/"&gt;Docker-website&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;It basically wraps our code-base and all required dependencies to run our application in an isolated environment that is independent of the currently running Operating System (OS) which enables cross-platform applications that run on top of what is called &lt;code&gt;Docker-engine&lt;/code&gt; as shown in the figure below:&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--f60ApE8I--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://www.docker.com/sites/default/files/d8/styles/large/public/2018-11/container-what-is-container.png%3Fitok%3Dvle7kjDj" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--f60ApE8I--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://www.docker.com/sites/default/files/d8/styles/large/public/2018-11/container-what-is-container.png%3Fitok%3Dvle7kjDj" alt="Containerized Apps"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h2&gt;
  
  
  Containers vs. VMs
&lt;/h2&gt;

&lt;p&gt;Containers give us all the features that a Virtual Machine (VM) provides but without the cost of running a whole OS, see the figure below:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--HRIgvrkB--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://www.docker.com/sites/default/files/d8/2018-11/docker-containerized-and-vm-transparent-bg.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--HRIgvrkB--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://www.docker.com/sites/default/files/d8/2018-11/docker-containerized-and-vm-transparent-bg.png" alt="Containers vs. VMs"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h2&gt;
  
  
  Dockerfile
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;It is a file that tells docker how a container will be built.&lt;/li&gt;
&lt;li&gt;It contains a list of instructions that are executed by Docker one by one from top to bottom.&lt;/li&gt;
&lt;/ul&gt;
&lt;h2&gt;
  
  
  Simple Container using Node.js
&lt;/h2&gt;

&lt;p&gt;Let us build a simple container using &lt;code&gt;Dockerfile&lt;/code&gt;. &lt;/p&gt;

&lt;p&gt;&lt;em&gt;Notes&lt;/em&gt;: &lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;The following steps are prepared to be working on Linux, macOS, WSL. &lt;/li&gt;
&lt;li&gt;I am supposing that docker is installed in your machine, check this &lt;a href="https://docs.docker.com/get-docker/"&gt;link&lt;/a&gt; for a more detailed explanation.&lt;/li&gt;
&lt;/ol&gt;
&lt;h3&gt;
  
  
  Steps
&lt;/h3&gt;

&lt;p&gt;1- Create a new directory in a workspace you select and nagivate to it by executing the following command:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;mkdir &lt;/span&gt;my-container &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="nb"&gt;cd &lt;/span&gt;my-container
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;2- Run the docker service in the OS&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;sudo &lt;/span&gt;service docker start
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;3- Create a Dockerfile&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;touch &lt;/span&gt;Dockerfile
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;4- Open your Dockerfile using your code editor, in my case, it is going to be VSCode:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight docker"&gt;&lt;code&gt;&lt;span class="c"&gt;# Pull ready-made node image from Docker Hub, using the format &amp;lt;image-name&amp;gt;:&amp;lt;image-version&amp;gt; &lt;/span&gt;
&lt;span class="k"&gt;FROM&lt;/span&gt;&lt;span class="s"&gt;  node:12-alpin&lt;/span&gt;

&lt;span class="c"&gt;# By default, Docker runs a container as a root user which is considered to be a security issue so, we need to run the commands as an unprivileged user whenever it is possible.&lt;/span&gt;
&lt;span class="c"&gt;# Node Image writers created a special user `node` for security purposes so, let us use it to run our commands within our container&lt;/span&gt;
&lt;span class="k"&gt;USER&lt;/span&gt;&lt;span class="s"&gt;  node&lt;/span&gt;

&lt;span class="c"&gt;# Set the working directory within the container&lt;/span&gt;
&lt;span class="k"&gt;WORKDIR&lt;/span&gt;&lt;span class="s"&gt;  /home/node/code&lt;/span&gt;

&lt;span class="c"&gt;# Copy the `index.js` to the working directory using permissions flag `--chown:&amp;lt;user&amp;gt;:&amp;lt;group&amp;gt;`&lt;/span&gt;
&lt;span class="k"&gt;COPY&lt;/span&gt;&lt;span class="s"&gt;  --chown=node:node  index.js  index.js&lt;/span&gt;

&lt;span class="c"&gt;# Run the command `node` with `index.js` as a parameter which means simple `node indexjs`&lt;/span&gt;
&lt;span class="k"&gt;CMD&lt;/span&gt;&lt;span class="s"&gt;  [  "node",  "index.js"  ]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;5- Create the &lt;code&gt;index.js&lt;/code&gt;file:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;touch &lt;/span&gt;index.js
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;6- Open &lt;code&gt;index.js&lt;/code&gt;using your favorite code editor&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;code index.js
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;7- Paste the code snippet, that simply print "Hello World!", within the file:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt;  &lt;span class="nx"&gt;http&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;require&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;http&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="nx"&gt;http&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;createServer&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;request&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;response&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;request received&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
        &lt;span class="nx"&gt;response&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;end&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Hello World!&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;utf-8&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="p"&gt;}).&lt;/span&gt;&lt;span class="nx"&gt;listen&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;3000&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;server started&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;8- Build the container and tag it. We tag a container to refer to it instead of containerID or easiness:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;docker build &lt;span class="nt"&gt;-t&lt;/span&gt; alpine-simple-app &lt;span class="nb"&gt;.&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--jY5W_Cdm--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://i.ibb.co/N983S5v/build.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--jY5W_Cdm--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://i.ibb.co/N983S5v/build.png" alt="docker build"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;9- Run the built container:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;docker run &lt;span class="nt"&gt;--init&lt;/span&gt; &lt;span class="nt"&gt;--rm&lt;/span&gt; &lt;span class="nt"&gt;-p&lt;/span&gt; 3000:3000  alpine-simple-app
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;--rm&lt;/code&gt; tells Docker to automatically delete the container when it exists&lt;br&gt;
&lt;code&gt;--init&lt;/code&gt; indicates that an init process should be used as the PID 1 in the container "used to stop the container when hitting &lt;code&gt;Ctrl+c&lt;/code&gt;", see &lt;a href="https://github.com/krallin/tini"&gt;link&lt;/a&gt; for more details.&lt;br&gt;
&lt;code&gt;-p&lt;/code&gt; publishes the container's port to the host's machine &lt;code&gt;&amp;lt;Docker_host_port&amp;gt;:&amp;lt;container_port&amp;gt;&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--mEePkDXf--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://i.ibb.co/0ct3Yn0/run.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--mEePkDXf--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://i.ibb.co/0ct3Yn0/run.png" alt="docker run"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;10-  Go to your browser and open the URL:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;[localhost:3000](http://localhost:3000/)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;You should see the following message:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--07fh-WKL--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://i.ibb.co/5j86xmj/image.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--07fh-WKL--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://i.ibb.co/5j86xmj/image.png" alt="hello-world"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;It is DONE! You were able to run a &lt;code&gt;Node.js&lt;/code&gt; app within a container!&lt;br&gt;
I hope that my post was helpful and enjoyable! Special thanks go to &lt;a href="https://twitter.com/holtbt"&gt;Brian Holt&lt;/a&gt; for his great explanation. &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Note&lt;/strong&gt;: All Figures are credit to &lt;a href="https://www.docker.com/"&gt;Docker&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;For further questions do not hesitate to contact me on &lt;a href="https://twitter.com/hazem_white"&gt;Twitter&lt;/a&gt;.&lt;/em&gt;&lt;/p&gt;

</description>
      <category>container</category>
      <category>docker</category>
      <category>node</category>
    </item>
  </channel>
</rss>
