Docker

Docker Cheat Sheet

Docker is an open-source platform that can be used to build, ship, and run applications by packaging software in containers. Docker has a lot of commands and options, and it is very difficult to remember every command. If you are new to Docker, or even if you have some experience in the Docker environment, it can be useful to have a quick reference for the most commonly used Docker commands to manage the Docker environment.

This tutorial explains some of the most commonly used Docker commands.

Docker Terms

This section defines some of the most useful Docker terms.

Docker Image: A set of read-only files. These files are a part of an operating system that is required to run a Docker container.

Dockerfile: A simple text file that contains all the commands a user could call in the command line to assemble or build an image.

Docker Containers: Small and lightweight alternatives to Virtual Machines used to run the application in an isolated environment.

Docker Registry: A centralized place to store all Docker images that allows you to upload and download any images using the command-line.

Docker Volume: A mechanism or technique that stores the data generated by the Docker container.

Docker Network: Used to communicate between docker host and container.

Docker Information Commands

If you are responsible for managing Docker container and image, then you will need to know how to get important information about containers and images to manage them.

To print information about the Docker platform installed on your system, run the following command:

docker info

The command will provide detailed information of Docker, as in the following output:

Client:
 Debug Mode: false

Server:
 Containers: 10
  Running: 2
  Paused: 0
  Stopped: 8
 Images: 8
 Server Version: 18.09.1
 Storage Driver: overlay2
  Backing Filesystem: xfs
  Supports d_type: true
  Native Overlay Diff: true
 Logging Driver: json-file
 Cgroup Driver: cgroupfs
 Plugins:
  Volume: local
  Network: bridge host macvlan null overlay
  Log: awslogs fluentd gcplogs gelf journald json-file local logentries splunk syslog
 Swarm: inactive
 Runtimes: runc
 Default Runtime: runc
 Init Binary: docker-init
 containerd version: c4446665cb9c30056f4998ed953e6d4ff22c7c39
 runc version: 4fc53a81fb7c994640722ac585fa9ca548971871
 init version: fec3683
 Security Options:
  seccomp
   Profile: default
 Kernel Version: 4.18.0-193.14.2.el8_2.x86_64
 Operating System: CentOS Linux 8 (Core)
 OSType: linux
 Architecture: x86_64
 CPUs: 2
 Total Memory: 1.94GiB
 Name: centos8
 ID: GWWI:E5JU:VW33:NKPG:NUSJ:Y5C3:JL55:FSKN:ONCD:GJXY:HTAZ:W7OD
 Docker Root Dir: /var/lib/docker
 Debug Mode: false
 Registry: https://index.docker.io/v1/
 Labels:
 Experimental: false
 Insecure Registries:
  127.0.0.0/8
 Live Restore Enabled: false
 Product License: Community Engine

To list only running containers in your system, run the following command:

docker ps

To list both running and stopped containers in your system, run the following command:

docker ps -a

To print the logs of any running container, run the following command:

docker logs container-id

To print the object information about a container, run the following command:

docker inspect container-id

To print all running processes in a container, run the following command:

docker top container-id

To print the live resource usage of any container, run the following command:

docker stats cntainer-id

To print the changes on files and directories in a filesystem, run the following command:

docker diff container-id

Docker Container Commands

To start a container, run the following command:

docker start container-id

To stop a container, run the following command:

docker stop container-id

To pause a container, run the following command:

docker pause container-id

To restart a container, run the following command:

docker restart container-id

To unpause a paused container, run the following command:

docker unpause container-id

To send a kill signal to a running container, run the following command:

docker kill container-id

To block until a running container stops, run the following command:

docker wait container-id

To connect to a running container, run the following command:

docker attach container-id

To rename an existing container, run the following command:

docker rename container-name new-container-name

To create a container from an image without starting it, run the following command:

docker create image-name

To start a new container from an image and remove the container upon exiting, run the following command:

docker run --rm image-name

To start a new container from an image and keep it running, run the following command:

docker run -td image-name

To start a new container from an image and create an interactive bash shell in the container, run the following command:

docker run -it -rm image-name /bin/bash

To update the configuration of one or more containers, run the following command:

docker update container-name

To remove a container if it is not running, run the following command:

docker container rm container-name

Docker Image Commands

To pull an image from the Docker Hub registry, run the following command:

docker pull image-name

To create an image from the Dockerfile, run the following command:

docker build Dockerfile

To push an image to the Docker Hub registry, run the following command:

docker push dockerhubusername/imagename

To create an image from a container, run the following command:

docker commit container-name new-image-name

To save an image to the tar archive, run the following command:

docker save image-name > tarfile

To remove an image, run the following command:

docker rmi image-name

To list all available images in your system, run the following command:

docker images

To display the history of an image, run the following command:

docker history image-name

Docker Network Commands

This section shows you some network-related commands.

To list all the networks in your system, run the following command:

docker network ls

To print information about one or more networks, run the following command:

docker network inspect network-name

To connect a container to a network, run the following command:

docker network connect network-name container-name

To disconnect a container from a network, run the following command:

docker network disconnect network-name container-name

To remove one or more networks, run the following command:

docker network rm network-name

Docker Volume Commands

This section shows you some volume-related commands in Docker.

To create a new Docker volume, run the following command:

docker volume create volume-name

To list all Docker volumes in your system, run the following command:

docker volume list

To print more information about a Docker volume, run the following command:

docker volume inspect volume-name

To remove a Docker volume from your system, run the following command:

docker volume rm volume-name

Conclusion

In this guide, you learned about the most commonly used Docker-related commands. I hope this article will save you time when managing the Docker environment on your system.

About the author

Hitesh Jethva

Hitesh Jethva

I am Hitesh Jethva lives in Ahmedabad, INDIA. I am Linux system administrator and Technical writer.

I felt in love with Linux while i was started to learn Linux. I am a fan of open source technology and have more than 8+ years of experience in Linux and Open Source technologies. My main motto is to make uncomplicated things easier.

I have extensive experience within the following areas: CentOS/RHEL/FreeBSD/Ubuntu/Debian, cPanel/WHM, High Availability Architecture, Proactive security/Web Application Security, Amazon Web Services (AWS), Apache/Nginx, OpenLDAP, DevOps automation: Chef/Docker.

My expertise includes Linux system administration, installation, configuration, tuning, security and troubleshooting. I have large skills in configuring/management/support of Apache, Nginx, MariaDB, MongoDB, PHP-FPM, Nagios, Zabbix, Graylg, Redis, Docker and much more.