Feel free to connect!

  • LinkedIn Social Icon
  • Twitter Social Icon
  • Unknown_2

[email protected]

+91-9790724673

Created by Aditya Anand

Search
  • Aditya Anand

Docker 101: Introduction

A beginner’s introduction to Docker & why it’s awesome?

Docker 101, is a new series that I am beginning, to give you guys a closer look at the most buzzing word of the DevSecOps world. This new technology has been revolutionising the way deployment takes place while easing the work for the people at the DevOps department.

I have been working very closely with the DevOps team at the organisation I currently work at. For a guy who comes from a pen-testing background, listening to the word “Docker” day in and day out became frustrating to a point where I decided to just leave security all together for some time and get involved in the DevOps part of things.

For the past few days, I have been binge-watching Docker tutorials, understanding why is it required, what are its benefits and how it saves time and resources. After putting in the work, I have realised that dockers are here to stay for a long time and how beneficial it is for everyone from development to deployment and management.


Let’s begin!

First, to give you an understanding of what Docker is I need you to remember everything you know about virtualisation or virtual machines. So, let’s recap for a moment and see what virtual machines do. Virtual machines allow you to set up a guest operating system over your current operating system i.e. you can run Ubuntu or Kali Linux machines on your Windows computer without a problem. All you have to do is download the .iso files for the guest OS and then install it in your virtualisation software like VMware, VirtualBox, etc. A pictorial representation of it would look something like this.

Now that you have a clear idea of what virtualisation is let’s proceed ahead and see what a Docker is and why do people keep comparing it with virtual machines. First, let me give you a pictorial representation of Docker so that you can understand easily.

Now, scroll back and forth and try to spot the differences between Docker and Virtual machines by yourself before I begin with my explanation.

Docker is nothing but a process that runs on the Host OS, which in turn helps us spawn containers inside of it. These containers allow developers to create a package for an application which contains the precise set of libraries, dependencies and toolset required to run the application. This is even more beneficial as the docker image of these containers, similar to .iso file for virtual machines can be shipped in one package wherever the application needs to be deployed. It can be any Linux machine irrespective of its flavour or the applications installed on the system and everything will work the way it should right out of the box.

Docker and Virtual Machine setup are similar only till the Host OS, but after that, they differ a lot and it is these differences that make all the difference let me point out how.

If the Docker requires only a specific set of libraries, toolset and dependencies it will consume way less storage than a standard virtual machine. One more thing as there are fewer dependencies and libraries required for Docker hence the startup time for a virtual machine is extremely slow when compared to the startup time of Docker. All these benefits and we haven’t even talked about its greatest positive point and that is that Docker enables us to package an entire application and ship it to any Linux machine all over the globe without ever having to worry about integration and not having to spend hours and hours troubleshooting why your software ain’t working the way it should on a certain client’s system across the globe in a completely different timezone. Let me sum it all up for you in an image from edureka’s video.

Now that we have clearly shown why Docker is better than Virtualisation solutions in a lot of ways let’s actually see how to work with dockers and understand how this amazing tech works.


Docker Commands

Let’s go through the different docker commands and what functions are they used to perform. You can open a new tab and practise these commands alongside while going through the article, click here this is a website designed for Docker beginners to learn about Docker and how it works.

$ docker run
$ docker run <docker-image>
e.g. 
$ docker run ubuntu
$ docker run nginx

“run” command is used to boot up or initiate the docker image.If the image is not locally present then it downloads the image.If the image is present locally then it executes it from the local file.

$ docker run kodekloud/simple-webapp

By default the docker containers run in an attached mode i.e. they run in the foreground.

$ docker run -d kodekloud/simple-webapp

Adding the “-d” parameter if you need the docker to run in the background and access them later on.

$ docker run redis:4.0

Appending the version “redis:4.0” is to signify which exact version is required.4.0 here is the tag.

$ docker run -p 80:5000 kodekloud/simple-webapp

We use “-p” to map ports of the host system to that of the docker.The port on the left side is the port of the host system and the one on the right is the port on the container.

$ docker run -v /opt/datadir:/var/lib/mysql mysql

This “-v” is used to map data volumes from data inside the docker to data directories in your local system. As before the directory on left in the one present in the host system and the one on the right is the container. Using this command the data from the docker container is saved on the given directory on the host system and so the data is protected even if the container is deleted.

$ docker run -e APP_COLOUR=blue kodekloud/simple-webapp

The “-e” tag is used to input the values of the environment variables. To check what environment variables are being utilised by a container, you can run the inspect container to see all the details.

$ docker ps
$ docker ps

It is used to list all docker containers which are currently running. It also provides us with basic stats for these running containers

$ docker ps -a

It is used to list all docker containers. Provides the same detail as “docker ps”

$ docker stop
$ docker stop <container-id> | <container-name>
e.g.
$ docker stop ubuntu
$ docker stop a3772d4a2fed

It is used to stop the running docker container

$ docker rm
$ docker rm <container-id> | <container-name>
e.g.
$ docker rm nginx
$ docker stop a3742d5a2fhd 

It is used to destroy the containers which aren’t currently executing. This is done to stop the containers from just lying around & taking up space

$ docker images
$ docker images

It is used to list all the images you have present on your local system

$ docker rmi
$ docker rmi <image-name>
e.g.
$ docker rmi nginx
$ docker rmi ubuntu

It is used to remove the images that are present on our system and are not required by us. To delete the image you must stop all containers that are using the image you want to delete

$ docker pull
$ docker pull

It is used to pull the image from the docker hub. Pulling essentially downloads the image from the docker hub for future use.

$ docker exec
$ docker exec <docker-name> <command>
e.g.
$ docker exec -it ubuntu sh -c "echo a && echo b"

It is used to execute a particular set of commands on a running container. First we need to enter the name of the container and then enter the command we want to be executed like cat /etc/hosts

$ docker attach
$ docker attach <container-name> | <container-id>
e.g.
$ docker attach nginx
$ docker attach a3742d5a2fhd

It is used when you want to attach your terminal to a docker that is running in the background.

$ docker -i | docker -t
$ docker -i kodekloud/simple-prompt-docker

The “-i” tag is used to initialise the interactive shell. When the docker is initialised you can then go ahead and properly interact with it.

$ docker -it kodekloud/simple-prompt-docker

The reason for using “-t” tag is to have a terminal pop upIt is recommended to use both “-i” and “-t” tag to establish proper interaction with the docker

$ docker inspect
$ docker inspect <container-name>

The “inspect” command is used to get detailed knowledge about the container.

$ docker logs
$ docker logs <container-id> | <container-name>

The “logs” parameter is used to view the logs of the particular container.


Conclusion

This article has shed light on the basics of Docker and the command that we use on a daily basis to properly maintain our running containers and to manage our dockers. I have more articles related to Docker that I will be publishing in this upcoming week. Stay tuned for those.


If you enjoyed it please do clap & let’s collaborate. Get, Set, Hack!

Website : aditya12anand.com | Donate : paypal.me/aditya12anand Telegram : https://t.me/aditya12anand Twitter : twitter.com/aditya12anand LinkedIn : linkedin.com/in/aditya12anand/ E-mail : [email protected]


Credits

To present you with this content I had to go through a lot of video content and lab environments.

Docker for Beginners — KodeKloud

Docker for Beginners — Lab Environment

Docker Tutorial for Beginners — Edureka

0 views