<?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: Ali Ahmed</title>
    <description>The latest articles on DEV Community by Ali Ahmed (@alinisarahmed).</description>
    <link>https://dev.to/alinisarahmed</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%2F156959%2F5321ab7e-feac-4ae5-89e4-c61847bc4399.jpeg</url>
      <title>DEV Community: Ali Ahmed</title>
      <link>https://dev.to/alinisarahmed</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/alinisarahmed"/>
    <language>en</language>
    <item>
      <title>Introduction to Docker and Cycle</title>
      <dc:creator>Ali Ahmed</dc:creator>
      <pubDate>Mon, 06 May 2019 19:32:17 +0000</pubDate>
      <link>https://dev.to/alinisarahmed/introduction-to-docker-and-cycle-5ef6</link>
      <guid>https://dev.to/alinisarahmed/introduction-to-docker-and-cycle-5ef6</guid>
      <description>&lt;p&gt;*(This is from my &lt;a href="https://alinisarahmed.live/learn-docker"&gt;blog post&lt;/a&gt;, my first attempt at writing technical articles)&lt;/p&gt;

&lt;p&gt;Docker has taken the world over by storm (or at least, taken over my world, the rest of the world realized its importance a while back). But what exactly is Docker? I recently got a chance to learn Docker on my own. This post documents my experience on what I learned and is a bird's eye view on the Container technology for those who do not have the faintest idea of what that is (just like me, when I started). This post aims to give readers an idea of what Docker is (I hope), and will act as a building block to learn further. We will also learn about container orchestration systems which manage containers on the server, and explore one such system, Cycle. &lt;/p&gt;

&lt;h2&gt;
  
  
  What is Docker
&lt;/h2&gt;

&lt;p&gt;Docker is an open-source software that helps us run other software by enfolding and bundling the whole thing in &lt;strong&gt;Containers&lt;/strong&gt;. Containers are self-contained units of software, with all their dependencies included within the package. If you take a look at Docker's &lt;a href="https://blog.docker.com/2013/06/announcing-new-docker-style/"&gt;logo&lt;/a&gt;, it is a whale carrying shipping containers, and that perfectly depicts how Docker works. It transforms all your project files, folders, and other stuff into a "shipping container", a self-sufficient unit of software carrying with it everything that it needs to run. An app may be divided into multiple such containers, and they can all run in tandem on a server, choreographed by a container orchestration system.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--zcQe7YJd--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://thepracticaldev.s3.amazonaws.com/i/9gy4ehr32m8mx9abh0l3.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--zcQe7YJd--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://thepracticaldev.s3.amazonaws.com/i/9gy4ehr32m8mx9abh0l3.png" alt="Docker Logo"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;So it is sort of like a plug-and-play system, we just download a "box", plug it into Docker, and off it runs, without a care in the world what operating system our computer has, or whether any dependency (or variations of same dependency) is installed on our system at all. Similarly, we can pack our next app in a container, ship it to a friend, and all we need to worry about whether our friend will be able to run the app or not is whether they have Docker itself installed on their system. Whatever the operating system might be, Windows, Mac or Linux, a Docker container can run inside of it without any hiccups, even if that container was assembled in either of Windows, Linux or Mac. All that is needed is the Docker software itself installed on our systems.&lt;/p&gt;

&lt;p&gt;Technically, Docker does not make Containers out of our projects directly, it makes &lt;strong&gt;Images&lt;/strong&gt;, and as the name might suggest, Images are what Docker &lt;strong&gt;Containers&lt;/strong&gt; come out of. You download an Image, make a Container out of it, and run your app as that container. There are thousands of Images on &lt;a href="https://hub.docker.com/"&gt;Docker Hub&lt;/a&gt;, a central repository for docker Images. &lt;/p&gt;

&lt;h2&gt;
  
  
  What was there before Docker?
&lt;/h2&gt;

