What is Docker? | How Docker Works? | Learn Docker Basic Commands

By | May 23, 2019



Docker is an open source devops tool which is designed to create, deploy and run your applications on lightweight containers.

Docker works same as virtual machine(VM) does. But unlike VM instead of creating a new operating system, docker allows applications to use host operating system.

Docker is available for Windows, Linux MacOS.

Docker container allows developers to package up an application with all libraries and dependencies, and deliver it as one package.

Today docker is used because of its lightweight container which gives a significant performance boost and  also reduces the size of applications.

 




How Docker Works?

To deploy application on container docker run image on container. Docker pulls this image from docker hub. 

Docker hub is cloud based version of docker repository where user can store their aplication’s images. User can make this repository either public or private based on their use.

Whenever any user want to deploy an image(say sql) on container in his local machine(or server), he will run docker run command.

docker run imageName

Example: sudo docker run mysql

When you will run this command, it will look for an image mysql in local registery, if it doesn’t find this image in local registery then it will try to pull it from docker hub. 

If an image with name mysql exists on docker hub then it will be pulled to the local machine and will run on caontainer.

Here you can see, to run mysql database in container on your local machine you only need to run a simple docker run command. No need to go throw download  and intsallation process. It makes docker simple and easy to use.

Install Docker On Your Linux Machine

To install docker on your linux machine enter following command in terminal –

1. sudo apt update
2. sudo apt install apt-transport-https ca-certificates curl software-properties-common
3. curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add –
4. sudo add-apt-repository “deb [arch=amd64] https://download.docker.com/linux/ubuntu bionic stable”
5. sudo apt update
6. apt-cache policy docker-ce
7. sudo apt install -y docker-ce
8. sudo systemctl status docker 
If sudo systemctl status docker gives you status running, it means docker is install successfully.


You can also type docker –version to check if docker is installed successfully.

   

 

Basic Docker Commands For Beginners

Here I am going to tell all basic docker commands which you should know as devops engineer.

Docker Image Commands

sudo docker search imageName

This command will search image on docker hub and will list all matches.

sudo docker search mysql


 This command will pull image from docker hub and will store it on your local registery.

 sudo docker pull imageName

 Example: 

Type this command –


sudo docker images

It will list all images stored in your lcal repository –
You will see no image with name ubuntu:14.04 exists there.

Now type –


sudo docker pull ubuntu:14.04

Now type this command again –


sudo docker images 

You will see an image with name ubuntu:14.04

Docker Container Commands

Now run this command to check how many containers are running.

sudo docker ps

Now run this command to to run your image  ubuntu:14.04 on container

sudo docker run ubuntu:14.04

 
Now check how many containers are running. 

sudo docker ps

Docker Start/Stop Commands

To stop this container type this command

 sudo docker stop containerId

To see all running and stopped container

 sudo docker ps -a
 

To stop this container type this command

 sudo docker start containerId

Docker Kill and Remove Commands

To kill the container run this command –

sudo docker kill containerId




To kill all running and stopped containers use this command 

 sudo docker kill $(docker ps -aq)


Where $(docker ps -aq) will list the container id of all running and stopped containers and docker kill $(docker ps -aq) will kill those containers.



To remove a container run this command –

sudo docker rm containerId

To remove a container focefully run  – 

sudo docker rm -f containerId

To remove an image from local machine the container run-

sudo docker rmi imageName

Docker Image and Container Details Commands

docker stats command shows the stastics of all runnng containers in terms of cpu usage %, memory usage, memory limit etc. – 

sudo docker stats


docker history command will shows a history of an image – 
 
sudo docker history imageName



docker inspect command gives in depth details(port, ip, memory, cpuQuota, hostname etc.) of a container – 
 
sudo docker inspect containerId




Use docker logs command to see all logs of a container – 
 
sudo docker logs containerId






Thank You For All Your Time!
Hope you like this quick tutorial on docker.

Please let me know how was it in comment.

Leave a Reply

Your email address will not be published. Required fields are marked *