Getting started with Docker - HashedIn Technologies

Getting started with Docker

Technology - 10 Jan 2017
Saurabh Srivastava

In this blog, we explore the beginner level docker basics. If you’re already familiar with docker basics, please read our advanced blogs on Creating Production Ready Docker Images or Deploying Docker With Compose. This docker basics blog is meant for people only interested in understanding what is Docker and why it is different from virtualisation.

What is Docker

Docker is an operating system container management tool that allows you to easily manage and deploy applications by making it easy to package them within operating system containers. Docker’s portability and lightweight also makes it easy to dynamically manage workloads, scaling up or tearing down applications, in near real time. One of the main benefits in using Docker, and container technology, is the portability of applications. It’s possible to spin up an application on-prem or in a public cloud environment in a matter of minutes.

Containers vs Virtual Machines

Terms “Containers” and “Virtual Machines” are often used interchangabley, however, this is often a misunderstanding. But, both are just different methods to provide Operating System Virtualization.

Standard virtual machines generally include a full Operating System, OS Packages and if reuqired, few applications. This is made possible by a Hypervisor which provides hardware virtualization to the virtual machine. This allows for a single server to run many standalone operating systems as virtual guests.

Containers are similar to virtual machines except that Containers are not full operating systems. Containers generally only include the necessary OS Packages and Applications. They do not generally contain a full operating system or hardware virtualization, that’s why these are “lightweight”.

Virtual Machines are a way to take a physical server and provide a fully functional operating environment that shares those physical resources with other virtual machines.

Whereas, a Container is generally used to isolate a running process within a single host to ensure that the isolated processes cannot interact with other processes within that same system. Containers sandbox processes from each other. For now, you can think of a container as a lightweight equivalent of a virtual machine.

Docker enables creating and working with Containers as easy as possible.

Installing Docker

To get started with docker basics, install docker using package manager like apt-get, yum, etc. Since our example system is running Ubuntu 16.4.1, we will do this using the apt package manager. To avoid confusion during docker basic training, I will avoid installation steps.

After installation, for a start, to check if any containers are running we can execute the docker command using the ps option.

The ps function of the docker command works similar to the Linux ps command. It will show available Docker containers and their current statuses. Since we have not started any Docker containers yet, the command shows no running containers.

Deploying a pre-built nginx Docker

One of my favorite features of Docker is the ability to deploy a pre-built container in the same way you would deploy a package with yum or apt-get. To explain this better let’s deploy a pre-built container running the Nginx web server. We can do this by executing the docker command again. However, this time with the run option.

The run function of the docker command tells Docker to find a specified Docker image and start a container running that image. By default, Docker containers run in the foreground. In order to launch this Docker container in the background, I included the –d (detach) flag.

Step 1 – Trying to run a docker

The run function of the docker command tells Docker to find a specified Docker image and start a container running that image. By default, Docker containers run in the foreground. That means, when you execute docker run, your shell will be bound to the container’s console and the process running within the container. In order to launch this Docker container in the background, I included the -d (detach) flag.

Step 2 See if it’s actually runnning

Docker Images

Images are one of Docker’s basic and key features and is similar to a virtual machine image. Like virtual machine images, a Docker image is a container that has been saved and packaged. Docker however, doesn’t just stop with the ability to create images. Docker also includes the ability to distribute those images via Docker repositories which are a similar concept to package repositories. This is what gives Docker the ability to deploy an image like you would deploy a package with yum. To get a better understanding of how this works let’s look back at the output of the docker run execution.

Break down of Step 2 – Image not found

The first message we see is that docker could not find an image named httpd locally. The reason we see this message is that when we executed docker runwe told Docker to start up a container, a container based on an image named httpd. Since Docker is starting a container based on a specified image, it needs to first find that image. Before checking any remote repository, Docker first checks locally to see if there is a local image with the specified name. Since the system where I “ran” the docker didn’t had image with name httpd, Docker downloaded it from a Docker repository.

Break down of Step 2 – Downloading the image 

This is exactly what the second part of the output is showing us. By default, Docker uses the Docker Hub repository, which is a repository service that Docker community runs. Like GitHub, Docker Hub is free for public repositories but requires a subscription for private repositories. It is possible however, to deploy your own Docker repository, in fact it is as easy as docker run registry. For now, we will skip learning registry on custom repository.

Stopping and Removing the Container

Let’s clean up our Docker environment to conclude docker basic training. We will do this by stopping the container from earlier and removing it. To start a container we executed docker with the run option, in order to stop this same container we simply need to execute the docker with the kill option specifying the container name.

You can confirm this by running docker ps again.

However, at this point we have only stopped the container; while it may no longer be running, it still exists. By default, docker ps will only show running containers, if we add the -a (all) flag it will show all containers running or not.

In order to fully remove the container we can use the docker command with the rm option.

While this container has been removed, we still have a httpd image available. If we were to re-run docker run -d httpd again the container would be started without having to fetch the httpd image again. This is because Docker already has a saved copy on our local system. To see a full list of local images we can simply run the docker command with the images option.

Free tag for commerce

E-book on Digital Business Transformation