{"componentChunkName":"component---src-templates-blog-post-js","path":"/docker-microservices/","result":{"data":{"site":{"siteMetadata":{"title":"tech-blog","author":"Alamgir Qazi"}},"markdownRemark":{"id":"43dadec2-1b86-541c-b4d1-7a8c3c419ef8","excerpt":"Before we talk about Docker and what is does, lets talk about some of the issues we face in development.  Its runs on my laptop/computer…","html":"<p>\n  <a\n    class=\"gatsby-resp-image-link\"\n    href=\"/tech-blog/static/0b86d91e4debadaaea08a19fe56432ed/fdaf8/docker.png\"\n    style=\"display: block\"\n    target=\"_blank\"\n    rel=\"noopener\"\n  >\n  \n  <span\n    class=\"gatsby-resp-image-wrapper\"\n    style=\"position: relative; display: block;  max-width: 590px; margin-left: auto; margin-right: auto;\"\n  >\n    <span\n      class=\"gatsby-resp-image-background-image\"\n      style=\"padding-bottom: 29.72972972972973%; position: relative; bottom: 0; left: 0; background-image: url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAGCAYAAADDl76dAAAACXBIWXMAAAsSAAALEgHS3X78AAABgElEQVQY002RyytEURzHf3fGeaSUJBZSSmHyB9hZMQskQpKNLDxqLoosRxMzKCOaMTWaWLgeIYVsPRKZ8SzPLKSGLGyEhQUd3+saOfXr+/udc3+f+z2/QzJwT2LoxMbdWySDyN3bDtG/bxd92yS8MY21RjTetUjMZWjcZRDXZzVmKXHXrGaGecYsJZIjFzY5eEQAFMuBg4jwRmNJNe5k7t5MF8NnBCDxzoUEgJhuwdhv/ZMnFGG6A2ynXHpjb8x3rITv8EN4dqel//pcePaqWHMwg+tztWhyAJCCpkw4KeO6kYM6FfvZgGXhR/nYg8OZDxKjNxv20KNKC8ffZfhJyVBcSf/lOO9aKgCgDQ1+aDeaugFrR+1CBADsgU7ibAxaaLokm++UqP+oumH94dO5+qy0icev5LHbqTSlyFz4qBEQJ7QObiqgcGtUQksQ9chNeDHTrXGQM6qoaOWFXu/Oc5uWr0qpdzOP1hQltUQsYMc8JR6B/c7r36P8zRHX1UzgN1A6q+31HsT/AAAAAElFTkSuQmCC'); background-size: cover; display: block;\"\n    >\n      <img\n        class=\"gatsby-resp-image-image\"\n        style=\"width: 100%; height: 100%; margin: 0; vertical-align: middle; position: absolute; top: 0; left: 0; box-shadow: inset 0px 0px 0px 400px white;\"\n        alt=\"Docker and Microservices\"\n        title=\"\"\n        src=\"/tech-blog/static/0b86d91e4debadaaea08a19fe56432ed/fcda8/docker.png\"\n        srcset=\"/tech-blog/static/0b86d91e4debadaaea08a19fe56432ed/12f09/docker.png 148w,\n/tech-blog/static/0b86d91e4debadaaea08a19fe56432ed/e4a3f/docker.png 295w,\n/tech-blog/static/0b86d91e4debadaaea08a19fe56432ed/fcda8/docker.png 590w,\n/tech-blog/static/0b86d91e4debadaaea08a19fe56432ed/fdaf8/docker.png 674w\"\n        sizes=\"(max-width: 590px) 100vw, 590px\"\n      />\n    </span>\n  </span>\n  \n  </a>\n    </p>\n<p>Before we talk about Docker and what is does, lets talk about some of the issues we face in development. </p>\n<ol>\n<li>Its runs on my laptop/computer :) </li>\n</ol>\n<p>We often hear developers say, “But it runs perfectly on my computer” while it doesn’t behave expectedly in production. This is one of the common issues developers face. Most of these issues are because the environments are not the same. </p>\n<ol start=\"2\">\n<li>Scaling applications is expensive. Adding more Virtual Machines (VMs) can be expensive. Its easy / cheap to start development but it becomes expensive in large-scale applications.</li>\n<li>Lets say, our application requires integrating different services e.g Elastic Search. Should every developer install Elastic Search and all databases (e.g postgres) on their laptop? what if there are version issues? what about OS issues? Mac and a windows? what about production environment? which version will be used?</li>\n</ol>\n<p>Here comes Docker </p>\n<h3>so what is Docker?</h3>\n<p>Docker is a <strong>tool</strong> designed to make it easier to create, deploy, and run applications by using <strong>containers</strong>.</p>\n<p>Docker enables developers to easily pack, ship, and run any application as a lightweight, portable, self-sufficient container, which can run virtually anywhere.</p>\n<h3>So what actually is a container?</h3>\n<p>Containers allow a developer to package up an application with all of the parts it needs, such as libraries and other dependencies, and ship it all out as one package.</p>\n<p>Containers are encapsulated environments in which applications are run.</p>\n<p>There are three main concepts regarding Docker</p>\n<ol>\n<li>Docker Images</li>\n<li>Containers</li>\n<li>Dockerfile</li>\n</ol>\n<p>Docker is a technology that allows you to incorporate and store your code and its dependencies into a neat little package - an <strong>image</strong>.</p>\n<p>A docker image holds your source code and the dependencies it needs. Lets say you have a Node.js app, A docker image of your app will contain the code + the node_modules and everything that’s needed to run your Node.js app.</p>\n<h3>Dockerfile</h3>\n<p>A Dockerfile is a text file that contains a bunch of instructions which informs Docker how the Docker image should get built.</p>\n<p>Here is an Angular project with multiple Dockerfiles for each environment</p>\n<p>\n  <a\n    class=\"gatsby-resp-image-link\"\n    href=\"/tech-blog/static/1499d878bcd75ca12f33d04166b976a8/55fc0/angularproject.png\"\n    style=\"display: block\"\n    target=\"_blank\"\n    rel=\"noopener\"\n  >\n  \n  <span\n    class=\"gatsby-resp-image-wrapper\"\n    style=\"position: relative; display: block;  max-width: 433px; margin-left: auto; margin-right: auto;\"\n  >\n    <span\n      class=\"gatsby-resp-image-background-image\"\n      style=\"padding-bottom: 173.64864864864867%; position: relative; bottom: 0; left: 0; background-image: url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAjCAYAAACU9ioYAAAACXBIWXMAABJ0AAASdAHeZh94AAAC+ElEQVRIx51WiW6bQBTk/7+pUhW1amMXO2mTBmNulvvwBdjYBk/frp0oSVPVy0prkLCGeW9m3qL0/QmL6oj9sQdfh+MRbbvHfn/AkKV0XY/frEW67l4Am2aLmnbf9zidTlJb4T9eskRWLFFVFZq6wXa7JYZ78QcOKrMVzqosM/iODRaEiKMYAQtQlKVgLMtSORwOqOoa6/WawCLB8nnJshMMu65DnibwfQZjbsB1HWRZjq7vh4rSYVEWcBwXeV5gs9lguVyJt/ElLUrXd8SIeuj7qOtK2OVISj+DDWLImQUBg20ZsEwbSZL+xU4KsCgKAWLbFnZt+wbs/f1/AXmv8jxHSsJ8tGTLVpqmgaZpKImlYRrI8kxs3tPVai3PkPtOVVXBUNefCNSEY5lwPR8tpUUasCZTPzw8ktIpjJkOzw+ga0/0gtfCSJS8IYbT6RQRRU7XZjAtC0EYYLfbDfNhQ977eX8nsmsZc3ieh4aGw0USeR+ydI1PN99xO33E5y9j3HxT8evJxobat9zSrKzJVjQvyyu3EuWVALl70HGr3uPr+A7q/QxBuoITFshWLVZNh2V93Va2TQ31xw8kcQzmO5QUixRmVL4Gk2zUXWJ4dcl8dI3HY4peQJPGhmFY1EcCJOtkZHgxxmRE4bYZjUYC0OOAc5PAXAEexhF4NGUSI5LCbeO5LoHNMNPmwouM+XA892XgShlbVSc09n0Cm0GfzeHTPTf2M4bUtBHR46Kk0WVi23Boaru2i3KxGBC96tzDMA5Fjj3KsGEYg8BeSp5OJ4jjRFiG0YnHGcYkyJ4OsNOpl+/hhEQJKb9zYmYa54OKkSAihrWsKNXZh2EYilKNOansOGKcvZ7YUqIIY4cRCWELVj5jCOnQZ75HB1Y3oOTJuYdcZduhpLicYfYGSNLYE6RJApuyq5MwDl3zS+ykVeZZ5j6MKHoWlex4/rtpLcmQ93A0uqXvmoDK9QjUfHOUSg9Yfoxy0JZA+Icmv370GXI14L8eDGHH1x8wJ5ruYXjFowAAAABJRU5ErkJggg=='); background-size: cover; display: block;\"\n    >\n      <img\n        class=\"gatsby-resp-image-image\"\n        style=\"width: 100%; height: 100%; margin: 0; vertical-align: middle; position: absolute; top: 0; left: 0; box-shadow: inset 0px 0px 0px 400px white;\"\n        alt=\"multiple docker files\"\n        title=\"\"\n        src=\"/tech-blog/static/1499d878bcd75ca12f33d04166b976a8/55fc0/angularproject.png\"\n        srcset=\"/tech-blog/static/1499d878bcd75ca12f33d04166b976a8/12f09/angularproject.png 148w,\n/tech-blog/static/1499d878bcd75ca12f33d04166b976a8/e4a3f/angularproject.png 295w,\n/tech-blog/static/1499d878bcd75ca12f33d04166b976a8/55fc0/angularproject.png 433w\"\n        sizes=\"(max-width: 433px) 100vw, 433px\"\n      />\n    </span>\n  </span>\n  \n  </a>\n    </p>\n<p>sample Angular Dockerfile</p>\n<div class=\"gatsby-highlight\" data-language=\"text\"><pre class=\"language-text\"><code class=\"language-text\"># Build Angular and upload to Firebase Hosting\n\nFROM johnpapa/angular-cli\n\nWORKDIR /home/angularapp\nCOPY package.json ./\n# RUN npm install &amp;&amp; ng build --prod --aot --progress=false\nRUN npm install npm@latest -g\nRUN npm install -g firebase-tools\nRUN npm install\nCOPY . /home/angularapp\n\nRUN ng build --prod\nRUN firebase deploy --token 1/h7xVYgTpyi4lt48D-8iasdasdasucj0aiFYz2Dk4</code></pre></div>\n<p>sample Node.js Dockerfile</p>\n<div class=\"gatsby-highlight\" data-language=\"text\"><pre class=\"language-text\"><code class=\"language-text\">FROM node:8.11.2-alpine\nWORKDIR /app\nCOPY package.json ./\n# Some packages for Node require these dependencies (e.g Bcrypt)\nRUN apk add --no-cache --virtual .build-deps alpine-sdk python \\\n    &amp;&amp; apk update &amp;&amp; apk upgrade \\\n    &amp;&amp; apk add pdftk  \\\n    &amp;&amp; npm install --production --silent \\\n    &amp;&amp; apk del .build-deps\nRUN npm install\nCOPY . /app\nEXPOSE 3000 \nCMD [ \"npm\", \"start\" ]</code></pre></div>\n<h3>Why use Docker ? What are the benefits?</h3>\n<p>Docker helps us set up local development environments that are exactly like a live production server.</p>\n<p>Suppose you are working on multiple projects. They all have different environments. One project uses Postgres 9, One uses Postgres 10. This is not a headache with Docker. It will install what is required.</p>\n<p>If you have 30 Docker containers that you want to run, you can run them all on a single virtual machine. To run 30 virtual machines, you’ve got to boot 30 operating systems with at least minimum resource requirements available before factoring the hypervisor for them to run on with the base OS.</p>\n<p>Docker has been designed in a way that it can be incorporated into most DevOps applications, including Puppet, Chef, Vagrant, and Ansible, or it can be used on its own to manage development environments.</p>\n<p>Agile and CI / CD practices are very easy to implement in a docker environment.</p>\n<p> Your development team is able to create complex requirements for a <strong>microservice</strong> within an easy-to-write Dockerfile.</p>\n<p>The performance difference b/w Containers and VMs is 26:1. Those types of efficiency gains are on par with cloud providers like Amazon and on the virtual machines that they sell per hour. It’s a huge enabler for their businesses because you’re suddenly able to do a lot more for the same price.</p>\n<h3>Docker Alternatives?</h3>\n<p>Although Docker is the most popular container technology, but there many other container solutions out there:\nBSD Jails\nLXD\nRKT (pronounced as Rocket)</p>\n<h3>Container Orchestration</h3>\n<p>Like any other element of your IT infrastructure, containers need to be monitored and controlled. Otherwise, you literally have no idea what’s running on your servers.</p>\n<p>That is where Orchestration comes in. There are 3 big players. </p>\n<p>Docker Swarm, Kubernetes (built by Google), and Mesosphere (used by Twitter). Today, these are all still around, but Kubernetes is by far the most dominant cloud-orchestration program.</p>\n<h3>Kubernetes</h3>\n<p>Kubernetes (K8s) is an open-source system for automating deployment, scaling, and management of containerized applications.</p>\n<p>“Designed on the same principles that allows Google to run billions of containers a week, Kubernetes can scale without increasing your ops team.”</p>\n<h3>Containers as a Service</h3>\n<p>AWS Fargate. Just build apps and Amazon will manage the containers for you (scale automatically).</p>\n<h3>Microservices</h3>\n<p>Microservices are an architectural approach to building applications. As an architectural framework, microservices are distributed and loosely coupled, so one team’s changes won’t break the entire app. The benefit to using microservices is that development teams are able to rapidly build new components of apps to meet changing business needs.</p>\n<p>Microservices can communicate with each other, usually statelessly, so apps built in this way can be more fault tolerant. This also allows dev teams to choose their own tools, since microservices can communicate through language-agnostic application programming interfaces (APIs).</p>\n<p>\n  <a\n    class=\"gatsby-resp-image-link\"\n    href=\"/tech-blog/static/ffd1db91e5b339f4eea04caac6f6a413/5b587/monovsmicro.png\"\n    style=\"display: block\"\n    target=\"_blank\"\n    rel=\"noopener\"\n  >\n  \n  <span\n    class=\"gatsby-resp-image-wrapper\"\n    style=\"position: relative; display: block;  max-width: 590px; margin-left: auto; margin-right: auto;\"\n  >\n    <span\n      class=\"gatsby-resp-image-background-image\"\n      style=\"padding-bottom: 50.67567567567568%; position: relative; bottom: 0; left: 0; background-image: url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAKCAYAAAC0VX7mAAAACXBIWXMAABJ0AAASdAHeZh94AAACGUlEQVQoz4VSTWjTYBgOgojePIlHBQ9DPQuCJxE9iCjeREHxoN6HeBg63BSGs+g2dIwdam031rluldZq979V2rqlWmeRTielK7ZqXNu0S1KT5vH9viYrePGBN7y/z5f3R6ibJorVTWxUqpAVBeVNBUa9DgaTYkwYqqpGcRVKrcalqqqokG3n2HmCYRjQdR02TCJjPpZQJ90W5pNkGd8KP7hIZRk6+Qwea9YIjCSc+IAL3Y9x0dEL59QsJ7aJGP5QMkNv8A32XL6OvVduoMv3Ev+C5XNCdySKlpu3cfBWOzrHAzxYoZbY6zqJTZj9+Qvzn1KYTq5AKpUwnEzhvMuLSyN+9MfFRsvs4xSTODY4hJPPvGifXmzMTNPwP7xKpdEamERbeA6e9ytNwuX0Ghy+IHr8rxEWP24VZDaKcCzG0TE5h/7oMkKra+iYWkBbIIxxIhsikkezEQzEEyjIlSZhloa89PkLxPRXrGZzWxsTc99xzv0CZz1juDoWRPdCDKddozhDvnszEVybCFHMh1POEbzNrFtbtgavqgrkcrm5EItUo9bzhQK7IW6XaHaS9Ns6CZPHNGs8fCn23xw+fgI79x9A0SKt0a0x3HnwEMK27QjNNLa/78hR7G45xPX5aAzCjl1ovdvJbXZ+gn0awxN+PHU9h0ZE7BHD2mwskcD9nj5k1hstOb2jGHB7uJ7L59HV9wSRd0vcZjV/AZTMwclX2HUvAAAAAElFTkSuQmCC'); background-size: cover; display: block;\"\n    >\n      <img\n        class=\"gatsby-resp-image-image\"\n        style=\"width: 100%; height: 100%; margin: 0; vertical-align: middle; position: absolute; top: 0; left: 0; box-shadow: inset 0px 0px 0px 400px white;\"\n        alt=\"monolith vs microservices.PNG\"\n        title=\"\"\n        src=\"/tech-blog/static/ffd1db91e5b339f4eea04caac6f6a413/fcda8/monovsmicro.png\"\n        srcset=\"/tech-blog/static/ffd1db91e5b339f4eea04caac6f6a413/12f09/monovsmicro.png 148w,\n/tech-blog/static/ffd1db91e5b339f4eea04caac6f6a413/e4a3f/monovsmicro.png 295w,\n/tech-blog/static/ffd1db91e5b339f4eea04caac6f6a413/fcda8/monovsmicro.png 590w,\n/tech-blog/static/ffd1db91e5b339f4eea04caac6f6a413/efc66/monovsmicro.png 885w,\n/tech-blog/static/ffd1db91e5b339f4eea04caac6f6a413/5b587/monovsmicro.png 1010w\"\n        sizes=\"(max-width: 590px) 100vw, 590px\"\n      />\n    </span>\n  </span>\n  \n  </a>\n    </p>\n<p>Microservices can be written in different languages depending on the use case and requirement. Modern application development essentially consists of composing an application from a variety of services.</p>\n<h3>Why not Docker?</h3>\n<p>Run applications as fast as a bare-metal server. Docker containers have less overhead than virtual machines. But Docker does not have zero overhead. The only way to get true bare-metal speed from an application is to run it directly on a bare-metal server, without using containers or virtual machines.</p>\n<p>Docker also creates new security challenges — such as the difficulty of monitoring so many moving pieces within a dynamic, large-scale Docker environment. Before moving workloads to Docker, you need to evaluate the Docker-specific security risks and make sure you can handle them.</p>\n<p>All the data stored inside a container will be lost forever once the container shuts down. Managing stateful applications in a docker environment is hard.</p>\n<p>You have to think of ways to save your data somewhere else first. To keep data safe in Docker, you need to employ an additional tool – Docker Data Volumes. Yet, this solution is still quite clumsy and needs to be improved.</p>\n<p>Docker should be avoided for production databases unless you have strong expertise in managing stateful applications in Docker.</p>","frontmatter":{"title":"Docker and Microservices","date":"February 27, 2020"}}},"pageContext":{"slug":"/docker-microservices/","previous":{"fields":{"slug":"/intro-to-nodejs-part-8/"},"frontmatter":{"title":"A very basic introduction to Node.js - Part 8"}},"next":{"fields":{"slug":"/install-hortonworks-data-platform-hdp-3.1.5/"},"frontmatter":{"title":"Install Multi-node Hadoop using Hortonworks Data Platform (HDP) 3.1.5 on Ubuntu 18 LTS"}}}},"staticQueryHashes":[]}