<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Docker - Moses Dinakaran</title>
	<atom:link href="https://mosesdinakaran.in/category/tutorials/devops/docker/feed/" rel="self" type="application/rss+xml" />
	<link>https://mosesdinakaran.in</link>
	<description>Tutorials and Documentations on Magento</description>
	<lastBuildDate>Wed, 10 Jan 2024 12:48:42 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.8.3</generator>
	<item>
		<title>Virtualization &#038; Docker Fundamentals for Beginners</title>
		<link>https://mosesdinakaran.in/virtualization-docker-fundamentals-for-beginners/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=virtualization-docker-fundamentals-for-beginners</link>
					<comments>https://mosesdinakaran.in/virtualization-docker-fundamentals-for-beginners/#respond</comments>
		
		<dc:creator><![CDATA[mosesdinakaran@gmail.com]]></dc:creator>
		<pubDate>Thu, 23 Feb 2023 08:17:54 +0000</pubDate>
				<category><![CDATA[DevOps]]></category>
		<category><![CDATA[Docker]]></category>
		<category><![CDATA[Tutorials]]></category>
		<guid isPermaLink="false">https://mosesdinakaran.in/?p=1621</guid>

					<description><![CDATA[<p>Why Docker Docker primarily helps in 2 scenarios. Local Development Lets say if you want to install Magento, First you have to install the below applications as per the system requirement PHP Nginx MySql Elasticsearch RabbitMq etc To do so you have the look for all the respective installation steps and install it one by [&#8230;]</p>
<p>The post <a href="https://mosesdinakaran.in/virtualization-docker-fundamentals-for-beginners/">Virtualization & Docker Fundamentals for Beginners</a> first appeared on <a href="https://mosesdinakaran.in">Moses Dinakaran</a>.</p>]]></description>
										<content:encoded><![CDATA[<h3 class="wp-block-heading">Why Docker</h3>



<p>Docker primarily helps in 2 scenarios.</p>



<h5 class="wp-block-heading">Local Development </h5>



<p>Lets say if you want to install Magento, First you have to install the below applications as per the system requirement </p>



<ul class="wp-block-list"><li>PHP</li><li>Nginx</li><li>MySql</li><li>Elasticsearch</li><li>RabbitMq </li><li>etc</li></ul>



<p>To do so you have the look for all the respective installation steps and install it one by one, may be not so tough. But consider you are working on 2 projects where the first project requires php7.4 and the second one required php8 you will require 2 php version running on the same machine  This is also possible but you will definitely spend more time on installing and configuring the application rather than coding.</p>



<h5 class="wp-block-heading">Deployment</h5>



<p>Lets say as a team you are supposed to develop a application in PHP7.4 and one of your team member by mistake has installed PHP8  and unknowingly he had used a PHP8 method which will obviously work in his Local environment. but when this code get pushed to the server which will be in php7.4 it will break. So to avoid this kind of failures instead of just moving the source code why not move php7.4 as well, Good Idea theoretically but how its possible in real time ? </p>



<p>The question that immediately comes in your mind is that php is an application that needs to be installed we cannot copy this kind of application from one serve to another. Its kind of possible in a different way and this is where docker &amp; VM comes in to picture.</p>



<h3 class="wp-block-heading">What is Docker</h3>



<p>Docker is a software application that performs operating system level virtualization, In other words Docker is a tool that allows you to install/deploy applications in an efficient way. To know how docker works we need to first understand about virtualization.</p>



<h3 class="wp-block-heading">Virtualization</h3>



<p>Letts say you have bought a powerful desktop with 32GB Ram 4TB hard disk and 6Core Processor on seeing that your son and daughter also asks for a separate system for them. Since you have already spend a lot of money on your system you cant afford to spend on 2 more new systems but also you cant say no to your kids. In this case, wouldn't be nice if you could just buy 2 monitors and connect them to your CPU so now you have 3 computes where you can save money and also make your kids happy as well <img src="https://s.w.org/images/core/emoji/16.0.1/72x72/1f642.png" alt="🙂" class="wp-smiley" style="height: 1em; max-height: 1em;" /> Of course it would be nice but how its possible  ?</p>



<p>Virtualization makes this possible, Its a technique of splitting the physical hardware in to a separate logical hardware and in those logical hardware we can install a separate OS </p>



<figure class="wp-block-image size-large is-resized"><img fetchpriority="high" decoding="async" src="https://mosesdinakaran.in/wp-content/uploads/2023/02/image-6-1024x594.png" alt="" class="wp-image-1624" width="768" height="446" srcset="https://mosesdinakaran.in/wp-content/uploads/2023/02/image-6-1024x594.png 1024w, https://mosesdinakaran.in/wp-content/uploads/2023/02/image-6-300x174.png 300w, https://mosesdinakaran.in/wp-content/uploads/2023/02/image-6-768x445.png 768w, https://mosesdinakaran.in/wp-content/uploads/2023/02/image-6.png 1238w" sizes="(max-width: 768px) 100vw, 768px" /></figure>



<h3 class="wp-block-heading">Hypervisor</h3>



<p>This virtualization is done using a software called hypervisor. A hypervisor is a type of computer software, firmware (A<em> low-level software that sits between the hardware and the OS</em>)  that creates and runs virtual machines. </p>



<p>There are two types of hypervisor</p>



<h5 class="wp-block-heading">Type 1:</h5>



<div class="wp-block-columns is-layout-flex wp-container-core-columns-is-layout-9d6595d7 wp-block-columns-is-layout-flex">
<div class="wp-block-column is-layout-flow wp-block-column-is-layout-flow" style="flex-basis:66.66%">
<ul class="wp-block-list" id="block-8f207437-8d2c-4c9e-9615-79f14d16e56c"><li>A Type 1 hypervisor&nbsp;runs directly on the underlying computer's physical hardware, interacting directly with its CPU, memory, and physical storage.</li><li>Also called as bare metal hypervisor because its installed on top of hardware even before installing any OS</li><li>This hypervisor itself will behave as a OS. Ex VMware EXSI</li><li>Usually Type 1 hypervisor are used in Big Big Enterprise and Data centers we will not use this in our laptop or day to day development activities.</li></ul>
</div>



<div class="wp-block-column is-layout-flow wp-block-column-is-layout-flow" style="flex-basis:33.33%">
<figure class="wp-block-image"><img decoding="async" src="https://mosesdinakaran.in/wp-content/uploads/2023/02/image-10.png" alt=""/></figure>
</div>
</div>



<h5 class="wp-block-heading">Type 2 : </h5>



<div class="wp-block-columns is-layout-flex wp-container-core-columns-is-layout-9d6595d7 wp-block-columns-is-layout-flex">
<div class="wp-block-column is-layout-flow wp-block-column-is-layout-flow" style="flex-basis:66.66%">
<ul class="wp-block-list"><li>A Type 2 hypervisor, is installed as a software application on an existing operating system. With this software we can create any no of Virtual Machines with its own operating system.</li><li>Few examples of type 2 hypervisor are VMware Workstation, Oracle VirtualBox, WSL2</li><li>It doses not have direct access to the hardware but each VM is allocated a dedicated hardware.</li><li>These VM1 and VM2 can be packaged as a whole (OS + PHP + Nginx + ES + etc + Magento) which is called as VM Image.</li><li>This VM Image can be moved to PROD or TEST server and can be booted.</li><li>So Instead of Just moving the Source Code, we can now move the complete OS + Software with the help of VM's</li></ul>
</div>



<div class="wp-block-column is-layout-flow wp-block-column-is-layout-flow" style="flex-basis:33.33%">
<figure class="wp-block-image size-full"><img decoding="async" width="311" height="399" src="https://mosesdinakaran.in/wp-content/uploads/2023/02/image-15.png" alt="" class="wp-image-1637" srcset="https://mosesdinakaran.in/wp-content/uploads/2023/02/image-15.png 311w, https://mosesdinakaran.in/wp-content/uploads/2023/02/image-15-234x300.png 234w" sizes="(max-width: 311px) 100vw, 311px" /></figure>
</div>
</div>



<h3 class="wp-block-heading">Virtual Machine Drawbacks</h3>



<p>Though VM's solves our problem of moving the complete OS form one place to another it also gives us the below issues.</p>



<h5 class="wp-block-heading">Resource Allocation</h5>



<ul class="wp-block-list"><li>Every VM that you create in your machine has to be allocated a separate RAM, Processor, Storage, So the more VM you create you system will get slow down.</li><li>In Every VM we should install a separate OS, This will result in consuming more storage space and also cost.</li></ul>



<h5 class="wp-block-heading">Package  Size  (VM Image)</h5>



<p>When you create an package(VM image) of the VM the OS within the VM will also get included which will result in huge size this will increase the time for creating the image and will become inconvenient in moving the image from one server to the another</p>



<p>This is where docker comes in to picture, where it solves these problem.</p>



<h3 class="wp-block-heading">Docker Containers vs Virtual Machines</h3>



<div class="wp-block-columns is-layout-flex wp-container-core-columns-is-layout-9d6595d7 wp-block-columns-is-layout-flex">
<div class="wp-block-column is-layout-flow wp-block-column-is-layout-flow" style="flex-basis:66.66%">
<p>To know the difference lets first look at how an Operating System is made up of.  An Operating System has 2 Layers OS Kernel and the Application Layer. OS Kernel is the one that will communicate with the hardware of the system and the applications layer is build on top of the Kernel Layer.</p>



<p>In Linux operating system there are hundreds of distributions such as centos, ubuntu etc They all look different in terms of GUI and the application they have in it. But under the hood they uses the same Linux kernel.</p>
</div>



<div class="wp-block-column is-layout-flow wp-block-column-is-layout-flow" style="flex-basis:33.33%">
<figure class="wp-block-image size-full"><img decoding="async" width="516" height="287" src="https://mosesdinakaran.in/wp-content/uploads/2023/02/image-18.png" alt="" class="wp-image-1643" srcset="https://mosesdinakaran.in/wp-content/uploads/2023/02/image-18.png 516w, https://mosesdinakaran.in/wp-content/uploads/2023/02/image-18-300x167.png 300w" sizes="(max-width: 516px) 100vw, 516px" /></figure>
</div>
</div>



<p>As we know Docker and Virtual Machine are both virtualization tools. But they differ in terms of which layer of the operating system they virtualize. Virtual Machine virtualizes the complete OS (OS Kernal + Application) In terms of Docker it virtualize only the application layer of the guest OS. </p>



<figure class="wp-block-table is-style-stripes"><table class="has-fixed-layout"><thead><tr><th>Docker Container</th><th>Virtual Machine</th></tr></thead><tbody><tr><td>No Separate OS required, Uses the OS Kernel of the Host Operating System.</td><td>A Complete OS will be installed in all the VM's</td></tr><tr><td>No Dedicated hardware </td><td>Dedicated hardware such as RAM, Storage Size, Processor needs to be assigned for each VM</td></tr></tbody></table></figure>



<h3 class="wp-block-heading">Docker Containers</h3>



<p>Docker containers are advanced version of Virtualization. Docker will not create virtual machines but it will create containers which does the same job as virtual machine but in a better way .</p>


<div class="wp-block-image">
<figure class="aligncenter size-large is-resized"><img loading="lazy" decoding="async" src="https://mosesdinakaran.in/wp-content/uploads/2023/02/image-20-1024x688.png" alt="" class="wp-image-1645" width="768" height="516" srcset="https://mosesdinakaran.in/wp-content/uploads/2023/02/image-20-1024x688.png 1024w, https://mosesdinakaran.in/wp-content/uploads/2023/02/image-20-300x202.png 300w, https://mosesdinakaran.in/wp-content/uploads/2023/02/image-20-768x516.png 768w, https://mosesdinakaran.in/wp-content/uploads/2023/02/image-20.png 1071w" sizes="auto, (max-width: 768px) 100vw, 768px" /></figure></div>


<p>Think of a container as a small, lightweight box that contains everything your application needs to run, including the code, system tools, dependent application such as php, mysql, nginx etc, and the settings. This means that you don't need to worry about installing and configuring all the dependencies on each machine where you want to run your application.</p>



<p>Containers can be as small as 10MB and you can easily&nbsp;<a href="https://phoenixnap.com/kb/docker-memory-and-cpu-limit" target="_blank" rel="noreferrer noopener">limit their memory and CPU usage</a>. This makes containers remarkably&nbsp;<strong>lightweight</strong>&nbsp;and&nbsp;<strong>fast to launch</strong>&nbsp;as opposed to deploying virtual machines, where the entire operating system needs to be deployed.</p>



<p>Packaging and moving a container image from one place to another is very easy when compared to VM image. The dependent software's will get downloaded from the Docker Hub, We will see this later.</p>



<h3 class="wp-block-heading">Docker Installation</h3>



<p>Docker is available for Mac, Linux and Windows and installing it is very straight forward https://docs.docker.com/engine/install/</p>



<p>Once you have docker installed you will  have the "docker" command line tool available.</p>



<figure class="wp-block-image size-full"><img loading="lazy" decoding="async" width="717" height="53" src="https://mosesdinakaran.in/wp-content/uploads/2023/02/image-21.png" alt="" class="wp-image-1648" srcset="https://mosesdinakaran.in/wp-content/uploads/2023/02/image-21.png 717w, https://mosesdinakaran.in/wp-content/uploads/2023/02/image-21-300x22.png 300w" sizes="auto, (max-width: 717px) 100vw, 717px" /></figure>



<h3 class="wp-block-heading">Docker Hub</h3>



<p>Once you have docker installed, now we need to create the container right, To create a container we need a container image.</p>



<p>A Docker container image is a lightweight, standalone, executable package that contains everything needed to run a piece of software. Container Images can be downloaded from the Docker Hub. A docker hub is a a public/private repo where we store the container images. Software vendors, you/me will store their application as a container image in the Docker repository. For every software application there will be an official image along with several non official images. To see the container images that are available in our system we can use the command </p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: csharp; title: ; notranslate">
docker images
</pre></div>


<p>In our system we have just installed the docker so we don't have any images or any container.</p>



<figure class="wp-block-image size-full"><img loading="lazy" decoding="async" width="800" height="138" src="https://mosesdinakaran.in/wp-content/uploads/2023/02/image-41.png" alt="" class="wp-image-1695" srcset="https://mosesdinakaran.in/wp-content/uploads/2023/02/image-41.png 800w, https://mosesdinakaran.in/wp-content/uploads/2023/02/image-41-300x52.png 300w, https://mosesdinakaran.in/wp-content/uploads/2023/02/image-41-768x132.png 768w" sizes="auto, (max-width: 800px) 100vw, 800px" /></figure>



<p></p>



<p>Downloading these images can be done  with the below command  </p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: csharp; title: ; notranslate">
docker pull imagename
Ex : docker pull nginx
</pre></div>


<p> To find the image name we can visit the docker hub portal https://hub.docker.com/ and search for the application that we need</p>



<h3 class="wp-block-heading">Container Image vs Container</h3>



<p>A Docker container image is a pre-built, static snapshot of a containerized application, along with all its dependencies and configurations. It is a read-only template that can be used to create multiple instances of the same container on different hosts. Container images are stored in a registry and can be easily shared and distributed across teams and environments.</p>



<p>A Docker container, on the other hand, is a running instance of a container image. So if the image is idle we call it as just Image, If the image is started and in the running state we call it as Container.</p>



<figure class="wp-block-image size-large is-resized"><img loading="lazy" decoding="async" src="https://mosesdinakaran.in/wp-content/uploads/2023/02/image-23-1024x360.png" alt="" class="wp-image-1650" width="768" height="270" srcset="https://mosesdinakaran.in/wp-content/uploads/2023/02/image-23-1024x360.png 1024w, https://mosesdinakaran.in/wp-content/uploads/2023/02/image-23-300x105.png 300w, https://mosesdinakaran.in/wp-content/uploads/2023/02/image-23-768x270.png 768w, https://mosesdinakaran.in/wp-content/uploads/2023/02/image-23.png 1027w" sizes="auto, (max-width: 768px) 100vw, 768px" /></figure>



<h3 class="wp-block-heading">Create Docker Containers </h3>



<p>To create a docker container its pretty use,  Say if we need to create a  Ubuntu container (aka ubuntu virtual machine)  We just need the below 2 commands </p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: csharp; title: ; notranslate">
docker pull ubuntu
</pre></div>


<figure class="wp-block-image size-full"><img loading="lazy" decoding="async" width="796" height="283" src="https://mosesdinakaran.in/wp-content/uploads/2023/02/image-25.png" alt="" class="wp-image-1652" srcset="https://mosesdinakaran.in/wp-content/uploads/2023/02/image-25.png 796w, https://mosesdinakaran.in/wp-content/uploads/2023/02/image-25-300x107.png 300w, https://mosesdinakaran.in/wp-content/uploads/2023/02/image-25-768x273.png 768w" sizes="auto, (max-width: 796px) 100vw, 796px" /></figure>



<p>We have pulled the docker image ubuntu from docker hub but its not started yet, i.e We have not created the container yet. To create the container we issue the next command</p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: csharp; title: ; notranslate">
docker run -ti -d ubuntu
</pre></div>


<p>Now we have a ubuntu container ( aka virtual machine ) running on windows.</p>



<p>Immediately you may ask, We discussed earlier container doesn't has its own OS then why are we pulling ubuntu OS?</p>



<p>Though docker containers does not require to have a complete OS (Kernel Layer + Application Layer), but they still need an operating system to run. However, unlike a traditional virtual machine, a Docker container shares the same kernel with the host operating system, but downloads a minimal application layer of ubuntu OS which will make is behave as a standalone ubuntu virtual machine.</p>



<p>So if you pull any application image ex docker pull nginx. This image will also have a very minimal OS within it. So while pulling the nginx image it will also pull a very minimal Base OS along with it.</p>



<p>Images are built in a layered architecture from a parent image. So we have a parent image and then this image is build on top of that parent Image.  If you see dockerfile  ( Dockerfile is a text file that contains a set of instructions or commands used to build a Docker image.) for an image you can see something like this</p>



<p>FROM debian:bullseye-slim</p>



<p>This will tell us from which parent image this image is build upon and in this case its build on base image of debian:bullseye-slim. This base image is basically an minimalistic OS without the kernel.</p>



<p>All good, But as per docker it can install linux container on Windows machine so how come docker uses the Windows Kernel to run Linux container</p>



<p>Under the hood, Docker for Windows uses a lightweight virtualization technology called "Hyper-V" to create a virtual machine that runs a small Linux kernel, which is responsible for managing the Docker containers. This Linux kernel is the same one used by Docker on other platforms, such as macOS and Linux.</p>



<p>Some Limitations to my knowledge (May be it will change in the future)</p>



<ul class="wp-block-list"><li>We cannot run Linux Container and the Windows Container at the same time.</li><li>Windows based Container cannot run on Linux and Linux based containers wont run on Windows Machine</li></ul>



<h3 class="wp-block-heading">Docker Containerization</h3>



<p>Docker containerization lets you package your application and its dependencies into a single unit, which can be easily deployed and managed in any environment.</p>



<p>Our goal here is to create a ubuntu container and install magento then this container will be packaged and moved to Docker Hub where we can use it in any TEST or PROD environment.</p>



<h5 class="wp-block-heading">Install Ubuntu</h5>



<p>Lets create a ubuntu container using the run command, At this point docker will assign a unique id for the container and a fancy name.</p>



<figure class="wp-block-image size-large"><img loading="lazy" decoding="async" width="1024" height="115" src="https://mosesdinakaran.in/wp-content/uploads/2023/02/image-26-1024x115.png" alt="" class="wp-image-1653" srcset="https://mosesdinakaran.in/wp-content/uploads/2023/02/image-26-1024x115.png 1024w, https://mosesdinakaran.in/wp-content/uploads/2023/02/image-26-300x34.png 300w, https://mosesdinakaran.in/wp-content/uploads/2023/02/image-26-768x86.png 768w, https://mosesdinakaran.in/wp-content/uploads/2023/02/image-26.png 1134w" sizes="auto, (max-width: 1024px) 100vw, 1024px" /></figure>



<p>Now we have created a container and ubuntu is running on it. Lets login to ubuntu. To do so we have the exec command,The&nbsp;<code>docker exec</code>&nbsp;command runs a new command in a running container.</p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: csharp; title: ; notranslate">
docker exec containeid command
Ex docker exec -ti CONTAINERID bash
</pre></div>


<p>Since we need to login to our  newly created container our command should be "bash". Bash command will create a container shell </p>



<figure class="wp-block-image size-full"><img loading="lazy" decoding="async" width="922" height="76" src="https://mosesdinakaran.in/wp-content/uploads/2023/02/image-27.png" alt="" class="wp-image-1655" srcset="https://mosesdinakaran.in/wp-content/uploads/2023/02/image-27.png 922w, https://mosesdinakaran.in/wp-content/uploads/2023/02/image-27-300x25.png 300w, https://mosesdinakaran.in/wp-content/uploads/2023/02/image-27-768x63.png 768w" sizes="auto, (max-width: 922px) 100vw, 922px" /></figure>



<p>-ti -&gt; This is for interactive session which is important</p>



<p>Once you are in the container and if we see the hostname, we will get the ContainerID, So basically outside application willl communicate to this container using this ContainerID</p>



<p>Now that we are in the ubuntu container we can run all the basic ubuntu commands.</p>



<figure class="wp-block-image size-full"><img loading="lazy" decoding="async" width="1022" height="167" src="https://mosesdinakaran.in/wp-content/uploads/2023/02/image-28.png" alt="" class="wp-image-1656" srcset="https://mosesdinakaran.in/wp-content/uploads/2023/02/image-28.png 1022w, https://mosesdinakaran.in/wp-content/uploads/2023/02/image-28-300x49.png 300w, https://mosesdinakaran.in/wp-content/uploads/2023/02/image-28-768x125.png 768w" sizes="auto, (max-width: 1022px) 100vw, 1022px" /></figure>



<h5 class="wp-block-heading">Install Nginx and Other Applications</h5>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: csharp; title: ; notranslate">
apt install nginx
</pre></div>


<figure class="wp-block-image size-full"><img loading="lazy" decoding="async" width="597" height="124" src="https://mosesdinakaran.in/wp-content/uploads/2023/02/image-29.png" alt="" class="wp-image-1657" srcset="https://mosesdinakaran.in/wp-content/uploads/2023/02/image-29.png 597w, https://mosesdinakaran.in/wp-content/uploads/2023/02/image-29-300x62.png 300w" sizes="auto, (max-width: 597px) 100vw, 597px" /></figure>



<p>The ubuntu base image that we downloaded will be very light weight it will not have most of the commands that you use regularly for ex systemctl will not be available which needs to be installed</p>



<figure class="wp-block-image size-full"><img loading="lazy" decoding="async" width="918" height="100" src="https://mosesdinakaran.in/wp-content/uploads/2023/02/image-30.png" alt="" class="wp-image-1658" srcset="https://mosesdinakaran.in/wp-content/uploads/2023/02/image-30.png 918w, https://mosesdinakaran.in/wp-content/uploads/2023/02/image-30-300x33.png 300w, https://mosesdinakaran.in/wp-content/uploads/2023/02/image-30-768x84.png 768w" sizes="auto, (max-width: 918px) 100vw, 918px" /></figure>



<p>No we a Ubuntu Container where Nginx is installed on it. Like wise we can install all the necessary application.</p>



<p>Once we are completely done with the Magento Setup, We need to package this container and push to Docker Hub.</p>



<h3 class="wp-block-heading">Creating a Container Image ( or Packaging)</h3>



<p>There are two ways to create a docker image.</p>



<ol class="wp-block-list"><li>Using an Existing Image</li><li>Building form the scratch using a DockerFile</li></ol>



<h5 class="wp-block-heading">Using an Existing Image</h5>



<p>Remember we pulled this "ubuntu" container and installed magento over it and now we are going to create an image of this container and adding it to Docker Hub.</p>



<h6 class="wp-block-heading">Docker Login</h6>



<p>First create an account in the docker hub. Now use that account credentials and login to the docker that you have installed in your system.</p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: plain; title: ; notranslate">
docker login 
</pre></div>


<figure class="wp-block-image size-large"><img loading="lazy" decoding="async" width="1024" height="113" src="https://mosesdinakaran.in/wp-content/uploads/2023/02/image-42-1024x113.png" alt="" class="wp-image-1704" srcset="https://mosesdinakaran.in/wp-content/uploads/2023/02/image-42-1024x113.png 1024w, https://mosesdinakaran.in/wp-content/uploads/2023/02/image-42-300x33.png 300w, https://mosesdinakaran.in/wp-content/uploads/2023/02/image-42-768x84.png 768w, https://mosesdinakaran.in/wp-content/uploads/2023/02/image-42.png 1318w" sizes="auto, (max-width: 1024px) 100vw, 1024px" /></figure>



<h6 class="wp-block-heading">Create the Container Image</h6>



<p>Identify the container for which we need to create the image. In our case its the "ubuntu" container where we have installed Magento</p>



<p>Now to create an image we need to use the commit command Use the docker ps command to find the container ID</p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: plain; title: ; notranslate">
docker commit containerID &amp;lt;username&gt;/&amp;lt;newimagename&gt; 
Ex docker commit containerID mosesdinakaran/ubuntu_magento
</pre></div>


<p> </p>



<figure class="wp-block-image size-large"><img loading="lazy" decoding="async" width="1024" height="293" src="https://mosesdinakaran.in/wp-content/uploads/2023/02/image-38-1024x293.png" alt="" class="wp-image-1674" srcset="https://mosesdinakaran.in/wp-content/uploads/2023/02/image-38-1024x293.png 1024w, https://mosesdinakaran.in/wp-content/uploads/2023/02/image-38-300x86.png 300w, https://mosesdinakaran.in/wp-content/uploads/2023/02/image-38-768x220.png 768w, https://mosesdinakaran.in/wp-content/uploads/2023/02/image-38.png 1168w" sizes="auto, (max-width: 1024px) 100vw, 1024px" /></figure>



<p>Username is required because, Only then docker will know to which user account this container needs to be pushed.</p>



<p>Now we have a new image named "mosesdinakaran/ubuntu_magento" created.</p>



<h6 class="wp-block-heading">Push Container Image to Docker Hub</h6>



<p>Once we have created the container image we need to push this image to docker hub, To do so use the below command</p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: plain; title: ; notranslate">
docker push username/newimagename
Ex docker push mosesdinakaran/ubuntu_magento
</pre></div>


<figure class="wp-block-image size-full"><img loading="lazy" decoding="async" width="659" height="121" src="https://mosesdinakaran.in/wp-content/uploads/2023/02/image-43.png" alt="" class="wp-image-1708" srcset="https://mosesdinakaran.in/wp-content/uploads/2023/02/image-43.png 659w, https://mosesdinakaran.in/wp-content/uploads/2023/02/image-43-300x55.png 300w" sizes="auto, (max-width: 659px) 100vw, 659px" /></figure>



<p>Once the image is pushed it will  be available in our account.</p>



<figure class="wp-block-image size-large"><img loading="lazy" decoding="async" width="1024" height="316" src="https://mosesdinakaran.in/wp-content/uploads/2023/02/image-40-1024x316.png" alt="" class="wp-image-1677" srcset="https://mosesdinakaran.in/wp-content/uploads/2023/02/image-40-1024x316.png 1024w, https://mosesdinakaran.in/wp-content/uploads/2023/02/image-40-300x93.png 300w, https://mosesdinakaran.in/wp-content/uploads/2023/02/image-40-768x237.png 768w, https://mosesdinakaran.in/wp-content/uploads/2023/02/image-40-1536x474.png 1536w, https://mosesdinakaran.in/wp-content/uploads/2023/02/image-40.png 1689w" sizes="auto, (max-width: 1024px) 100vw, 1024px" /></figure>



<p>Note, We have not pushed this new container image to ubuntu repo but to our personal repository with a new name "ubuntu_magento"</p>



<p>This new container image can be now pulled from any server.</p>



<h5 class="wp-block-heading">Building form the scratch using a DockerFile</h5>



<h6 class="wp-block-heading">Write a Dockerfile</h6>



<p> A Dockerfile is a text file that specifies the instructions needed to create a container image. It typically includes information about the base image, any additional software or dependencies needed, and any configuration settings required for the application.</p>



<figure class="wp-block-image size-full"><img loading="lazy" decoding="async" width="451" height="187" src="https://mosesdinakaran.in/wp-content/uploads/2023/02/image-47.png" alt="" class="wp-image-1714" srcset="https://mosesdinakaran.in/wp-content/uploads/2023/02/image-47.png 451w, https://mosesdinakaran.in/wp-content/uploads/2023/02/image-47-300x124.png 300w" sizes="auto, (max-width: 451px) 100vw, 451px" /></figure>



<h6 class="wp-block-heading">Build the image: </h6>



<p>Once you've written your Dockerfile, you can use the Docker command line tool to build the container image. This process involves running the instructions in the Dockerfile to create a new image that includes your application and all its dependencies.</p>



<h6 class="wp-block-heading">Push the image to a registry: </h6>



<p>Once you've built the container image, you'll typically want to push it to a Docker registry. This is a central location where you can store and distribute container images, so that they can be easily accessed and deployed by others.</p>



<h6 class="wp-block-heading">Deploy the container:</h6>



<p> Once your container image is in the registry, you can use it to deploy your application to any environment that supports Docker containers. This might involve running the container locally on your own computer, or deploying it to a cloud-based hosting platform like Amazon Web Services (AWS) or Microsoft Azure.</p>



<p></p>



<h3 class="wp-block-heading">Running 2 Version Of the Same Application</h3>



<p>Lets say if we need to run 2 redis version on our laptop its very easy with docker, We just need to create 2 containers with the respective version</p>



<p>docker run redis -&gt; Install the latest version</p>



<p>docker run redis:4.0 -&gt; Install the version 4.0</p>



<figure class="wp-block-image size-large"><img loading="lazy" decoding="async" width="1024" height="132" src="https://mosesdinakaran.in/wp-content/uploads/2023/02/image-35-1024x132.png" alt="" class="wp-image-1664" srcset="https://mosesdinakaran.in/wp-content/uploads/2023/02/image-35-1024x132.png 1024w, https://mosesdinakaran.in/wp-content/uploads/2023/02/image-35-300x39.png 300w, https://mosesdinakaran.in/wp-content/uploads/2023/02/image-35-768x99.png 768w, https://mosesdinakaran.in/wp-content/uploads/2023/02/image-35.png 1277w" sizes="auto, (max-width: 1024px) 100vw, 1024px" /></figure>



<p>In the above image we see 2 containers  listening to the incoming request on the same port. In both the redis image it was specified to run redis on 6379 port. So both containers open the same port So how does this actually work and how do we not have conflict while both are running on the same port ? This is because of container ports.</p>



<figure class="wp-block-image size-large"><img loading="lazy" decoding="async" width="1024" height="577" src="https://mosesdinakaran.in/wp-content/uploads/2023/02/image-36-1024x577.png" alt="" class="wp-image-1665" srcset="https://mosesdinakaran.in/wp-content/uploads/2023/02/image-36-1024x577.png 1024w, https://mosesdinakaran.in/wp-content/uploads/2023/02/image-36-300x169.png 300w, https://mosesdinakaran.in/wp-content/uploads/2023/02/image-36-768x433.png 768w, https://mosesdinakaran.in/wp-content/uploads/2023/02/image-36-320x180.png 320w, https://mosesdinakaran.in/wp-content/uploads/2023/02/image-36.png 1084w" sizes="auto, (max-width: 1024px) 100vw, 1024px" /></figure>



<p>Your laptop will have certain ports available which can be opened for running certain application. In our case a separate laptop port will be opened for each container that yor run. To communicate between the laptop and the container you need to create a binding between the laptop(Host) port and the Container Port.</p>



<p>At present two redis container is running but they are unreachable because we didn't do any binding. So while running the container we need to make sure that we are binding that container port with that of the laptop port.</p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: csharp; title: ; notranslate">
docker run laptopport:containerport imagename
Ex 
docker run -p6000:6379 redis
docker run -p6001:6379 redis4.0

</pre></div>


<p>Now we have two redis version running on the same laptop which can be access thorough the binding ports.</p>



<figure class="wp-block-image size-large"><img loading="lazy" decoding="async" width="1024" height="107" src="https://mosesdinakaran.in/wp-content/uploads/2023/02/image-48-1024x107.png" alt="" class="wp-image-1719" srcset="https://mosesdinakaran.in/wp-content/uploads/2023/02/image-48-1024x107.png 1024w, https://mosesdinakaran.in/wp-content/uploads/2023/02/image-48-300x31.png 300w, https://mosesdinakaran.in/wp-content/uploads/2023/02/image-48-768x80.png 768w, https://mosesdinakaran.in/wp-content/uploads/2023/02/image-48.png 1026w" sizes="auto, (max-width: 1024px) 100vw, 1024px" /></figure>



<h5 class="wp-block-heading">To access Host from Docker Container</h5>



<p>Use your internal IP address or connect to the special DNS name&nbsp;<code>host.docker.internal</code>&nbsp;which will resolve to the internal IP address used by the host.</p>



<p>This is for development purpose and does not work in a production environment outside of Docker Desktop.</p>



<h3 class="wp-block-heading">Quick Commands</h3>



<figure class="wp-block-table"><table><tbody><tr><td>To Build a docker image from the Doker file</td><td>docker build -t imagename:tagname .</td></tr><tr><td>To Run the Container</td><td>docker run -it -p 8085:80 ImageId<br>localhost:8085 from the host will point to the port 80 of docker container</td></tr><tr><td>To Login to the Containe</td><td>docker exec -it conterid bash</td></tr><tr><td>To Delete a Docker Image</td><td>docker rmi -f imageID</td></tr><tr><td>Restart nginx withing the container</td><td><code>nginx -s reload</code></td></tr><tr><td></td><td></td></tr><tr><td></td><td></td></tr></tbody></table></figure>



<p></p>



<h3 class="wp-block-heading">References</h3>



<p>Plz refer the below two videos if you need more information on this topic. It was really easy to follow and helpful.</p>



<p class="wp-embed-aspect-16-9 wp-has-aspect-ratio"><a href="https://www.youtube.com/watch?v=3c-iBn73dDE">https://www.youtube.com/watch?v=3c-iBn73dDE</a></p>



<p><a href="https://www.youtube.com/watch?v=vWjP3fsfgrw">https://www.youtube.com/watch?v=vWjP3fsfgrw</a></p>



<p></p>



<p></p><p>The post <a href="https://mosesdinakaran.in/virtualization-docker-fundamentals-for-beginners/">Virtualization & Docker Fundamentals for Beginners</a> first appeared on <a href="https://mosesdinakaran.in">Moses Dinakaran</a>.</p>]]></content:encoded>
					
					<wfw:commentRss>https://mosesdinakaran.in/virtualization-docker-fundamentals-for-beginners/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
