- DevOps Blogs -

What is Docker? Docker Commands Cheat Sheet with Example for Ubuntu

As Docker is a quite popular and most used tool for DevOps and here in this article, we are going to introduce Docker and its containers. We will also see the important commands that are used in Docker tool and why is it used by the professionals. For this, you must know that Dockers are usually used as a replacement for Virtual Machines. Following are the few reasons due to which Docker is used rather than any virtual machine:

  • Virtual machines take much time to boot. Containers take less time for booting because they use host OS and share the relevant libraries
  • Unlike virtual machines, they do not block host resources.
  • Containers have isolated libraries and binaries for their running applications.
  • Containers are handled by a separate engine that is called a containerization engine.
  • Docker gives a platform for containerization through which containers can be created and run.

Now as it is clear now that Docker provides a platform for containers that are considered over virtual machines. So now it’s time to introduce Docker in detail.

What is Docker? A quick introduction to Beginners

As far as Docker is concerned, Docker is a platform for containerization that can package a user application and its dependencies so that application works seamlessly in an entirely different environment.

Docker works on the containers concept, containers are standard units that can be created to deploy any particular application for any environment. Docker can provide Ubuntu containers or CentOS containers as per the requirements that can work like an operating system. Developers may also get application-oriented containers like Tomcat-Ubuntu containers or CakePHP containers, etc.

What are the Features or Benefits?

Docker tool can be used by system administrators and developers so this is taken as an important part of DevOps. Docker has made the application deployment convenient and quick. Even application deployment flexibility has been increased due to Docker. It is comparatively a smaller tool so little overhead is required. Docker provides following listed features to the users due to which it is a most preferred tool for the developers and administrators:

1). Shared Operating System

As Docker containers do not need to set up their own separate operating system and they can use the existing OS features and storage so they do not require any separate operating system for execution. They are considered as much more efficient than virtual machine because they use shared operating system. Even rest OS can be used by other applications and they use only a part of that.

2). CI/CD

CICD or continuous integration and continuous deployment are two of the most in-demand and imperative features of Dockers. Here, the user need not wait for deployment after development, instead as soon as the application is developed and ready to use it can be deployed in a prototype environment and it can be tested as well.

3). Increased Portability

Through Docker, applications can get instant portability. Here, portability is easier so the application can be packed, shipped, and run instantly by a container. These containers are self-sufficient and can be made run from anywhere. So, in this way, application deployment and development become much easier when compared traditionally.

4). Isolated Code

Developers can isolate their application code through containers. As the code is kept in an isolated manner so whenever required, it can be changed or update easily. Docker’s containers can be deployed on cloud quickly and even larger applications can be broken down into smaller applications and then can be deployed as per requirements.

Comparison of Docker Containers and Virtual Machines

Usually, Docker containers are preferred over virtual machines. They both are compared to the following parameters:

  • Size - Through size, Docker containers and virtual machines are compared as per the number and size of resource that they need for execution.
  • Startup - Startup means a type of booting required. Virtual machines take much time to boot. Containers take less time for booting because they use host OS and share the relevant libraries
  • Integration- It signifies the capability of their integration with other tools in a seamless development environment.

Docker gives a better platform to build apps and microservices. As apps can be deployed automatically through the container, so virtualization is provided in an efficient way. The user need not bear any extra cost for virtual machine implementation and so containers are considered an affordable and cost-efficient along with quickness. Development and testing phase of the application becomes easier and the user can easily execute the commands needed to deploy the application.

In the following situations, Dockers may be usually needed by the developers:

  • When you have to run your code locally on your laptop and need a duplicate running environment on the server side.
  • For CI/CD during development, testing and QA process.
  • To distribute OS of apps with a team and to control the version of an application.

In above-mentioned cases, Docker can be of great help. The user can download the Docker edition and toolbox from the site to use it. For Oracle virtual box, you may need to install an additional pack and will have to then run the setup.

How can we use Dockers?

Virtual machines can create the snapshots that can be revisited if required instantly. Docker containers enhance the virtualization of lightweight processes to become OS independent and to use Linux Kernel’s functionality. Through Docker image, these snapshots are being created, these images are in-turn created by Docker files that are customizable. Driver ‘libcontainer’ is used to create the default docker container. There is a Docker hub that can be used to search the images or to see the snapshots of the applications that is being created.

Most Used Commands of Docker or Docker Cheat Sheet with examples

So far in this blog, we have seen what Docker is and why is it being used. Now, in order to use Docker, you will have to execute certain commands. These commands are called Docker commands that are frequently used for Docker platform.

Here is the list of these most used Docker commands:

Docker Commands Cheat Sheet

docker –version docker kill
docker pull docker commit
docker run docker login
docker ps docker push
docker ps –a docker images
docker exec docker rm
docker stop docker build

Docker Cheat Sheet is a document that provides the information of basic Docker commands and their details with examples. When you will install Docker, you can check the document to get information of each and every command used in Docker. Now let’s see the description of the above-mentioned commands along with some additional commands and examples that are frequently used in Docker:

Docker – version

You can check the currently used Docker version on your system through this command -

$ docker –version

Docker pull

This command can pull the images from docker’s hub or repository that is hub.docker.com

$ docker pull ubuntu

All the images of the hub will be cached and stored from docker’s hub.

Docker run

You can create a container from the image through this command.

$ docker run –it –d ubuntu

Docker ps

To check the running containers or to know that how many containers are running right now, you can use this command:

$ docker ps

Docker ps –a

To view all the running and exited containers, you can use this command:

$ docker ps –a

Docker exec

To access the running container, you can use this command:

$ docker exec it <container id> bash

Docker stop

To stop the running container, we can use this command:

$ docker stop <container id>

Docker kill

The containers get killed after getting stopped by this command. In Docker, stop command container gets the full time to shut down, but when you need to shut down any container immediately then you can kill the Docker container through kill command.

$ docker kill <container id>

Docker commit

To create a new image of the edited container on the local system, you can use this command:

$ docker commit <container id> <username/imagename>

Docker login

To login the docker hub repository, you can use this command:

$ docker login

Docker push

You can push a new image into Docker hub through this command:

$ docker push <username/image name>

Docker images

All locally stored images in docker hub will be listed through this command:

$ docker images

Docker rm

If you want to delete any stopped container then this command can help you:

$ docker rm <container id>

Docker build

If you want to build an image from a Docker file then you can use this command:

$ docker build <path to docker file>

Apart from the above-listed Docker commands cheat sheet, one can also use other commands for Docker like ‘docker export’ command that can export a container’s filesystem as an archive file or ‘docker attach’ that can attach any running container, etc.

Final Words:

There are many more Docker commands and it can be used as a container as well. You can check the cheat sheet file of Docker to know the details of all commands that you can use in order to use the containers effectively. All Docker commands can be executed with lots of options that we have already discussed throughout the blog.

You can use these commands for different objective and don’t forget to share your experience with us how this blog helped you in using Docker commands cheat sheet successfully.

    Janbask Training

    JanBask Training is a leading Global Online Training Provider through Live Sessions. The Live classes provide a blended approach of hands on experience along with theoretical knowledge which is driven by certified professionals.


DevOps Upcoming Batches

Mon - Fri

6 Weeks

Mon - Fri

6 Weeks