&lt;p&gt;The standard has been to use Virtual Machines (a computer inside a computer, with an operating system of its own) to run applications. Virtual Machines provide isolation for the software run inside them, at the cost of computational power and hardware resources required to run a full-blown operating system inside another. A few Virtual Machines running on a system is a huge resource hog for that system. Docker takes a different approach, they create virtual isolation for our apps, and, unlike VMs, provides an app with just enough resources it needs to run in isolation. The figure below, taken from the official Docker &lt;a href="https://docs.docker.com/get-started/"&gt;guide&lt;/a&gt;, perfectly sums all this up.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--2ggxEN9B--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://thepracticaldev.s3.amazonaws.com/i/r5u7h215ukse6asklz97.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--2ggxEN9B--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://thepracticaldev.s3.amazonaws.com/i/r5u7h215ukse6asklz97.png" alt="Docker vs Virtual Machines"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  How to Dockerize a project
&lt;/h2&gt;

&lt;p&gt;Building an app using Docker can be summarized as below&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Create a &lt;code&gt;Dockerfile&lt;/code&gt; in the root of the project&lt;/li&gt;
&lt;li&gt;Create Image(s) using &lt;code&gt;docker build ...&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Run Containers from those Images using &lt;code&gt;docker run ...&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Combine those containers in an app, and run them together through a &lt;code&gt;docker-compose.yml&lt;/code&gt; file.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Let's explore these steps further.&lt;/p&gt;

&lt;p&gt;Docker Images are built by giving the Docker software various instructions in a file in the root of our project, called &lt;code&gt;Dockerfile&lt;/code&gt;. A sample Docker file might look like this&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;FROM node:11.14 as builder
WORKDIR /app
COPY package.json /app
RUN npm install
COPY . /app
RUN npm run build

FROM node:11.14
COPY /.env ./ 
COPY --from=builder /app/dist/ ./dist
COPY --from=builder /app/server/ ./server
COPY /packageJson_server/package.json ./
RUN npm install
ENV SECRET="abcdefg"
EXPOSE 8080
CMD [ "npm", "run", "serve" ]
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;The commands in a &lt;code&gt;Dockerfile&lt;/code&gt; are just instructions to the Docker software on what it needs to do inside a project folder (many of them are self-explanatory, in fact), and they come into play once Docker is instructed, from the command-line, to start building an Image out of our project. These command-line instructions to build and run Images are written like &lt;code&gt;docker build --tag=note-taking-app .&lt;/code&gt;, for example. There are a plethora of such commands, which may seem daunting at first, but once we start building a few containers, they become second nature. Most useful and oft-used commands are &lt;code&gt;docker build ...&lt;/code&gt; &lt;code&gt;docker run ...&lt;/code&gt;, &lt;code&gt;docker ps&lt;/code&gt; &lt;code&gt;docker image ps&lt;/code&gt; &lt;code&gt;docker image ls&lt;/code&gt;, etc. &lt;/p&gt;

&lt;p&gt;When we order Docker to make an Image, it reads the &lt;code&gt;Dockerfile&lt;/code&gt; and starts executing the commands one by one. It usually takes some time the first time an Image is generated from a &lt;code&gt;Dockerfile&lt;/code&gt;, but after that, Docker is smart enough to cache the build, and on subsequent builds it reads the dependency list from the manifest files (&lt;code&gt;package.json&lt;/code&gt; or &lt;code&gt;requirements.txt&lt;/code&gt;), and if unchanged, just uses the cached version of those dependencies. &lt;/p&gt;

&lt;p&gt;Another feature is the &lt;strong&gt;multi-stage builds&lt;/strong&gt;, in which &lt;em&gt;artifacts&lt;/em&gt; from the first build (files &amp;amp; folders that will not be used in production, like &lt;code&gt;node_modules&lt;/code&gt; folder) are discarded in the next stage of the build, thus keeping the builds lean and clean. So, in the &lt;code&gt;Dockerfile&lt;/code&gt; pasted above, I first build the front-end of the application (&lt;code&gt;npm run build&lt;/code&gt;), and then in the next stage, only copy over the build (&lt;code&gt;dist/&lt;/code&gt;) folder, thus leaving the front-end &lt;code&gt;node_modules&lt;/code&gt; behind, which will not be included in the final build. Thus the container becomes as lightweight as it can possibly be, consisting of only those files needed at runtime.&lt;/p&gt;

&lt;p&gt;Once an Image is built, we can make as many containers out of them as we please, by running the command &lt;code&gt;docker run (Name/ID of Image) (params)&lt;/code&gt; (we can provide various parameters, which are not relevant to this post).&lt;/p&gt;

