Docker has completely changed how programmers build, distribute, and run applications. It provides a lightweight and portable containerization platform that allows applications to run consistently across different environments.To use the full power of Docker, it is important to understand the most commonly used Docker commands and their respective uses. In this blog post, we will provide a comprehensive overview of these commands in a convenient table format.
Docker Commands and Their Uses
|Downloads a Docker image from a registry.|
|Builds a Docker image from a Dockerfile located at the specified PATH.|
|Creates and runs a new Docker container from the specified IMAGE.|
|Lists running containers.|
|Lists downloaded images.|
|Stops a running container.|
|Removes a stopped container.|
|Removes a Docker image.|
|Kill running container|
|Remove unused images|
|Runs a command inside a running container.|
|Creates and starts containers defined in a Compose file.|
|Stops and removes containers, networks, and volumes defined in a Compose file.|
Let’s examine each command and its typical use case in more detail:
docker pull IMAGE_NAME: Using this command, you can download a Docker image from a private repository or a registry like Docker Hub.. For example,
docker pull ubuntuwill download the latest Ubuntu image.
docker build PATH: Use this command to build a Docker image from a Dockerfile located at the specified PATH. The Dockerfile contains instructions to define the image’s contents. For instance,
docker build -t myapp:latest .builds an image named
myappfrom the current directory.
docker run [OPTIONS] IMAGE: This is one of the most frequently used commands. It creates and runs a new Docker container from the specified IMAGE. You can use options like
-dto run the container in the background,
-pto map ports, and
-vto mount volumes.
docker ps [OPTIONS]: To see the list of running containers, use this command. Options like
-ashow all containers, including stopped ones.
docker images [OPTIONS]: This command lists all the Docker images downloaded on your system. Options like
-adisplay intermediate images as well.
docker stop CONTAINER_ID: To stop a running container, use this command with the correct container ID. The container will gracefully shut down.
docker rm CONTAINER_ID: After stopping a container, you can remove it with this command. Specify the container ID, and it will be permanently deleted.
docker rmi IMAGE_ID: If you want to remove a downloaded Docker image, use this command. It requires the image ID as the parameter.
docker exec [OPTIONS] CONTAINER COMMAND: With this command, you can run a specific command inside a running container. For example,
docker exec -it my_container bashwill open an interactive shell in
docker-compose up [OPTIONS]: Docker Compose allows you to define multi-container applications using a YAML file. This command creates and starts the containers defined in the Compose file.
docker-compose down: When you’re done with a Docker Compose setup, this command stops and removes the defined containers, networks, and volumes.
Just a few of the frequently used Docker commands are included here; there are many more to choose from to suit different use cases. Always refer to the official Docker documentation for an in-depth understanding of each command and its options.