Docker For beginners Part 2 by ikalamtech.comDocker For beginners Part 2 by ikalamtech.com
Docker For beginners Part 2 by ikalamtech.com

We all know the definition of Docker. Docker is a software that packages applications into standardised units called containers that have everything required by the software including libraries, system tools, code and runtime. But this doesn’t help us truly understand the beauty of docker. This is what a clueless MBA educated executive will include in their boring presentations that we all are forced to listen to. In the previous blog, we understood the basic introduction of Docker & how it helped to resolve ever existing conflict between Bob(dev) & Alice(QA). In this blog we will deep dive more & will try to understand the concepts and architecture of Docker.

Containers and Images

Docker Containers

Hopefully all of you would have eaten sandwich. There are two ways to make a sandwich. First way is that you harvest the wheat crop, grind it into powder, make a dough out of it and bake it into bread. And then use this bread to make your sandwich. Second way is you buy the bread and make the sandwich and eat it. Everything needed out of the “bread” to make the sandwich is contained within the packet of the bread. The user need not worry about anything related to bread and can focus completely on making the sandwich (and eating it too!).

You can imagine “Docker containers” to be like that packet of bread, but for software. The Docker container of a software contains everything needed to install and run it. When we install Docker on a machine, it becomes the Docker Host. Docker creates a logical entity on the host, deploying the application on it, known as a Container in Docker’s lingo.

The Docker container does not have any operating system in it. Instead, it inherits the virtual copy of the process table, network interfaces, and file system mount points from the operating system of the host. Multiple containers share the kernel code of the host’s operating system. This is what makes a Docker container light weight.

Docker containers are isolated from each other. A host can have multiple Docker containers in it if each of the containers have the same Operating System requirements. Docker virtualises the Operating system of the host for the applications. In this way, Docker supports multiple applications with different requirements to be run on the same host, if they have the same operating system requirements.

Docker Image

A docker image is a template using which a Docker container can be created. It contains the application and all the requirements to run that application (dependencies, configurations etc).We can say that the container is an instance of the image that can be run.

How are containers and images used in real time? Let us say you have a backend application. This has a Java Spring Boot based Web server and a MySQL database. Let us say, the developer makes some changes in the Spring Boot application. The developer will then create an image of the application. This image can be used to create a Java Spring Boot container along with the MySQL container. When both these containers are run, the backend is Up and running. With this clarity, let us try to understand the architecture of Docker.

Docker Architecture

Docker follows a client-server architecture. It has three main components

  1. Docker Client
  2. Docker Host
  3. Docker Registry
Docker Host

This is the server side of the Docker tool. This contains a process known as Docker Daemon(dockerd) that is responsible for creating and managing docker images and containers.

Docker Client

This is a CLI tool that executes the docker commands. It sends these commands to the dockerd using Rest API.

Docker Registry

It is a centralised location for storing and distributing the docker images. Just like we have an SVN like GitHub to store and share our source code, we have registries to store and share the docker images of the application. Docker is a public registry that anyone can use and is the default registry.

Docker Commands

Now let us look at some of the most commonly used Docker commands.

Docker Create

We use the create command to create a new docker container. The syntax for this is as follows:

docker create [optons] IMAGE [commands] [arguments]

Here the “[]” brackets denote an optional argument for the command. The docker first checks whether the image of the container is available on the host machine. If it is available, it will create a container from that image. If it is not available, it will download the latest image from the docker repository and create a container from that image. For example:

docker create ubuntu

The above command will create a ubuntu container. If the container was created successfully, it will return the id of the container. Every container has a unique id using which we can perform various operations on the container like starting, stopping etc.

docker ps

This command allows us to view all the containers that are presently running in the host.

docker ps

If one has to view all the container that was created on the particular host, irrespective of whether they are running or not, one can use the following command.

docker ps -a

Let us understand the output of the docker ps command.

