Docker has become a must-know tool for developers. It is an infrastructure as a service (IaaS) that allows you to build, test and deploy applications swiftly and is one of the most popular DevOps tools used for Containerization. It allows you to create containers that help in the portability and isolation of software and reduces the load on the computer for running them. According to the Stack Overflow survey 2023, Docker is the top-used tool among all the respondents. This blog covers the basic concepts related to containers, Docker, some basic Docker commands and some terms related to Virtualization.
What are Containers?
A Container is an isolated environment in a computer. It provides a lightweight virtualization technology that allows users to run a software environment without installing required dependencies or packages. For example, you can run a Express server in a container, without even installing Node.js on your computer. This solves problems related to the configuration and installation of required modules and packages.
A container can also be termed as a bunch of processes running in a shared kernel. All containers run on a shared kernel, meaning, they share the kernel of the host computer. This allows us to run multiple containers in one operating system.
Containerization is achieved through Linux Namespaces, which are responsible for process and resource isolation. These namespaces isolate the container from the remainder of the operating system, which helps in resource management and improves security.
Difference between Containers and Virtual Machines
Virtual machines are used for running flow-blown operating systems in a computer. VMs use a hypervisor, which interacts directly with the hardware directly at a lower level to manage and virtualize the resources of the computer. Whereas, Containers interact with the kernel of the operating system and hence they are lightweight compared to virtual machines. However, containers cannot do all that a Virtual machine does. For example, VMs have kernel-level control where you can configure the kernel for specific tasks.
Docker and basic terms
Docker is a platform that helps you run containers on your computer. It is a tool that allows the masses to run containers on their computers.
Some of the basic terminologies related to Docker are:
Docker Image: It is a TAR file or an archive of the file system of the container. We can create multiple containers from an image. It contains all the required libraries and files to run the container.
Docker File: The Docker file contains a set of instructions to create a Docker Image.
Docker Hub: It is a public registry that contains images of different applications. It contains images created by other users and organizations. It is the GitHub for containers.
Running your first Container
To run your first container, you need to install Docker Desktop on your computer. To check your installation, open your command prompt and run
docker --version
If the version is visible, open Docker Desktop(this is done to start the Docker Daemon - which is a process that is used to manage containers and images) and you are good to run your first container.
In this tutorial, we are going to run an Ubuntu image inside our container and create a directory inside it.
To run an Ubuntu image, open up your terminal and run
docker container run -t ubuntu top
Let’s break down the command
docker container run
is the default command for running a container.-t
allows to run the container interactively.ubuntu
is the name of the image.top
is a Linux command that prints the processes on a system in order of resources used.
You have successfully run your first container.
To activate an interactive bash terminal, open another window or tab of the command prompt and run
docker container exec -it containerID bash
-it
is used to create an interactive terminal.
To access the containerID
, run
docker container ls
And, you will get the list of containers running on your computer. You can select the ID of the container that has the image name “ubuntu”
After running the command, the command prompt will display a bash terminal with a user id with the username and the id of the container. For example:
root@1938413902: /#
In there, you can run ls
to see the list of directories in it.
To create a new directory, you can run
mkdir dirname
And, run ls
again. In the list, the directory we just created will be visible.
To stop the container, run
docker container stop containerID
Outro:
Hope you learnt something new from this blog and tried running your first container. If you have any feedback on the blog, feel free to share it with me and follow for more blogs like this one. Subscribe to the newsletter so you don’t miss out on any new blogs I’m writing.
Thanks for reading!! See you soon!
Images conceptualized and designed by Athithya