To use Docker from Docker CLI, the user must understand Docker architecture and its basic commands to manage and use Docker components.

This post will provide the Docker commands cheat sheet to manage the Docker working environment.

The Docker command line utility has more control over Docker core blocks. Therefore, Docker CLI is one of the effective ways to use and manage Docker components to build, ship, and develop projects and software in a containerized environment.

So, let’s get started!

General Commands

The basic commands that are used to manage or start the Docker environment are listed below in tabular form:

Command NameSyntaxDescription
Docker Daemondocker -dStarts the Docker Daemon 
Helpdocker --help“–help” will open up the Docker manual to get help
Docker Information docker infoThis command will display the Docker host-wide information
Docker Live Streamdocker statsThis command shows the live stream of executing containers and shows the information about memory and CPU usage
Disk Space Usagedocker system dfThis command shows the disk space usage of Docker containers.
Docker Versiondocker versionThis command will show the installed version of the Docker engine.

Manage Images

Images are the key element of Docker that are utilized to containerize applications. The Docker image is the basic template of the container. Images in Docker are created using a simple instruction file, “Dockerfile”. To manage the Docker image, we have listed basic commands:

Command NameSyntaxDescription
Build Imagedocker build -t <img-name> -f Dockerfile .The “build” command creates or generates the Image from Dockerfile.
Run Imagedocker run -d <img-name>Creates a new container and runs a command or Docker image within a container.
Run Image on Portdocker run -d -p 8080:8080 <img-name>To run a container or image as a container on a specific port the “-p” option is used along with the “run” command.
List Imagedocker images -aList all Docker images.
Tag Imagedocker tag <img-name> <new-img-name>:<tag>Tag the Docker image to uniquely identify the image version.
Remove Imagedocker rmi -f <img-name>This command is used to remove images forcefully.
Image Historydocker history <img-name>This command shows the detailed history of the Docker image.
View Supported Options for Image Builddocker build --helpThis command shows the supported options for the Docker build.
Inspect Imagedocker inspect image <image-name>This will inspect the image in detail

Manage Containers

Docker containers are another major part of the Docker environment. These are used to manage projects and their dependencies. Containers encapsulate all project essentials and source code to build and deploy the projects. To utilize the containers in Docker, go through the provided table:

Command NameSyntaxDescription
Create Containerdocker create --name <container-name> -p 5000:5000 <docker-img>This command is used to create the Docker container.
List Containerdocker ps -aIt is used to display all containers.
Start Containerdocker start <container-name>This command starts the container. However, users can also use container ID with the “start” command.
Stop Containersdocker stop <container-name>The provided command will stop the executing container.
Remove Container docker rm <container-name>To remove the container, the “docker rm” command is used.
Restart Containerdocker restart <container-name>This command will restart the docker-stopped container.
Kill Containerdocker kill <container-name>It will kill or terminate the running containers only.
Kill All Running Containersdocker kill $ (docker ps -q)This will kill or eliminate all executing containers.
Attach Containerdocker attach <container-name>Connect a running container’s local input, output, and error streams.
Exposed Portdocker port <container-name>Show the mapping of ports within the container.
Docker container execdocker exec -it <container-name>The command will open the container shell in which the user can run commands.
Commit Docker Containerdocker commit <image-name>:<tag>This will save the container changes in the form of an image. Actually, this command is used to generate images from the container.
Inspect the Containerdocker inspect <container-name>This command is used to inspect the Docker container. This will show the network information, host information, and many more.
Container Logsdocker logs <container-name>The command will fetch and display the container’s logs.
Copy File to Containerdocker cp <local path> <container-name>:<container-path>This command will copy a file or folder from the host system to the Docker container path.
Copy File From Containerdocker cp <container-name>:<container-path> <local-directory-path>This command is used to copy files/ directories from the container to the host system

Manage Registry

Docker registry is the cloud storage system for managing, publishing, and storing Docker images. For this purpose, Docker has provided cloud storage (registry) “Docker Hub”. However, users can also create and utilize the private registry. To play with the Docker registry from the command line, check out the provided commands:

