Cheat Sheet to Docker- Important Docker Commands for Software Developers

0
6612
Docker commands

Considering, the widespread use of Docker, we have created this article that includes widely-used Docker commands for making your work more productive & efficient.


Undoubtedly, Docker has become hotter than hot! When S. Hykes launched it in 2013, it didn’t take long for Docker to garner all the attention from all over the world. 

All the noise was happening because of all the benefits Docker provides. In a nutshell, Docker makes it easy for anyone to create containerized applications in no time; it allows you to run more apps than anything else on the same old servers; and it also helps in managing or deploying any apps without any hassle. In this article, we have covered some of the most important and widely used Docker commands.

Though there are several Docker commands you must know, here, we have covered some of the most essential commands for Docker. We have outlined 11 Docker commands that will help you in saving your time. Moreover, these commands will greatly help you to have a clear picture of the Docker working without any difficulties.

Let’s first understand the fundamentals of Docker…

Various cases in which you can use Docker

You can use Docker for-

  • Distributing the OS of your app with a team as a version control system.
  • In development phases like “Development”, “Testing” & “QA”.
  • While running your code locally to replicate the server’s environment. 

Locally setting up Docker?

  • First, you have to download Docker and its toolbox.
  • After downloading, now you have to check that the AMD-V, Virtualization Techs, or KVM is enabled in your BIOS.
  • Lastly, simply running the setup after installing the extension pack in the Oracle VirtualBox will do the job.

How to use a Docker?

Just like the snapshots of virtual machines, Docker has images. These are nothing but a file having several layers. It is used to execute code in Docker and can be used as it is or customized. To create a Docker container, you can use the default execution driver i.e. libcontainer. Moreover, you can use Docker Hub for searching for all the Docker images.

  • $ docker search <image>” – You can use it for searching any image in the Docker Hub.
  • $ docker images” – You can use it for checking the total images on your system.
  • $ docker run hello world” – You can use it for downloading a “hello world” image for creating a Docker container.

Read More: Learn How To Use Docker Images?

In case, if you don’t know what Docker is or looking to learn Docker then you can begin with “Docker for Dummies – The Complete Absolute Beginners Guide”.

11 Docker commands that you can use right away!

Since its inception, Docker has grown tremendously offering several services as per various virtualization needs. Because of this, you can find a myriad of commands for Docker CLI.

1. To create a container with images from Docker Hub

$ docker create -it ubuntu:xenial bash

Docker Hub is basically an official repository for Docker images. It includes many pre-built images that were created by enterprises and developers. You can use the above command that will help you to pull the image for Ubuntu Xenial from Docker Hub. It will create a container using this image.

2. Listing containers in Docker

After creating the container, now you have to check whether it was a success or not. The below command will inform the Docker to list out all the current containers.

$ docker ps

There will be a high chance that the “ps” command won’t list any running containers. This might be due to the fact that you have not initiated your Docker container. Don’t worry – you can use the “-a” flag that will inform Docker to list all the containers.

$ docker ps -a

It will help you to see the container that you have created with the 1st command.

3. Initiating your container

You have to start your container so that you can use it in Docker. The below command will help you start your container in seconds.

$ docker start CONTAINER_ID

In this command, “CONTAINER ID” will be the id of your container. You can get this ID while running the “ps -a” command.

Read More: Learn How To Create And Start Docker Containers?

4. Connecting your container

Now you have started your container but you still need to connect with your container to use it. To do so, you have to use the Docker “attach” command.

$ docker attach CONTAINER_ID

It will lead to a change in your prompt and now you are inside a Ubuntu Xenial-based container. Here, whatever you do will remain inside the container.

5. Terminating your container

Just like starting a container was simple, terminating a container is equally simple. Without any issues, you can simply terminate an active container with Docker “stop” command. When it comes to the newbies, it is one of the most useful commands because it allows you to exit easily from any running container.

$ docker stop CONTAINER_ID

However, the catch here is that you have to run this command from a different terminal as the original terminal is running the container and does not contain Docker. Still, if it is difficult to figure out then you can just exit the container by pressing “Ctrl+D” or just typing “exit” inside the container.

6. Deleting your container

One of the major reasons for a significant increase in the popularity of the container is its ability that helps you in deleting any container without affecting the host machine. You can use the below command for this action.

$ docker rm CONTAINER_ID

This command will instantly delete your container, but with the container, all your configurations and files will be deleted too. So, if you need them in the future, it is better to move them before you delete the container.

7. Killing your container

After reading the title, I am sure the 1st thing that came in your mind was “How stopping or terminating a container is different from killing a container”, right? Well, in Docker, there is a very subtle difference between the two. When you terminate the container, it will 1st stop the process within the container and then it will stop the container. 

Whereas, with the “kill” command, you kill the entire process of the container running in the host system. It might lead to corruption in certain container processes or can cause unwanted misconfigurations. So, while using the below command, be very cautious about killing a container.

$ docker kill CONTAINER_ID

Read More: Learn How To Stop, Kill And Clean Up Docker Containers

8. Containers detachment

This command will be useful for you in a certain situation wherein you are already attached to any container but because of some reason, you want to leave the container running and want to use terminal sessions. Because the docker commands for stopping or killing a container will terminate the container, you won’t be able to use them.

Ctrl+P Ctrl+Q

Instead, you can just hold the combination of the above keys when you are inside a running container. It will help you in detaching from that container. For instance, you can just hold down the Ctrl button and continuously press P & Q one after another.

9. How to copy contents from containers to the file system and vice versa?

The need may arise to copy the configuration files from the container to the system or other containers. You can use the “cp” command for executing this action and it copies content recursively.

$ docker cp CONTAINER_ID:/test_file test_file

The above command helps in copying the test_file from the container with the CONTAINER_ID to the host system. And you can use the below command for copying the file from the host to a container.

$ docker cp test_file CONTAINER_ID:/test_file

10. Listing all the images of Docker

As stated earlier, Docker images are very essential for developers and are also the building block of any container. Do not forget that the base of all the containers is created using a Docker image. Because of this, often developers keep multiple images of Docker across their systems. With the following command, you can check all the images in your system in no-time.

$ docker images

Executing this Docker command will help you check all the top Docker images with its repository, tags, and size. If you want to just fetch the IMAGE_ID then you can simply use -q option.

11. Removing Docker images

A time will arise when you might not require any particular image/s. In such cases, you can remove one or more Docker images with the below command for Docker.

$ docker rmi <IMAGE_ID>

In case, if any particular image is tagged with multiple repositories then you have to delete it with the following Docker command.

$ docker rmi REPOSITORY:TAG

And again, you can find the required information when you run the command Docker images.

Wrapping Up!

No doubt Docker has become an integral part of modern software development. It is packed with a myriad of Docker commands that you can check in its documentation. However, in this article, we have tried to cover some of the most basics and important Docker commands that you can use for your upcoming project without any difficulties.

I hope that all these Docker commands will be useful for your project. Also, soon we will be releasing some of the more advanced commands of Docker, so stay tuned and keep reading! And meanwhile, if you want to learn more about Docker then we have also listed some of the top books on Docker that you can read right away.

Previous articleEducating the Educators – A How To Guide!
Next articleWhat No Book Will Tell You About Biometrics? An In-Depth Guide To Biometrics!

LEAVE A REPLY

Please enter your comment!
Please enter your name here