You are currently viewing A Comprehensive Guide to Docker Commands and Their Uses
Docker Commands and Their Uses

A Comprehensive Guide to Docker Commands and Their Uses

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

CommandDescription
docker pull IMAGE_NAMEDownloads a Docker image from a registry.
docker build PATHBuilds a Docker image from a Dockerfile located at the specified PATH.
docker run [OPTIONS] IMAGECreates and runs a new Docker container from the specified IMAGE.
docker ps [OPTIONS]Lists running containers.
docker images [OPTIONS]Lists downloaded images.
docker stop CONTAINER_IDStops a running container.
docker rm CONTAINER_IDRemoves a stopped container.
docker rmi IMAGE_IDRemoves a Docker image.
docker killKill running container
docker pruneRemove unused images
docker exec [OPTIONS] CONTAINER COMMANDRuns a command inside a running container.
docker-compose up [OPTIONS]Creates and starts containers defined in a Compose file.
docker-compose downStops and removes containers, networks, and volumes defined in a Compose file.

Let’s examine each command and its typical use case in more detail:

  1. 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 ubuntu will download the latest Ubuntu image.
  2. 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 myapp from the current directory.
  3. 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 -d to run the container in the background, -p to map ports, and -v to mount volumes.
  4. docker ps [OPTIONS]: To see the list of running containers, use this command. Options like -a show all containers, including stopped ones.
  5. docker images [OPTIONS]: This command lists all the Docker images downloaded on your system. Options like -a display intermediate images as well.
  6. docker stop CONTAINER_ID: To stop a running container, use this command with the correct container ID. The container will gracefully shut down.
  7. 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.
  8. docker rmi IMAGE_ID: If you want to remove a downloaded Docker image, use this command. It requires the image ID as the parameter.
  9. 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 bash will open an interactive shell in my_container.
  10. 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.
  11. 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.

Leave a Reply