Command NameSyntaxDescription
Login docker loginThis command is used to log in to Docker Hub. Users can also use the “-u” option to provide the user name in the command.
Logoutdocker logoutThis command logs out the user from the Docker registry.
Search Imagedocker search <img-name>This command is used to search images from the Docker registry.
Push Image docker push <img-name>This command is used to push the Docker image from the local registry to the remote registry, either in the private or official Docker registry.
Pull Imagedocker pull <img-name>This command is utilized for pulling or downloading images from the Docker registry.

Manage Volume

The file system that is linked to the Docker container is referred to as the Docker Volume. It is utilized to preserve the data produced by the Docker container. Docker volume is actually an external file system that is mounted to a Docker container. It is used for backup purposes. If the container is deleted, the data stored in the container will be preserved and users can reuse it. To manage the Docker volume, look at the Docker volume basic commands:

Command NameSyntaxDescription
Create Volumedocker volume create <volume-name>This command creates the new volume.
List Volumedocker volume lsList all Docker volumes.
Mount Volumedocker run [OPTION] --mount source=<volume-name>, target=<container-path> nginx:latestThis command is used to mount the volume with the Docker container
Remove Volumedocker volume rm -f <volume-name>This command is used to remove volume forcefully.

Manage Network

The Docker network provides the network on which users can execute services or projects. Docker networking enables us to use as many networks as we like. To get started with the Docker network, look at the provided table:

Command NameSyntaxDescription
Create Networkdocker network create <option> <network-name>This command creates a new network. 
List Network docker network lsList all available networks.
Inspect Network docker network inspect <network>This command shows detailed network information.
Connect Networkdocker network connect network containerThis command is utilized to connect the network with the container
Remove Networkdocker network rm <network>This command removes the network.

Manage Services

Service is an instance or task that is running in the cluster (docker swarm) on the system. While creating the service in Docker, the user can specify the image of any container to use and also define the commands that will be run inside the container. To manage the Docker services, look at the following table:

Command NameSyntaxDescription
Create Servicedocker service create [OPTIONS] <image-name> <command>This command is used to introduce a new Docker service.
Inspect Servicedocker service inspect <service-name>This command is utilized to inspect the service in detail.
Service Logsdocker service logsThis command displays the logs of the running service.
List Servicedocker service lsList down the Docker services
Service Process Statedocker service psThis will show the process or task of docker services.
Scale Servicedocker service scale <service-name>=<replicas-no>Scale the service or replicate the Docker service
Remove Servicedocker service rm <service-name>This will remove the Docker service.
Update Servicedocker service update <service-name>This command is utilized to update the Docker service.
Rollback the Servicedocker service rollback <option> <service-name>This is utilized to revert the changes or configurations in the service.

Clean Docker Environment

While working on the Docker platform for project development, users are occasionally required to clean the Docker system or maybe clean some components of the Docker. For this purpose, go through the mentioned commands in the table:

Command NameSyntaxDescription
Docker Prune Volumedocker volume pruneThis command prunes or removes all unused volumes in Docker.
Docker Prune Imagedocker image prune -aThis command removes all dangling or unused Docker images.
Docker Prune Containerdocker container prune -aThe provided command removes all dangling, unused, and stopped containers.
Docker Prune Systemdocker system pruneThe specified command completely cleans the Docker by removing all unused, dangling Docker images, networks, and containers. To remove volume along with other components, the “–volume” option will be used.
Remove All Containersdocker rm $(docker ps -aq)This command will remove all stopped Docker containers.
Remove All Imagesdocker rmi -f $(docker images -aq)The provided command will remove all Docker images forcefully.

Here we go! We have provided the basic command line cheat sheet for the Docker platform.

Conclusion

Docker CLI is one of the most effective ways to use and manage Docker components to build, ship, and develop projects and software. To play with Docker, the user must have basic knowledge of Docker commands. These basic commands include how to build, run, remove, and manage the Docker images, containers, volume, services, and networks. This blog has provided the basic command line cheat sheet for Docker.