Unlike the Virtual Machine in Linux which uses all the resources of the host machine, Docker is a platform that makes it easier to create, deploy, and use an application using containers. A container contains an application with all parts of it needed such as libraries and other dependencies and ships it as one package.
Instead of using dedicated resources, it shares kernel and other resources of the host system. There are two versions of the Docker – Docker community edition (Docker CE) and Docker enterprise edition (Docker EE). Docker CE is a community-supported version of docker and available free to use. Ahead in this article, we will see more about the installation and usage of Docker CE.
What is Containerization?
Containerization is a process that encapsulates an application in a container with its own operating environment. It is a kind of lightweight alternative to full machine virtualization. The benefits of using this are you can run the application on any physical machine without worrying about dependencies.
What were the problems prior to Docker?
So before we dive into the installation process and practical usages of docker, We should understand that what problems developers were facing before docker and what were the existing solution to them-
- Configuration changes from developer to developer machines, So a single project, might take some time to properly set up for testing
- It was a nightmare to resolve the dependency issues on the server-side when the code is ready to be published
- Scaling of an application was much more painful
Vagrant box vs Docker
Vagrant boxes solve some of these problems, Vagrant boxes are portable virtual machine-based boxes with a complete environment to run an application. These are easy to get started. Below are some points which show the comparison between the vagrant box and docker –
- Vagrant boxes are based on virtual machines while docker uses the same kernel code as host OS
- These are difficult to scale up while docker provides a single command to scale as many as docker containers as we like
- It has a restricted environment based on virtual machines. The communication between host and guest was difficult while Docker provides the best way to deal with configuration conflict in different environments without impacting too much to host machine
Installing Docker CE in Ubuntu/Linux Mint
Before we start the installation we have to set up the official repository of docker to install and upgrade it easily. Follow the below steps –
First, uninstall the older version of docker if it is installed –
sudo apt-get remove docker docker-engine docker.io containerd runc
Then update the apt package index by using –
sudo apt-get update
Install the following packages to allow apt to use a repository over HTTPS –
sudo apt-get install apt-transport-https ca-certificates curl gnupg-agent software-properties-common
Now add the official GPG key of Docker –
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -
Add the Docker stable repository to apt sources, remember to replace the architecture and name of the distribution with your own –
sudo add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu bionic stable"
Now again run the apt update command –
sudo apt-get update
And install the Docker CE in your system by using the following command –
sudo apt-get install docker-ce
Now verify the installation by using –
Using Docker Commands
The syntax of docker command is given below –
docker [options] [command] [arguments]
To see the available options and commands that can be used with docker. Use the following command in your terminal –
Download and use a Docker image
Docker image is an immutable file that is essentially a snapshot of a container. Images are created with build command, and they produce a container when started with the run command. Docker pulls these images from the docker hub. Docker hub maintains a registry because an image can become quite large, these are designed to be composed of other images, allowing a minimal amount of data to be sent when transferring images over the network.
To download an image from docker hub use the following command –
sudo docker pull ubuntu
Display the list of local images use the following command –
sudo docker images
To search an image you can use the following command –
sudo docker search image_name
For example –
sudo docker search ubuntu
Running a Docker Container
For using a docker container first download an image and then execute the following command –
sudo docker run -ti ubuntu bash
To display running containers use –
sudo docker ps
-a option to display all the containers –
sudo docker ps -a
Start a container in interactive mode with –
sudo docker start -i container_name_or_ID
To stop a container use the given command-
sudo docker stop container-ID
Remove a container if it is no longer needed –
sudo docker rm container_id_or_name
Committing changes to a container
When you create, modify, or delete a file or install a package in a container. The changes apply to that container you can start or stop it but if you destroy it using
docker rm command the changes will be lost. You can save the state of a container as a new docker image this will keeps the changes made by you. Suppose we installed Nginx server on existing ubuntu container now to save the state of this container as a new docker image we will use the following command –
docker commit -m "description of image" -a "Author Name" container_id repository/new_image_name
For example –
docker commit -m "Ubuntu with Nginx installed" -a "lalit" 35b25c1729bf ubuntu-nginx
-m is for commit message and
-a is used to specify the author.
Now again execute the following run to display the list of docker images –
sudo docker images
here you can see the created image is saved with the name
In this article, you understand the docker and its uses now if you are intended to know more about it you can follow the official documentation of docker.