Updated March 8, 2023
Introduction to Docker Commands Cheat Sheet
The following article provides an outline for Docker Commands Cheat Sheet. With DevOps taking over all the software industry, there is a need for tools that can do all activities in the software development life cycle together. Docker is a tool designed to create, deploy and run different applications by making use of containers. These containers help the developer to create packages for an application with all the parts that are needed in the program. These can be different libraries and other dependencies and ship it out to the package. It acts as a virtual machine. It allows the applications to use the same Linux kernel as the system that they are running and requires only the applications that are to be shipped and things that are not already running on the computer.
Basic Docker Commands Cheat Sheet
Following are some basic docker commands cheat sheets:
1. docker version
This command will give the currently installed version:
Code:
docker version
Output:
2. docker help
This Cheat Sheet Docker command provides the entire list of commands that the client understands. Most of the commands present here are used to deal with the containers.
Code:
docker --help
Output:
3. docker start
This command lets the user start a particular container.
Syntax:
docker start [container]
Code:
docker start iisnanobase
Output:
4. docker ps
This command helps in getting all running containers as well as the containers that are exited from.
Code:
docker ps -a
Output:
5. docker exec -ti [container] [command]
This Docker Commands Cheat Sheet can be used to run any shell command inside any particular container.
Syntax:
docker exec -ti [container] [command]
Code:
docker exec demo powershell.exe ipconfig
Output:
6. docker run -ti –rm –image
This Cheat Sheet Docker command does the work of creating and starting a container at the same time. It can also run the command in it and, in the end, remove the container after executing this command.
Code:
docker run --rm nginx nginx -h
Output:
7. docker inspect
This Cheat Sheet Docker command is useful in getting displayed the low-level information of any particular Docker object.
Code:
docker inspect microsoft/dynamics-nav:devpreview
Output:
8. docker kill
This command is used to kill the container by stopping the execution of the container. Its kill is different from ‘docker stop’ as ‘docker stop’ gives the container time to shut down properly, and in such situations, it takes longer to stop. The kill command, on the other hand, stops the process immediately.
Code:
docker kill fc5e91dfe6f6
Output:
9. docker commit
This command helps in creating a new image of any edited container that is already existing on the container.
10. docker login
If a user wants to login to the Docker repository, then the user must make use of this command.
Code:
docker login docker.viurdata.com
Output:
Intermediate Docker Commands Cheat Sheet
Below commands can be used on an intermediate level by any user.
1. docker push
This command can be used to push an image to the docker hub repository.
The syntax for this command is as below:
docker push <username/image name>
2. docker images
This command lists all images that are stored locally on a computer. It also lists all images, including all intermediate images on the terminal.
3. docker build
By using this command, a docker image is built from a Docker file. The time at the end of the command specifies the context, which is built using the Docker image. Also, if the file does not reside in the same folder where the command is being run, the user needs to add the ‘-f’ flag to specify the folder where the file resides.
4. docker pulls
This command can be used to pull an image from the docker registry to your computer. Also, a specific version of an image can be pulled.
5. docker tag
This helps in tagging the image with any tag that helps the registry that will be used instead of the one that was used locally. This tag can be the same as the local tag.
6 docker rm $(docker ps -a -q)
This command is useful when a user deletes all containers that are currently not in a running state. By hitting this command, all unnecessary containers will be deleted.
7 docker create –name container_name image_name: tag
This Cheat Sheet Docker command is useful when a container from any image is to be instantiated. It is always required that you name the file or image by providing the –name parameter. Once this is done, it is required to run the docker start container_name and docker stop container_name commands. Once the container is created, it can be run for further uses.
8. docker rmi
This command is useful when the user wants to delete an image from the local image store.
9. docker ps
In order to get a list of all containers that are currently running, the user can use this command and get a complete list.
10. docker-machine env default
If the user wants to configure the environment variables, then this command will come to your rescue. ‘default’ here suggests that without any additional complexity, this command should run and set the command line variables. As a developer, you can also list the available machines with the docker-machine ls command and start or stop any particular machines.
Advanced Docker Commands Cheat Sheet
Below are some advanced docker commands cheat sheet:
1. Using multiple Docker Compose Files
These Cheat Sheet Docker Commands use multiple docker files; a developer must change the application with its environments, staging and production. The Docker Compose command helps in taking this forward as it already reads two files by default.
Code:
$ docker-compose up -f my-override-1.yml my-override-2.yml
Developer can use the –f option to docker-compose up in order to use multiple files.
2. Using ONBUILD Commands in Images
This ONBUILD directive specifies different commands which are to be run when a new image is being built from the image that is being used. It can be thought of as a concept of inheritance.
Example:
Code:
ONBUILD COPY package.json /app
Tips and Tricks for Docker Commands Cheat Sheet
Below, tricks on Commands Cheat Sheet can help you save a lot of time.
1. Tailing logs
TO quickly have a look at logs, the below command can be used.
Code:
sudo docker logs -t-tail 1000 my_postgres 2>&1 | grep -I error
2. Copy and paste files
Docker enables you to copy and paste in files in a container. It can be done from host to container and vice versa.
Code:
COPY script.sh /tmp
ADD script.sh /tmp
3. Stop all containers
All containers can be stopped by using the below command.
Code:
docker stop $(docker ps -q)
It will run stop only for active.
Code:
docker stop $(docker ps -aq)
It will run stop for all.
Conclusion
Docker is a savior in the DevOps working environment. With its different facilities, it is one of the best tools which can be used to deploy different projects. It is the best platform to develop, ship, and run different applications together.
Recommended Articles
This has been a guide to Docker Commands Cheat Sheet. Here we have discussed the content and command as well as free tips and tricks of Docker Commands Cheat Sheet. You may also look at the following articles to learn more –