Getting started with Docker

Saurabh Srivastava

10 Jan 2017

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 virtualization.

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 make it easy to dynamically manage workloads, scaling up or tearing down applications, in near real time.

 

One of the main benefits of 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 interchangeably, 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 required, 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.

docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES

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

docker run -d httpd -name test
Unable to find image 'httpd' locally
Pulling repository httpd
5c82212b43d1: Download complete
e2a4fb18da48: Download complete
58016a5acc80: Download complete
657abfa43d82: Download complete
dcb2sc3w3d16: Download complete
c79a417d7c6f: Download complete
abb90243122c: Download complete
d6137c9e2964: Download complete
85e566ddc7ef: Download complete
69f100eb42b5: Download complete
cd720b803060: Download complete
7cc81e9a118a: Download complete

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 running

docker ps
CONTAINER ID IMAGE        COMMAND       CREATED      STATUS PORTS           NAMES
f6d31sa07fc9 httpd:latest 4 seconds ago Up 3 seconds        443/tcp, 80/tcp test

Docker Images

Images are one of Docker’s basic and key features and are 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 us look back at the output of the docker run execution.

 

 

Break down of Step 2 – Image not found

 

 

docker run -d httpd
Unable to find image 'httpd' locally

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 run we 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 have an image with name httpd, Docker downloaded it from a Docker repository.

 

 

Break down of Step 2 – Downloading the image 

Pulling repository httpd
5c82212b43d1: Download complete
e2a4fb18da48: Download complete
58016a5acc80: Download complete
657abfa43d82: Download complete
dcb2sc3w3d16: Download complete
c79a417d7c6f: Download complete
abb90243122c: Download complete
d6137c9e2964: Download complete
85e566ddc7ef: Download complete
69f100eb42b5: Download complete
cd720b803060: Download complete
7cc81e9a118a: Download complete

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 the learning registry on a 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.

docker kill test
test

You can confirm this by running docker ps again.

docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES

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.

docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS NAMES
f6d31sa07fc9 5c82215b03d1 4 weeks ago Exited (-1) About a minute ago test

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

docker rm test
test

While this container has been removed, we still have an 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.

docker images
REPOSITORY   TAG      IMAGE ID     CREATED
httpd        latest   9fab4645484a 1 days ago

Have a question?

Need Technology advice?

Connect

+1 669 253 9011

contact@hashedin.com

facebook twitter linkedIn youtube