| by Arround The Web | No comments

Docker Cheat Sheet

Docker is a well-liked platform for project development and deployment. The docker containerization concept has made it one of the most popular tools for building projects and apps. The major components around which Docker plays are Containers, Docker images, Networks, and Docker registry. With the help of Docker containers, it is simple to install each and every project requirement into a container and fix deployment problems like missing libraries and dependencies, incorrectly specified paths, and many more.

While using Docker, the user must understand Docker elements and its basic commands to manage and use Docker components.

This post will provide a basic command line cheat sheet for Docker.

Docker Cheat Sheet

Docker CLI is one of the most effective ways to use and manage Docker components to build, ship, and develop projects and software. To get started with the Docker platform, we have provided some basic commands to manage the essential components of Docker.

Docker Image

Docker images are the base point of Docker that are used to containerize applications. The Docker image can be created using a simple instruction file, “Dockerfile”. To manage and build 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 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.

 

Docker Containers

Docker containers are another significant part of the Docker environment. These are used to manage projects and their dependencies. Containers encapsulate all project essentials and source code to build, deploy, and share projects. To manage and use 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 This command is used to list 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 stopped container.
Kill Container docker kill <container-name> The “kill” command kills the running containers only.
Kill All Running Containers docker kill $ (docker ps -q) This command will kill or eliminate all running 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 Registry

Docker registry is the cloud storage and system to manage, publish and store Docker images. For this purpose, Docker has provided us with the official registry “Docker Hub”. 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.

Docker Volume

The file system that is linked to the Docker container is referred to as the Docker Volume. It is used to preserve the data produced by the Docker container. To manage the Docker volume, look at 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.
Remove Volume docker volume rm -f <volume-name> This command is used to remove volume forcefully.

Docker Network

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 information on one or more networks.
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.

Docker Clean

While working on the Docker platform for project development, users are occasionally required to clean the Docker system or maybe clean some components of 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 volume 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 removed 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 to build, create, run, and remove the Docker images, containers, volume, and networks. This blog has provided the basic command line cheat sheet for Docker.

Share Button

Source: linuxhint.com

Leave a Reply