&lt;p&gt;Multiple containers can be run simultaneously using a &lt;code&gt;docker-compose.yml&lt;/code&gt; file. Certain conditions may be specified in the &lt;code&gt;docker-compose.yml&lt;/code&gt; file, conditions like which container should start first, how containers communicate to each other and to the outside world, what folders (volumes) those containers are allowed to access, etc. A sample &lt;code&gt;docker-compose.yml&lt;/code&gt; file looks like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;version: '3'
services: 
  db:
    image: 'alinisarahmed/note-app:my-mongo'
    ports:
      - "27017:27017"
    volumes:
      - /data/db:/data/db
  nodejs:
    build: .
    image: 'alinisarahmed/note-app:v2'
    command: npm run serve
    ports: 
      - "3000:3000"
    depends_on:
      - db
    links:
      - db
    environment: 
      - MONGO_HOSTNAME=mongodb://db:27017/test-db
      - PORT=3000
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;h2&gt;
  
  
  Advantages of Docker
&lt;/h2&gt;

&lt;p&gt;So, What's the point of doing all this "Containerization", you ask? Running applications as containers have many benefits, as summarized below:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Containerization provides an abstraction from the environments in which apps are made and run. This decoupling engenders consistency, standardization, and parity, and allows developers to create predictable environments for their apps to run anywhere. Developers can focus on the code without worrying about the system that it will ultimately be running on.&lt;/li&gt;
&lt;li&gt;It helps us avoid dependency issues such as clashes between dependencies, dependency variations between systems, or situations like one program altering the version of a dependency, which renders another program useless.&lt;/li&gt;
&lt;li&gt;It takes the "Module" model of software development to the level of app deployment, we can utilize Images that other people have made and integrate them seamlessly into your own apps, without any worry.&lt;/li&gt;
&lt;li&gt;Docker manages to reduce deployment to seconds (provided you use a good container orchestrator, more on this later). This is due to the fact that it creates a container for every process and does not boot an OS.&lt;/li&gt;
&lt;li&gt;Docker enables easy version control of the production builds, making it easy to rollback to a previous working image of the production build.&lt;/li&gt;
&lt;li&gt;Docker ensures apps can be removed cleanly from a system, since, once an app container is deleted, all its associated dependencies are gone from our system as well without leaving any temporary or configuration files on the host OS. &lt;/li&gt;
&lt;li&gt;Docker also ensures that each application only uses resources that have been assigned to them.&lt;/li&gt;
&lt;li&gt;Last, but not the least, another benefit of using Docker is security. From a security point of view, Docker ensures that applications that are running on containers are completely segregated. No Docker container can look into processes running inside another container.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Deployment, Container Orchestration, and Cycle
&lt;/h2&gt;

&lt;p&gt;So, we have our app containerized, how do we deploy it?&lt;/p&gt;

&lt;p&gt;One way to deploy containers is to deploy them on a cloud server (a computer running on, well, cloud), monitored by a Container orchestration system. One of the best-known container orchestration systems is &lt;a href="https://kubernetes.io/"&gt;Kubernetes&lt;/a&gt;, another is &lt;a href="https://cycle.io"&gt;Cycle&lt;/a&gt;. We will not be comparing these systems here, but the ease with which I was able to set up a server and deploy my app (consisting of two containers, one for MongoDB and another for Node/Express JSON API and a React/TypeScript Front-end), without knowing jack about orchestrators, clouds, servers, and other dev-ops paraphernalia is a testament to Cycle's claim that a developer can now do what used to take an entire dev-ops team (heck I'm not even a professional developer yet!). You have to try it to believe it, and if you do want to try it, read on.&lt;/p&gt;

&lt;p&gt;Cycle claims to be a container orchestration system that promises to deliver, easing the process of container management for us by letting us focus solely on the code, and not on infrastructure. User the hood, CycleOS utilizes a runtime which implements the &lt;a href="https://www.opencontainers.org/"&gt;&lt;strong&gt;Open Container Initiative&lt;/strong&gt;&lt;/a&gt; spec, allowing Cycle to support a variety of container technologies. Cycle offers us a &lt;strong&gt;GUI Dashboard&lt;/strong&gt;, from where one can easily control how the containers are grouped and deployed. It currently partners with Vultr and Packet, both cloud service providers, a company that provides &lt;strong&gt;Infrastructure&lt;/strong&gt; to host our containers. There is also a feature in pipeline where users will be able to connect their own infrastructure to the Cycle dashboard. If more fine-grained control is needed, Cycle also offers a REST-based API. &lt;/p&gt;

