Saturday, August 6, 2016

Getting Started with Docker for the Node.js Developer_part1


Difficulty level: Beginner

Requirements: Mac OS X (This tutorial assumes you're using a Mac, but you can find installation instructions for Windows or Ubuntu and skip ahead the Setup section)

Docker has just celebrated its 2nd birthday, but it's still a "new" powerful piece of technology. A lot of developer friends that I talk to have either heard or read about it but haven't actually used it. It lets you do really cool things like quickly test your app in development with the exact same environment as in QA/Test/Production, or share that app with other developers for a quick a painless onboarding. A commonly used analogy for Docker is to compare it to actual real-life containers or lego bricks: it provides a fundamental unit, and with it a way for an application to be portable and moveable, regardless of hardware.
In this tutorial, I'll give a quick overview of what Docker is and why you might want to use it, how to install it, and then we'll work on setting up a Node container and creating an express starter app inside it. This is a long tutorial! The official Docker getting started guide gets you up and running quicker, what I aim to do here is explain what's happening on each step along the way.
What we’ll cover:
  • Introduction (What's Docker and why use it)
  • Installation
  • Docker Hub and Dockerfiles
  • Docker Pull: Pulling an Ubuntu image
  • Docker Run: Running our Ubuntu image and accessing the container
  • Docker Commit: Installing node, npm, express and committing the changes
  • Docker Push: Pushing our container back so other people can use it
Notes:

I'll be referring to commands executed in your own terminal with:
  1. $ command
And commands inside a container with:
  1. $ root: command
Introduction

You've probably heard of Docker by now. Every day there's some front-page HackerNews mention of it, or you see people on Twitter/IRC talking about it. Its popularity has grown enormously in the past couple years, and most cloud providers already support it. If you are curious about it, but still haven't tried it out, this tutorial is for you. ☺
Okay, so what is Docker? Well, Docker can be a reference to a few things:
  • Docker client: this is what's running in our machine. It's the docker binary that we'll be interfacing with whenever we open a terminal and type $ docker pull or $ docker run. It connects to the docker daemon which does all the heavy-lifting, either in the same host (in the case of Linux) or remotely (in our case, interacting with our VirtualBox VM).
  • Docker daemon: this is what does the heavy lifting of building, running, and distributing your Docker containers.
  • Docker Images: docker images are the blueprints for our applications. Keeping with the container/lego brick analogy, they're our blueprints for actually building a real instance of them. An image can be an OS like Ubuntu, but it can also be an Ubuntu with your web application and all its necessary packages installed.
  • Docker Container: containers are created from docker images, and they are the real instances of our containers/lego bricks. They can be started, run, stopped, deleted, and moved.
  • Docker Hub (Registry): a Docker Registry is a hosted registry server that can hold Docker Images. Docker (the company) offers a public Docker Registry called the Docker Hub which we'll use in this tutorial, but they offer the whole system open-source for people to run on their own servers and store images privately.
Now that we cleared the different parts of Docker, here are a few reasons why you might want to use it:
  • Simplifying configuration of a development environment
  • Quickly testing your app in an environment similar to QA/Test/Production (less overhead compared to VMs)
  • Sharing your app+environment with other developers, which allows for fast/reliable onboarding.
  • Ability to diff containers (this can be immensely useful in debugging)
Installation

Running a container, and therefore Docker, requires a Linux machine. Since we're using a Mac, that means we'll need a VM. To make the installation process easier, we can use Boot2Docker which installs the Boot2Docker management tool, VirtualBox, and sets up a VM inside it with Docker installed.
Head over to this link to download the latest release of Boot2Docker, and install it (Boot2Docker-1.5.0.pkg at the time this was written):

https://github.com/boot2docker/osx-installer/releases/latest

After the installation is done, go to your Applications folder and open Boot2Docker. That's going to open a new terminal and run a few commands which basically start a VM that already has Docker installed, inside VirtualBox, and then sets a few environment variables so we can access the VM from our terminal. If you don't want to always open Boot2Docker to interact with Docker, just run the following commands:
  1. # Creates a new VM if you don't have one  
  2. $ boot2docker init

  3. # Starts the VM  
  4. $ boot2docker start

  5. # Sets the required environment variables  
  6. $ $(boot2docker shellinit)
Now type in:
  1. $ docker run hello-world
That's gonna make Docker download the hello-world image from Docker Hub and start a container based on it. Your terminal should give you an output that says:
  1. Hello from Docker.  
  2. This message shows that your installation appears to be working correctly.
Awesome! Docker is installed. ☺
(If you have any problems, feel free to ping me or you can find Docker's official installation instructions here)

Dockerfiles and Docker Hub

Before we move forward, I think it's important to understand what happened when we executed $ docker run hello-world so you're not just copy+pasting the next instructions.docker run is the basic command that we use to start a container based on an image while passing commands to it. In this case, we said, "Docker, start a container based on the image hello-world, no extra commands". Then it downloaded the image from Docker Hub and started a container inside the VirtualBox VM based on that image. But where does the hello-world image come from? That's where Docker Hub comes in. The Docker Hub, like we mentioned in the introduction, is the public registry containing container images to be used with Docker, created by Docker, other companies, and individuals. Here you can find the image for hello-world we just executed:

Docker Hub Hello-World Image

Every image is built using a Dockerfile. In the description for the hello-world image, you can find a link to its Dockerfile which only has 3 lines:
  1. FROM scratch  
  2. COPY hello /  
  3. CMD [“/hello”]
Dockerfiles are just text files containing instructions for Docker on how to build a container image. You can think of an image as a snapshot of a machine, and a container as being the actual running instance of the machine. Dockerfiles will always have the format:
  1. INSTRUCTION arguments
So in our hello-world example, we can take a look at the root of the GitHub repo which contains the Dockerfile. The image is being created from another image called "scratch" (all Dockerfiles start with the FROM instruction), then copying the hello file to the root of the system, and finally running hello. You can also find the contents of the hello file here, which contains the output we just saw in our terminal.

Docker Pull: Downloading an Ubuntu image

Now that we know our Docker installation is correctly setup, let's start playing with it! Our next step is getting an Ubuntu image. To find an image we can either go to the Docker Hub website or just run in the terminal:
  1. $ docker search ubuntu
This is going to give a list of all the images containing Ubuntu in its name. This is what's shown in my terminal:


The output is sorted by number of stars in each image repository. You can see that there's an Official and Automated column there.
  • Official images are images maintained by the docker-library project and accepted by the Docker team. That means they adhere to a few guidelines found here, some of which are living in a git repository and that repository being at least read-only so users can check its contents. You can count on those images for working correctly with Docker. Also, contrary to other images where you need to reference them to pull using USERNAME/IMAGE_NAME, these images can simply be referred to in commands by IMAGE_NAME (such as Ubuntu). All of their Dockerfiles can be found in this organization.
  • The automated column refers to Automated Build images. It simply means that the image is being built from a Dockerfile inside a GitHub or BitBucket repository, and it's automatically updated when changes are made to it.
Let's download the official Ubuntu image:
  1. $ docker pull ubuntu
The $ docker pull IMAGE_NAME command is the way to explicitly download an image, but that is also done if you use the $ docker run IMAGE_NAME command, and Docker can't find the image you're referring to.
Written  by Heitor Tashiro Sergent

If you found this post interesting, follow and support us.
Suggest for you:


No comments:

Post a Comment