What in the World is Docker?

Hemanthhari2000
featurepreneur
Published in
6 min readMar 30, 2021

Understand the basics of Docker and its real-world applications.

Photo by Ian Taylor on Unsplash

Introduction

Developing applications and deploying them in production is still a time-consuming and complex thing to do as a developer. We need to set all the dependencies, packages, libraries onto the server. The server may have some other OS and we need to build each dependency that is server-specific to run our application. Just by reading this, we get to know that this is a tedious process and requires a lot of time and energy just to set up the server to run our application. So, to avoid such work, Docker was released to ease the process. Docker uses a concept called Containerization, which makes the deployment process effortless.

Overview

Let’s take a look at the contents that we will cover in this article.

  • Docker
  • Docker Images
  • Containers Vs Virtual Machines (VMs)
  • Installing Docker
  • Docker Commands
  • Implementation of Docker
  • Conclusion

Docker

Docker is a set of Platform As A Service (PaaS) products that use OS-level virtualization to deliver software in packages called containers. Containers are isolated from one another and bundle their own software, libraries, and configuration files; they can communicate with each other through well-defined channels. Because all of the containers share the services of a single operating system kernel, they use fewer resources than virtual machines. The software that hosts these containers is called Docker Engine. Docker includes components such as Docker Client, Docker Server, Docker Machine, Docker Hub, Docker Composes, and more.

Docker benefits both Developers and System Admins. It eradicates the machine-specific problem like “Code runs on one PC but not on the other”. Enterprises use Docker to securely built agile software delivery pipelines to ship new application features faster and more securely. It can not only be used for deployment but also for the development process, which makes it more scalable and robust.

Docker Images

A Docker image is a read-only template that contains a set of instructions for creating a container that can run on the Docker platform. It provides a convenient way to package up applications and preconfigured server environments, which you can use for your own private use or share publicly with other Docker users. You can pull some existing docker images from the official docker hub from here.

Note: You need to login into the docker hub account and then search for your images.

Containers Vs Virtual Machines (VMs)

Docker Containers are the lightweight alternatives of the virtual machine. It allows developers to package up the application with all its libraries and dependencies, and ship it as a single package. The best thing Docker offers you is you need not provide any space for storage or allocate any RAM for your application. It will automatically generate storage and space according to the application and its requirements. It does not require any hypervisor as the virtual machine does. Which makes it more scalable and fast.

Whereas a Virtual Machine is a virtual representation of a physical computer. They are referred to as guest machines that use our local hardware to function. Virtualization is the process of creating multiple virtual machines, each with its own operating system (OS) on a single physical machine. A VM cannot interact directly with a physical computer. Instead, it needs a lightweight software layer called a hypervisor to coordinate between it and the underlying physical hardware. The hypervisor allocates physical computing resources such as processors, memory, and storage to each VM. It keeps each VM separate from others so they don’t interfere with each other.

Let’s get our hands dirty as always and start implementing docker.

Note: In this article, we use a Linux-based OS i.e, Ubuntu to run docker because it is easy and fast in Linux-based systems.

Installing Docker

Now let us open our terminal and update your apt packages first using the following and install snap too.

sudo apt update
sudo apt install snap

That’s it for the prerequisite though. Now let’s install docker using snap.

sudo snap install docker

Done. That’s right. We just installed docker in our Ubuntu. That was pretty easy, right?

Docker Commands

Now we’ll see some docker commands that will be useful in the long run.

Note: make sure you use sudo before any docker command or else it won't execute in Ubuntu.

  • List Docker images
docker images
  • List all Docker running containers
docker ps -a
  • List Docker containers
docker container ls
docker container ls --all
  • Stop any Container
docker stop <CONTAINER_NAME>
  • Remove any Container
docker rm <CONTAINER_NAME>
  • Clear or Remove all Docker Containers
docker system prune -a

for more such commands, visit the official Docker website.

Implementation of Docker

Go ahead and create your Dockerfile and pull the existing image from the Docker Hub. After pulling python image from Docker Hub go ahead and install NumPy or any other import you want. As this is an example of how Docker works so let us stick with simplicity here.

Dockerfile

FROM python:latest

we import or pull the existing Python Docker Image from Docker Hub.

ADD . /code
WORKDIR /code

Add our existing working directory to the new directory /code inside the Docker Container.

RUN pip install numpy

Use the RUN command to run any terminal-specific commands like installing numpy using pip.

CMD ["python", "main.py"]

This CMD command is used to run the python command on the Docker terminal. Specify the command that is python to run any python file and the file itself to execute here it is main.py

That’s it, our Docker is set and all we have to do now is create your main.py where you use some NumPy operation (as for this example) or any other operations of your choice.

main.py

Finally, build and run your Docker container using the following command

Build

docker build -t docker-testing:latest .

Run

docker run -it docker-testing:latest sh

That’s it. If your installing and setup was perfect then you should see output saying the following

[[1 2 3 4 5]
[1 2 3 4 5]]
<class 'numpy.ndarray'>

Congratulations! you have successfully set up your docker station and now it is up to your applications to deploy using docker.

Conclusion

In this article, we have seen a detailed introduction about Docker and its uses in real-world applications. We understood the concept of containerization and also to demonstrate its functionality, we have implemented a simple Docker application. So, go ahead and discover new wonders with Docker. I hope this article was useful to you all. Will see you in my next article until then as always code learn repeat …….

Follow for more…

--

--