for example
  • CONTAINER ID: A unique alpha-numeric identification of all the containers.
  • IMAGE: The name of the docker image used to create the docker container
  • COMMAND: A command that needs to be executed when the container is started
  • CREATED: Amount of time elapsed since the container was created
  • STATUS: The current status of the container, whether it is running or stopped. When the container is running, it will be displayed as “Up” along with the time period elapsed. Similarly, if the container has stopped ,it will be displayed as “Exited” along with the reason for exit and the time elapsed.
  • PORTS: Any port mapping defined for the container is displayed.
  • NAMES: Apart from the CONTAINER ID, each container is also assigned a unique name. We can refer to a container either using its container ID or its unique name. Docker automatically assigns a unique silly name to each container it creates. But if you want to specify your own name to the container, you can do that by including the 
    -- name (double hyphen name) option to the docker create or the docker run (we will look at the docker run command later) command.

docker start

This command starts any Stopped docker container. The container can be started by specifying the first few unique characters of the the container id or by specifying their name. The Syntax is as follows:

docker start [options] CONTAINER ID/NAME [CONTAINER ID/NAME…]

Some of the examples of using this command is as follows:

docker start 30143

Here, Docker starts the container beginning with the container ID 30143.

docker start i_am_kalam

Whereas in this example, Docker starts the container named i_am_kalam

docker stop

This command stops any running docker container. The container can be started by specifying the first few unique characters of the container id or by specifying their name.

The syntax for this command is as shown below:

docker stop [options] CONTAINER ID/NAME [CONTAINER ID/NAME…]

Some of the examples of using this command is as follows

docker stop 30143

Here Docker stops the container beginning with the container ID 30143.

docker stop i_am_kalam

Whereas in this example, Docker stops the container named i_am_kalam

docker restart

Use this command to restart any running Docker container. Specify the first few unique characters of the container ID or its name to restart the container.

The syntax for this command is as shown below:

docker restart [options] CONTAINER ID/NAME [CONTAINER ID/NAME…]

Some of the examples of using this command is as follows

docker restart 30143

Here Docker restarts the container beginning with the container ID 30143.

docker restart i_am_kalam

Whereas in this example, Docker restarts the container named i_am_kalam

docker run

This is a combination of create and start command. It creates the container and starts it at one go.

The syntax for this command is as shown below:

docker run [options] IMAGE [commands] [arguments]

Some of the examples of using this command are shown below:

docker run ubuntu

In this, docker will create and run the latest image of Ubuntu. But it immediately stops it and we will not be able to interact or execute any command on it. For that purpose, we need to add the “-it” parameter to this command. This will provide us with the option to run our commands inside the started container

docker run -it ubuntu

In order to come out of the container, you need to type exit in the terminal.

docker rm

Use this command if you want to delete a stopped container. Ensure the container is stopped before you attempt to delete it.

The syntax for this command is as shown below:

docker rm [options] CONTAINER ID/NAME [CONTAINER ID/NAME...]

Some of the examples of using this command are shown below:

docker rm 30f143 i_am_kalam

Using the above command,we are deleting two containers in one stroke. The first one is the container with the id “30f143” and the second one is the container with the name i_am_kalam

docker images

This is similar to the docker ps command but for images. This command lets you list all the docker images currently in your host. The syntax is as follows:

docker images

When you type the above command you will get the following set of outputs. Let us glance into the output of the above command.

For example
  • REPOSITORY: Unique name of the docker image.
  • TAG: Tag represents the version of the image. This is usually a word or set of numbers.
  • CREATED: Time elapsed since the creation of the image. SIZE: Size of the concerned image.
  • SIZE: Size of the concerned image.
docker rmi

This command removes the image from the host. The syntax for this command is as shown below:

docker rmi [options] IMAGE NAME/ID [IMAGE NAME/ID...]

Some of the examples of using this command are shown below:

docker rmi informix

The above command removes the image named informix from docker host.

docker rmi python ubuntu

Now, this command removes the images named python and ubuntu,

docker rmi 143e81

And this command removes the image starting with the image ID 94e81.

Conclusion

Hopefully, you have understood the depth and beauty of the wonderful tool called docker. There are many more commands . Its important to play with these commands.You can visit the website for practising these commands. Once you are comfortable with this, we will learn how to actually convert a real time application into a docker container in our next blog.

By Admin

Leave a Reply

Your email address will not be published. Required fields are marked *