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 Name | Syntax | Description |
---|---|---|
Docker Daemon | docker -d | Starts the Docker Daemon |
Help | docker --help | “–help” will open up the Docker manual to get help |
Docker Information | docker info | This command will display the Docker host-wide information |
Docker Live Stream | docker stats | This command shows the live stream of executing containers and shows the information about memory and CPU usage |
Disk Space Usage | docker system df | This command shows the disk space usage of Docker containers. |
Docker Version | docker version | This 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 Name | Syntax | Description |
---|---|---|
Build Image | docker build -t <img-name> -f Dockerfile . | The “build” command creates or generates the Image from Dockerfile. |
Run Image | docker run -d <img-name> | Creates a new container and runs a command or Docker image within a container. |
Run Image on Port | docker 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 Image | docker images -a | List all Docker images. |
Tag Image | docker tag <img-name> <new-img-name>:<tag> | Tag the Docker image to uniquely identify the image version. |
Remove Image | docker rmi -f <img-name> | This command is used to remove images forcefully. |
Image History | docker history <img-name> | This command shows the detailed history of the Docker image. |
View Supported Options for Image Build | docker build --help | This command shows the supported options for the Docker build. |
Inspect Image | docker 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 Name | Syntax | Description |
---|---|---|
Create Container | docker create --name <container-name> -p 5000:5000 <docker-img> | This command is used to create the Docker container. |
List Container | docker ps -a | It is used to display all containers. |
Start Container | docker start <container-name> | This command starts the container. However, users can also use container ID with the “start” command. |
Stop Containers | docker 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 Container | docker restart <container-name> | This command will restart the docker-stopped container. |
Kill Container | docker kill <container-name> | It will kill or terminate the running containers only. |
Kill All Running Containers | docker kill $ (docker ps -q) | This will kill or eliminate all executing containers. |
Attach Container | docker attach <container-name> | Connect a running container’s local input, output, and error streams. |
Exposed Port | docker port <container-name> | Show the mapping of ports within the container. |
Docker container exec | docker exec -it <container-name> | The command will open the container shell in which the user can run commands. |
Commit Docker Container | docker 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 Container | docker inspect <container-name> | This command is used to inspect the Docker container. This will show the network information, host information, and many more. |
Container Logs | docker logs <container-name> | The command will fetch and display the container’s logs. |
Copy File to Container | docker 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 Container | docker 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 Name | Syntax | Description |
---|---|---|
Login | docker login | This command is used to log in to Docker Hub. Users can also use the “-u” option to provide the user name in the command. |
Logout | docker logout | This command logs out the user from the Docker registry. |
Search Image | docker 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 Image | docker 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 Name | Syntax | Description |
---|---|---|
Create Volume | docker volume create <volume-name> | This command creates the new volume. |
List Volume | docker volume ls | List all Docker volumes. |
Mount Volume | docker run [OPTION] --mount source=<volume-name>, target=<container-path> nginx:latest | This command is used to mount the volume with the Docker container |
Remove Volume | docker 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 Name | Syntax | Description |
---|---|---|
Create Network | docker network create <option> <network-name> | This command creates a new network. |
List Network | docker network ls | List all available networks. |
Inspect Network | docker network inspect <network> | This command shows detailed network information. |
Connect Network | docker network connect network container | This command is utilized to connect the network with the container |
Remove Network | docker 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 Name | Syntax | Description |
---|---|---|
Create Service | docker service create [OPTIONS] <image-name> <command> | This command is used to introduce a new Docker service. |
Inspect Service | docker service inspect <service-name> | This command is utilized to inspect the service in detail. |
Service Logs | docker service logs | This command displays the logs of the running service. |
List Service | docker service ls | List down the Docker services |
Service Process State | docker service ps | This will show the process or task of docker services. |
Scale Service | docker service scale <service-name>=<replicas-no> | Scale the service or replicate the Docker service |
Remove Service | docker service rm <service-name> | This will remove the Docker service. |
Update Service | docker service update <service-name> | This command is utilized to update the Docker service. |
Rollback the Service | docker 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 Name | Syntax | Description |
---|---|---|
Docker Prune Volume | docker volume prune | This command prunes or removes all unused volumes in Docker. |
Docker Prune Image | docker image prune -a | This command removes all dangling or unused Docker images. |
Docker Prune Container | docker container prune -a | The provided command removes all dangling, unused, and stopped containers. |
Docker Prune System | docker system prune | The 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 Containers | docker rm $(docker ps -aq) | This command will remove all stopped Docker containers. |
Remove All Images | docker 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.