About Hariharan Narayanan

Hari graduated from the School of Computer and Information Sciences in the University of Hyderabad. Over his career he has been involved in many complex projects in mobile applications, enterprise applications, distributed applications, micro-services, and other platforms and frameworks. He works as a consultant and is mainly involved with projects based on Java, C++ and Big Data technologies.

What is a Docker container – How to use Docker?

1. Introduction

Containers, in general, and Docker in particular is quickly gaining in popularity. In this post, we will get some basics sorted out about the Docker container. We will learn the meaning of the term Container in general and see specifically how Docker implements them. We will try to execute a few basic commands to understand the bare basics of Docker itself, Docker images and Docker containers. So, let’s start.

2. Containers, what are they?

Operating system designers have always striven to provide secure operating environments to applications running on them. While applications need the harness the complete power of the underlying processing power and hardware for their work, they also need to be secured from harming other running applications. Over time, operating systems have progressed to provide features to virtualize several aspects of the underlying hardware – memory, file-systems, network interfaces, process trees, etc.

These virtualization efforts diverged primarily into 2 directions (among many of course) – hardware virtualization and operating system (OS) level virtualization. Hardware virtualization refers to the creation of virtual machines (on a host OS or even further lower) that virtualizes the underlying physical machine. Users can install complete OS into such virtual machines. OS level virtualization refers to features provided in the kernel (of the host OS) that allows creation of virtual and isolated hosting environments in the user space of the host OS. These virtual hosting environments are also termed Containers.

3. Docker – What is it?

So now, what is Docker? Simply put, Docker is a container technology that enables users to deploy their applications into containers and run them within those containers. Docker enables users to easily package their applications and all of their dependencies into read-only images called Docker images. These images can be easily distributed through a Docker hub. These images can then be downloaded and executed in Docker Containers.

Basic workflow while using Docker

Now that we are briefly introduced to Docker, let us next discuss how we can install Docker and use it to create new containers.

4. Install Docker

Docker can be installed on Linux, Mac, and Windows. Please follow the installation instructions for Docker docs to get them installed in your machine. Once installed you can check if it installed properly by running a Hello world container. Please follow this post to explore a few ways to do so. Now that Docker is installed, let us play around with the basics.

5. Basic commands to understand your Docker installation

We can get an overall information from Docker using the command

$ docker info

Output of the command “docker info”

As you can see from the top 3 lines of the output, the number of total containers and images is 0 since we have a fresh install of Docker.

Once installed you can check the Docker images present using the command

$ docker images

Output of Docker images after a fresh Docker install

It will be empty since we have installed Docker just now.

Once installed you can check the Docker containers present using the command

$ docker ps

Output of docker ps for a fresh Docker install

It will be empty since we have installed Docker just now. Now let us create a container and try to understand how Docker behaves.

6. Basic Docker commands to create containers and images

Let us suppose that we want to create a separate container running Alpine Linux. To do this we simply pull down the official Alpine Docker image shared in the Docker Hub.

$ docker pull alpine:latest

Output of docker pull

If you do not have this Docker image already then you will see a similar output like below. Otherwise you will get a message saying you already have this image. Let us see what Docker tells us about the number of images available locally now.

$ docker info

Partial output of docker info after pulling an image

Now you can see that the number of images is 1 – the Alpine image we just downloaded. However, the number of Containers is still 0 since we have not spawned a new container yet. This image gets stored locally in the folder /var/lib/docker/image

Local location of downloaded Docker images in /var/lib/docker

We can see that the SHA256 id listed is the same as in the output for docker images.

To use this image, we need to create a new container out of this image. We do that by the docker run command.

$ docker run alpine:latest ls -l

Run the Alpine image using docker run

So we spawned a new container out of the alpine image and ran the command ls -l within this container. What you see is the output of this command within the virtual environment provided by the container. By using the docker info command we can see that the number of containers has increased by 1.

Output of docker info after creating a container

Specifically, we see from the above output that the number of stopped containers has increased by 1. That is because when we spawned a new container above from the Alpine image, a new container was created and then the ls -l command was run and then the container was stopped. We can see all containers that are created by the docker ps command like so

Output of docker ps –all

From above, there is now one container created from the Alpine image and it has exited now.

Now let us see what happens if we use the same Alpine image to run another command in a container.

$ docker run alpine:latest echo "hello world"

Output of docker run on Alpine image

“hello world” was printed as instructed. But let us see how many containers are present now

$ docker ps --all

Output of docker ps

We can see that Docker created a new container from the same image to execute the new command. So Docker spawns a new container from an image for every operation unless we explicitly ask it to use the same container to execute different commands.

That is a lot information we get about the image there. We will not discuss them in detail here but you can see that the image id, tag and time-stamp of creation is obvious there. Any containers created from this image is listed next with details of that container. The Docker version used and the Author of the image are also listed. Then follow further details of the image, it’s architecture of the virtual hosting environment, and finally the emulated root file-system for that image.

Now let us try to understand more about the container we created from this image. Let us see what all container we have at present.

$ docker ps --all

Output of docker ps

We see that the containers adoring_bell and desperate_poincare is based on the image alpine:latest. Let us see the metadata of this container more.

There is a wealth of info there. We will not get into the details but discuss it overall here. The first 5 entries give the container id, time-stamp at container creation, the command sent to the container at start (echo “hello world”), and the current state of the container (it has exited now). Next follows the details of the image from which the container was spawned and where it is mapped into the local file-system. The container name assigned is seen (adoring_bell). Then follows the various details of the emulated virtual environments – the virtual networks, virtual mount points, etc.,

7. Summary

We covered some very basic understanding of what a Docker container is in this post. We were introduced to the concept of OS level virtualization and a software container. Then we covered some basic commands to download Docker images and spawn new containers from the image. Later we covered some commands to inspect the metadata that Docker holds about itself, the images and containers.

Newsletter

Join them now to gain exclusive access to the latest news in the Java world, as well as insights about Android, Scala, Groovy and other related technologies.

Email address:

Receive Java & Developer job alerts in your Area

Leave this field empty if you're human:

Join Us

With 1,240,600 monthly unique visitors and over 500 authors we are placed among the top Java related sites around. Constantly being on the lookout for partners; we encourage you to join us. So If you have a blog with unique and interesting content then you should check out our JCG partners program. You can also be a guest writer for Java Code Geeks and hone your writing skills!

Disclaimer

All trademarks and registered trademarks appearing on Java Code Geeks are the property of their respective owners. Java is a trademark or registered trademark of Oracle Corporation in the United States and other countries. Examples Java Code Geeks is not connected to Oracle Corporation and is not sponsored by Oracle Corporation.