Docker Cheat Sheet

Photo of author

By admin

Do you also get stuck and search for different places to find commands in order to perform the desired action on Docker? If yes, then search no more. Here, you will find all the important commands, useful one-liners, cleanup commands, machine commands, compose syntax, and instructions to help you interact with containers via Docker. This is the Docker cheat sheet.

This article will help you to eliminate all – or at least, many of – the doubts and will load up your arsenal with all the essential commands you need to champion Docker. Before heading to the Docker cheat sheet, let’s first understand Docker in brief.

What is Docker?

Docker is an open-source container manager that is used to develop, ship, and run applications using containers. It enables developers to pack up an application with other essential parts, such as libraries and other dependencies, and deploy it as a single isolated unit.

Unlike Virtual Machines, Docker doesn’t create a full virtual operating system. Instead, it allows applications to use the same Linux kernel as the system that they’re running on. This grants higher speed and a boost in performance along with reducing the size of the applications.

Useful Docker Glossary

Following are some of the most-commonly-used terms that you may find everywhere when dealing with Docker. Let’s understand their actual meaning:

Image

A Docker image is the read-only layer that is the base of a Docker container. Moreover, you can have a parent image to detach the more basic filesystem snapshot.

Layer

A layer can be explained as a “read-only” snapshot of the filesystem. It is a set of read-only files meant to provide to the system.

Docker Compose

Docker Compose allows multiple containers to run as a single service. It enables them to interact with each other and ensure that they are functioning properly.

This results in applications in one container to run properly with your database in a different container, and your analytics application in a different container, and so on. Moreover, it allows them to run in complete isolation, making them independent.

Registry / Hub

This is the centralized place onto which all the publicly published images go live. Here, you can search and upload Docker images and can also pull one.

Docker Machine

It is the VM within which the Docker containers run. If you’re using Linux, you can run containers on Docker Machine natively. But on macOS and Windows, a layer of abstraction is required. A docker-machine will spin a very lightweight virtual machine that integrates with the docker command-line utilities really well.

Docker Cheat Sheet

This list of Docker commands mentioned below includes the most useful commands you will use in your day-to-day dealing with Docker containers.

(Source: Docker)

Installation

Operating System Command
Windows https://download.docker.com/win/stable/InstallDocker.msi
Linux curl -sSL https://get.docker.com/ | sh
macOS https://download.docker.com/mac/stable/Docker.dmg

Docker Management

To get more information regarding a particular command, you can run the “Run docker <command> –help” on the terminal.

Commands Functions
builder For managing builds.
cluster For managing Docker clusters.
config For managing configures.
context For managing contexts.
engine For managing the Docker engine.
image For managing images.
network For managing networks.
node For managing the Swarm nodes
plugin For controlling plugins.
registry For managing the registries.
secret For opening and editing Docker secrets.
service For managing services.
stack Used for managing Docker stacks.
swarm For accessing and customizing swarms.
system For managing Docker.

Build

Commands Functions
docker build -t myimage:1.0 . This command is used to build an image from the Dockerfile in the same directory and tag the image.
docker image ls With the help of this command, you will be able to list all images that are locally stored with the Docker Engine.
docker image rm alpine:3.4 If you want to delete an image from the local image store, this command will help you.

Share

Commands Functions
docker pull myimage:1.0 Run this command to pull an image from the registry.
docker tag myimage:1.0 myrepo/myimage:2.0 You can tag an existing image with a new image name and tag with the help of this command.
docker push myrepo/myimage:2.0 Allows you to push an image to a registry.

Run

Commands Functions
docker container run –name web -p 5000:80 alpine:3.9 Through this piece of code, you’ll be able to run a container from the Alpine version 3.9 image, the name would be “web” and port 5000 will be exposed externally.
docker container stop web or,

docker container kill web

Allows you to stop a running container.
docker network ls All your network will be listed using this command.
docker container ls All your running containers will be listed. You can add (–all) to include stopped containers as well into the list.
docker container rm -f $(docker ps -aq) Running this command will delete all your containers. Including both, running and stopped containers.
docker container logs –tail 100 web Through this command, the last 100 lines of your container log will be listed.

More Docker Commands

Commands Actions
docker run -ti –name container_name image_name /command Use this command to run a shell command inside a newly created container.
docker start container_name Start a container using this command.
docker run –rm -ti image_name /command You can execute this command inside a container from the image and later remove the container after the command is successfully done.
docker exec -ti container_name “cmd” Used to run a shell command in the container.
docker logs -ft container_name Allows you to log the output of the container.
docker rmi $(docker images -q -f dangling=true) Deletes dangling or faulty images.
docker rm $(docker ps -a -q) Removes only the stopped containers.

Conclusion

That sums up the Docker cheat sheet. Docker is a robust platform for running containers efficiently. But to achieve the desired goals, one needs to have a comprehensive knowledge of Docker commands. This blog provides users with a centralized place for knowing the commands related to Docker.

Also, this list of commonly used Docker commands will help you to become more familiar with the popular containerization platform. This Docker cheat sheet will be helpful for you to create and manage containers, images, and networks.

Any queries, suggestions, errors, or anything else? Comment them down below.

 

Leave a Comment