Docker Commands Cheat Sheet
Are you finding difficulty remembering all the commands that you need in order to work with Docker?
Guys, don’t worry! This Docker cheat sheet will give you a quick reference to the basics of Docker that you must know to get started with it.
At Intellipaat, we support our learners with a handy reference, that’s the reason we have created this cheat sheet. It is designed for those who have already started learning Docker but need a handy reference to recall the concepts that they have learned.
Download a Printable PDF of This Cheat Sheet
Introduction
The Docker tool was introduced to make it easier for developers to create, deploy, and run applications using containers. Containers provision the developers with the packaging of their applications, together with all important components they require such as libraries and other dependencies, and shipping of them all out as a single package. This assures the developers that their applications can be run on any other machine.
We will start this tutorial by understanding the Docker architecture and its main aspects in it, and then we will continue with the important commands required for the Docker operations, such as Docker installation, build, push, run, ship, clean up, and the interaction with the container.
Docker Architecture
DevOps architecture consists of five main entities, namely, registry, image, container, daemon, and client.
- Registry: It hosts public and official images. The Docker registry that we use is Docker Hub.
- Image: It can be downloaded from the registry directly or implicitly when starting a container.
- Container: It is the instance of an image. Multiple containers can exist for a single image.
- Docker daemon: A daemon creates, runs, and monitors containers, along with building and storing images.
- Client: A client talks to Docker daemon via HTTP.
Orchestrate
Orchestration is used for managing a container’s life cycle, especially in dynamic environments. It is used for controlling and automating many tasks for containers.
There are many Docker orchestration tools such as Docker Swarm, Kubernetes, etc. In this tutorial, we’re elucidating Docker Swarm commands. Let’s check out the commands used for Docker orchestration.
Commands
- To initialize the swarm mode and listen to a specific interface:
Docker swarm init --advertise-addr 10.1.0.2
- To join an existing swarm as a manager node:
Docker swarm join --token<manager-token> 10.1.0.2:2377
- To join a swarm as a worker node:
Docker swarm join --token<worker-token> 10.1.0.2:2377
- To list all nodes in the swarm:
Docker node ls
Get 100% Hike!
Master Most in Demand Skills Now!
- To create a service from an image on the existing port and deploy three instances:
Docker service create --replicas 3 -p 80:80 name -webngix
- To list services running in a swarm:
Docker service ls
Docker service scale web=5
- To list the tasks of a service:
Docker service ps web
Build
Build is used for creating images from Docker files. We will now discuss some essential Docker build commands
Commands
- To build an image from the Docker file and tag it:
Docker build -t myapp :1.0
- To list all the images that are locally stored:
Docker images
- To delete an image from the Docker Store:
Docker rmi alpine: 3.4
Run
The run command is used for creating a container from a specified image. Check out the below-listed run commands.
Commands
- To create and run a command:
Docker run --name container_name docker_image
Flags used:
- -d: To detach a container on start
- -rm: To remove a container once it stops
- -p: To publish the host IP and the host port to the container port
- -v: To define and share the volume across containers
- –read-only: To set to the read-only permission
Ship
Docker gives us the capability of shipping our application container anywhere, on any platform. Let’s check out some commands used for it.
Commands
- To pull an image from the registry:
Docker pull alpine:3.4
- To retag a local image with a new image name:
Docker tag alpine:3.4 myrepo/ myalpine:3.4
- To log in to the registry:
Docker login my.registry.com:8000
- To push an image to the registry:
Docker push myrepo/ myalpine:3.4
Clean up
To prevent wasting resources, we must know how to clean up. In this Docker cheat sheet tutorial, next, a few essential clean-up commands are provided.
Commands
- To clean an unused/dangling image:
Docker image prune
- To remove an image that is not used in a container:
Docker image prune -a
- To prune the entire system:
Docker system prune
Docker swarm leave
Docker stack rm stack_name
- To kill all running containers:
Docker kill $ (docker ps -q)
- To delete all stopped containers:
docker rm $(docker ps -a -q)
docker rmi $(docker images -q)
Services
Now, let’s take a sneak peek at some of the commands used to view running services, run them, view service logs, and scale them up/down accordingly.
Commands
To list all services running in a swarm:
Docker service ls
To see all running services:
Docker stack services stack_name
To see all service logs:
Docker service logs stack_name service_names
To scale a service across qualified nodes:
Docker service scale stack_name_service_name= replicas
Interaction with a Container
Let’s check out how to make an interaction with a container in Docker.
Commands
To run a command in a container:
Docker exe -ti container_name command.sh
To follow container logs:
Docker logs -ft container name
To save a running container as an image:
Docker commit -m “commit message” -a “author” container_name username/image_name: tag
Important Terms
Some of the important terms to know about while using Docker container are listed below:
- Layer: Read-only files to provision the system
- Image: Read-only layer that is the base of an image
- Container: A runnable instance of the image
- Registry/hub: A central place where images reside
- Docker machine: A VM to run the Docker container
- Docker Compose: A VM to run multiple containers as a system
Watch this Docker vs Kubernetes video tutorial:
Download a Printable Docker Cheat Sheet PDF
We have covered all the basics of Docker in this Docker commands cheat sheet. If you want to start learning DevOps in-depth, then check out the DevOps certification training offered by Intellipaat.
Not only will you get to learn and implement Docker with step-by-step guidance and support from us but you will also get to learn other important topics in DevOps such as Chef, Git/GitHub, Ansible, Jenkins, Puppet, Selenium, Nagios, and many more.
Here, at Intellipaat, you will also get 24/7 technical support from us that will help you with any of your queries in the respective technologies throughout the certification period. So, why wait? Check out the training program and enroll today!
We hope this tutorial helps you gain knowledge of DevOps course online. If you are looking to learn DevOps Training in a systematic manner with expert guidance and support then you can enroll to our DevOps course.