Docker Commands Cheat Sheet – Introduction
With DevOps taking over all the software industry there is a need for tools which can do all activities in 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 ships it out to the package. Docker acts like 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 which are not already running on the computer. Let us have a look at some of the Docker commands cheat sheet below.
Basic Docker Commands Cheat Sheet
1. docker version
This Docker Commands will give the currently installed version of the docker.
2. docker help
This Cheat Sheet Docker command provides the entire list of commands that the Docker client understands. Most of the commands present here are used to deal with the containers.
3. docker start
This command lets the user to start a particular container.
The syntax is: docker start [container]
4. docker ps -a
This command helps in getting all running containers as well as the containers that are exited from.
5. docker exec -ti [container] [command]
This Docker Commands Cheat Sheet can be used to run any shell command inside any particular container.
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.
7. docker inspect
This Cheat Sheet Docker command is useful in getting displayed the low-level information of any particular Docker object.
8. docker kill
This command is used to kill the container by stopping the execution of the container. Docker 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.
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.
Intermediate Docker Commands Cheat Sheet
Below commands can be used on an intermediate level by any user.
11. 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>
12. 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.
13. 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.
14. 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.
15. 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.
16 docker rm $(docker ps -a -q)
This command is useful when a user deletes all containers that are currently not in running state. By hitting this command all unnecessary containers will be deleted.
17 docker create –name container_name image_name: tag
This Cheat Sheet Docker Commands 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.
18. docker rmi
This command is useful when the user wants to delete an image from the local image store.
19. docker ps
In order to get a list of all containers that are currently running user can use this command and get a complete list.
20. 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 docker-machine ls command and start or stop any particular machines.
Advanced Docker Commands Cheat Sheet
21. 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. The command can be:
- $ docker-compose up -f my-override-1.yml my-override-2.yml
- Developer can use –f option to docker compose up in order to use multiple files.
22. 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 a concept of inheritance.
Eg. ONBUILD COPY package.json /app
Tips and Tricks for Docker Commands Cheat Sheet
Below tricks on Docker Commands Cheat Sheet can help you save a lot of time
1. Tailing logs
TO quickly have a look at logs below command can be used
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.
COPY script.sh /tmp
ADD script.sh /tmp
3. Stop all containers
All containers can be stopped by using below command
docker stop $(docker ps -q) – will run stop only for active
docker stop $(docker ps -aq) – will run stop for all
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. Docker is the best platform to develop, ship and run different applications together.
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 article to learn more –