&lt;p&gt;Signing up and setting up a server with Vultr was a breeze, and the same was the case for Cycle. These services are not free, but, Vultr provides free credit for benign acts like following them on twitter, and Cycle provides $25 credits for the first month. That is enough juice to try out these services and get our feet wet.&lt;/p&gt;

&lt;p&gt;Deploying a server, and then deploying an app through Cycle on that server, proved to be a really painless task. Rather than setting up everything from a command-line, we work from inside the GUI Dashboard. This is how the whole process goes: &lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Select a Cloud service provider, (currently Vultr and Packet, soon AWS will be available too). &lt;/li&gt;
&lt;li&gt;Select a server size (called Infrastructure), based on your needs and budget.&lt;/li&gt;
&lt;li&gt;Upload Docker Images from a repository like Docker Hub.&lt;/li&gt;
&lt;li&gt;Make &lt;strong&gt;Stacks&lt;/strong&gt; out of Docker Images (a stack is a group of images; it is an atomic unit, a bit like Images themselves), specified by a &lt;code&gt;cycle.json&lt;/code&gt; file, called a stack file (more on this below).&lt;/li&gt;
&lt;li&gt;Deploy the Stacks to an &lt;strong&gt;Environment&lt;/strong&gt;, which automatically builds a private, encrypted network on which the containers communicate, and starts other services.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;The only challenging aspect of the whole process for me was the creation of &lt;code&gt;cycle.json&lt;/code&gt; file, however, previous experience with how &lt;code&gt;docker-compse.yml&lt;/code&gt; file helped here, since &lt;code&gt;cycle.json&lt;/code&gt; is basically a JSON version of the &lt;code&gt;docker-compose.yml&lt;/code&gt; file, with some added options specific to Cycle. In the future, users will be able to generate &lt;code&gt;cycle.json&lt;/code&gt; file automatically from a &lt;code&gt;docker-compose.yml&lt;/code&gt; file.&lt;/p&gt;

&lt;p&gt;Once an Environment is created, we are led to the full real-time GUI dashboard, which looks like this&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--TRXj_wIi--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://thepracticaldev.s3.amazonaws.com/i/v64qe2pw5q2pdoqb8u1x.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--TRXj_wIi--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://thepracticaldev.s3.amazonaws.com/i/v64qe2pw5q2pdoqb8u1x.png" alt="Cycle GUI Dashboard"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;From the dashboard, one can easily start, stop or scale containers as they wish. Have an updated Image for one of your containers? That is just a few clicks away and can be done effortlessly right on the dashboard. Worried that an update might crash your whole app? Cycle has the capability to automatically fall-back to last known working configuration in case of failure. Each Environment also comes with services like VPN (Virtual Private Network), Load Balancer, and Discovery (a DNS service), which are containers themselves. In the future, users will be able to provide their own "services" containers and thus gain even more control over how their Environment behaves.&lt;/p&gt;

&lt;p&gt;Overall, it really was a delightful experience seeing my simple two-container app deployed so quickly. I'm sure that as the number of containers increase and complexity of an application grows, Cycle is THE platform you need to manage the whole system. Read more about this technology on their &lt;a href="https://docs.cycle.io/"&gt;documentation&lt;/a&gt; page.&lt;/p&gt;

&lt;h2&gt;
  
  
  Further Resources
&lt;/h2&gt;

&lt;p&gt;If you want to learn more about the container technology, following links helped me tremendously.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://docker-curriculum.com/"&gt;One of the best resources out there&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://docs.docker.com/get-started/"&gt;The Official Docker guide&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://medium.freecodecamp.org/docker-simplified-96639a35ff36"&gt;This great medium post which goes into a lot more detail&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://youtu.be/pGYAg7TMmp0"&gt;This awesome 2-part video series on Docker&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://dev.to/peterj/run-a-react-app-in-a-docker-container-kjn"&gt;How to run a React App in a Docker Container&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://www.digitalocean.com/community/tutorials/how-to-build-a-node-js-application-with-docker"&gt;Another great tutorial, from Digital Ocean&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;




</description>
      <category>docker</category>
      <category>cycle</category>
      <category>containers</category>
      <category>orchestration</category>
    </item>
  </channel>
</rss>
