Docker is one of my favorite tools. If you are a developer, docker container should be in your skillset. It makes the development & deployment process easier and a lot less time-consuming.
In this article, I will talk about Docker and demonstrate how to run docker containers step-by-step.
Docker was founded by Solomon Hykes and Sebastien Pahl in 2010 and it was launched in 2011. Docker was publicly released in 2013 and in the same year, Docker was made open source.
Initially, Docker used Linux Containers aka. LXC, a method for operating system level virtualization for running multiple operating systems in isolation using the host’s kernel. But after one year, the team developed its own component called
libcontainer and replaced LXC with it.
Today, Docker is natively available on almost all major operating systems including Windows 10. No matter what platform you use or what technology you work with, docker can make development & deployment a lot easier and quicker.
What are Docker Containers?
A Docker container is all the magic of Docker. Docker can package applications and all its dependencies in a virtual container. That package/container image can be run and deployed in any docker-supported environment. Docker uses resource isolation features of Linux kernel and a union capable filesystem to run different operating systems in isolation using a single kernel.
Since 2014, docker has been using its own component called
libcontainer to use the Linux kernel’s resource isolation features directly.
What is Docker image?
A docker image is a package that contains applications and all its dependencies. The difference between a docker image & a docker container is when docker image is run, it’s called Docker container.
Docker Hub is a place where you can find all the docker images. Docker allows you to pull the image locally by using simple commands. Once pulled, images can be run by using another docker command.
How to run Docker image?
In order to run docker on your operating system, you should first set up docker and make sure that the docker service is running.
Once docker is installed and running on your system, here is how to pull docker image from docker hub –
sudo docker run -it --name Ubuntu20 ubuntu:latest
First of all, the above command will look for the image locally, if it does not find any image, then it’ll search, pull, and run the image from Docker hub.
So this command will automatically run the image after pulling it from the docker hub.
How to pull docker image?
If you only want to pull image locally and not run it, use
docker pull command –
sudo docker pull ubuntu:bionic
How to list docker images?
docker pull command will pull the image of Ubuntu (xenial) locally. Now this image is sitting and waiting to be run. To run this image, find the image id and start the image using image id.
sudo docker images
sudo docker run -it --name=Xenial -p 980:80 6e092214ffcd
The above command will run the container.
-it parameter will get you into the container after it’s launched.
--name parameter sets the name of the container that’s visible when you list the containers.
-p parameter publishes container port to host port. In the above command,
980 is the host port and
80 is the container port. Finally,
6e092214ffcd is the id of the image that I just pulled.
How to list docker containers?
You can list all the containers by running the following command –
sudo docker ps
After the container is up and running, the -it parameter will get you into the container but if exit out of the container, you can get back in using the docker exec command.
sudo docker exec -it Xenial /bin/bash
If you ever want to get rid of a container, you can easily stop the container and delete it using the docker rm command. But first, stop the container.
How to stop docker container?
sudo docker stop Xenial
How to remove docker container?
sudo docker rm Xenial
How to remove docker image?
You can also delete any image that you pulled locally. First of all, stop and remove all the containers that are using the image you want to remove. Once done, use the
docker image rm command to delete an image.
sudo docker image rm image_id
Managing docker from CLI isn’t that difficult. But if you want any GUI, there are many graphical tools available to manage docker containers easily on Linux servers. One of such tools is portainer. To learn more about portainer and how to install it on Linux, read the following article on LinuxAndUbuntu. For more docker commands, please head over the docker documentation.
Difference between Docker containers and Virtual Machines
At one point you might ask if dockers create virtual containers, then how docker containers differ from virtual machines. Well, docker accomplishes similar tasks by creating virtual operating systems but in a completely different way which is way better than virtual machines.
Docker containers virtualize the operating system and use a single kernel to function, whereas virtual machines virtualize the hardware and each virtual machine has its own kernel. Containers are way lighter and faster than virtual machines. Because containers are lighter and use fewer system resources, we can run more containers on a single machine.
Docker containers are worth learning. If you are a developer, using containers you can test your applications in different environments without having to affect your host operating system. On Docker hub, you will find a great number of docker images that you can customize as per your requirements.