Learn Docker CLI Commands

Docker Container Management Commands

  1. docker run
    • Purpose: Used to create and start a container from a specified image.
    • Example: Running an Nginx container can be done with docker run -d -p 80:80 nginx. This command runs the container in the background (-d) and binds port 80 of the container to port 80 on the host machine (-p 80:80).
  2. docker start
    • Purpose: Start containers that have been previously stopped.
    • Example: To restart a stopped container named my_container, use docker start my_container.
  3. docker stop
    • Purpose: Gracefully stop a running container.
    • Example: To stop a running container named my_container, use docker stop my_container.
  4. docker restart
    • Purpose: Restart one or more containers.
    • Example: To restart a container named my_container, the command is docker restart my_container.
  5. docker kill
    • Purpose: Forcefully stop a running container by killing its main process.
    • Example: To kill a container immediately, use docker kill my_container.
  6. docker rm
    • Purpose: Remove stopped containers.
    • Example: Use docker rm my_container to delete a container that is no longer running.
  7. docker pause
    • Purpose: Temporarily suspend all processes in a container.
    • Example: docker pause my_container will pause a container.
  8. docker unpause
    • Purpose: Resume a paused container’s processes.
    • Example: Resume the container with docker unpause my_container.
  9. docker ps
    • Purpose: List all currently running containers.
    • Example: docker ps shows active containers, while docker ps -a shows all containers, including those that are stopped.
  10. docker logs
    • Purpose: Fetch the logs from a container.
    • Example: To view the logs of my_container, use docker logs my_container.
  11. docker exec
    • Purpose: Run a command inside a running container.
    • Example: To open a bash shell in my_container, use docker exec -it my_container bash.
  12. docker attach
    • Purpose: Attach your terminal to a running container’s main process.
    • Example: Use docker attach my_container to interact with the container’s standard input, output, and error streams.
  13. docker inspect
    • Purpose: Display detailed information about a container or image.
    • Example: Get detailed information on my_container by running docker inspect my_container.
  14. docker top
    • Purpose: View the active processes running within a container.
    • Example: See the running processes in my_container with docker top my_container.
  15. docker stats
    • Purpose: Monitor real-time resource usage statistics for containers.
    • Example: To get a live stream of resource usage stats for my_container, use docker stats my_container.
  16. docker commit
    • Purpose: Create a new image from a container’s changes.
    • Example: Save the state of a container as a new image with docker commit my_container my_new_image.
  17. docker rename
    • Purpose: Rename an existing container.
    • Example: To rename a container from old_name to new_name, use docker rename old_name new_name.
  18. docker update
    • Purpose: Update configuration options for an existing container.
    • Example: Change the memory limit of my_container with docker update --memory 512m my_container.

Docker Image Management Commands

  1. docker pull
    • Purpose: Download a specific image from a Docker registry.
    • Example: To pull the latest Nginx image from Docker Hub, use docker pull nginx.
  2. docker push
    • Purpose: Upload an image to a Docker registry.
    • Example: Push your locally built image to a repository with docker push my_image:tag.
  3. docker build
    • Purpose: Create an image from a Dockerfile.
    • Example: Build an image with docker build -t my_image:tag ., where . specifies the build context.
  4. docker images
    • Purpose: List all Docker images stored on your machine.
    • Example: Use docker images to see a list of all images, along with their repository names, tags, and sizes.
  5. docker rmi
    • Purpose: Delete one or more images from your local storage.
    • Example: Remove an image by running docker rmi my_image:tag.
  6. docker tag
    • Purpose: Tag an existing image with a new name or repository.
    • Example: Tag an image for a different repository with docker tag my_image:tag my_repo/my_image:tag.
  7. docker save
    • Purpose: Export one or more images to a tar archive.
    • Example: Save an image as a tar file using docker save -o my_image.tar my_image:tag.
  8. docker load
    • Purpose: Load an image from a tar archive.
    • Example: Import an image from a tar file with docker load -i my_image.tar.
  9. docker history
    • Purpose: Show the history of changes made to an image.
    • Example: View the layers of an image with docker history my_image:tag.

Docker Volume Management Commands

  1. docker volume create
    • Purpose: Create a new volume to store data outside of containers.
    • Example: Create a volume named my_volume with docker volume create my_volume.
  2. docker volume ls
    • Purpose: List all Docker volumes on your system.
    • Example: Use docker volume ls to see a list of all volumes.
  3. docker volume inspect
    • Purpose: Display detailed information about a volume.
    • Example: To view details of my_volume, use docker volume inspect my_volume.
  4. docker volume rm
    • Purpose: Delete one or more volumes.
    • Example: Remove a volume by running docker volume rm my_volume.
  5. docker volume prune
    • Purpose: Remove all unused volumes.
    • Example: Clean up all unused volumes with docker volume prune.

Docker Network Management Commands

  1. docker network create
    • Purpose: Create a new network for containers to communicate.
    • Example: Create a network named my_network using docker network create my_network.
  2. docker network ls
    • Purpose: List all Docker networks.
    • Example: Use docker network ls to see all networks available on your system.
  3. docker network inspect
    • Purpose: Display detailed information about a network.
    • Example: Get detailed info on my_network with docker network inspect my_network.
  4. docker network connect
    • Purpose: Connect a container to an existing network.
    • Example: Connect my_container to my_network using docker network connect my_network my_container.
  5. docker network disconnect
    • Purpose: Disconnect a container from a network.
    • Example: Disconnect my_container from my_network with docker network disconnect my_network my_container.
  6. docker network rm
    • Purpose: Delete one or more networks.
    • Example: Remove a network with docker network rm my_network.

Docker System and Cleanup Commands

  1. docker system df
    • Purpose: Display disk usage statistics for Docker.
    • Example: Use docker system df to see how much disk space is being used by images, containers, volumes, and more.
  2. docker system prune
    • Purpose: Remove all unused data, including stopped containers, unused images, and networks.
    • Example: Clean up your system with docker system prune.
  3. docker info
    • Purpose: Display detailed information about the Docker installation.
    • Example: View system-wide Docker information with docker info.
  4. docker version
    • Purpose: Show Docker version details.
    • Example: Use docker version to see the version of the Docker client and server.
  5. docker events
    • Purpose: Get a real-time stream of Docker events.
    • Example: Track Docker activity live with docker events.
  6. docker login
    • Purpose: Log in to a Docker registry.
    • Example: Authenticate with a Docker registry using docker login.
  7. docker logout
    • Purpose: Log out from a Docker registry.
    • Example: To log out from the current registry, use docker logout.

Docker Compose Commands

  1. docker-compose up
    • Purpose: Start and run containers as defined in a docker-compose.yml file.
    • Example: Start services in the background with docker-compose up -d.
  2. docker-compose down
    • Purpose: Stop and remove containers, networks, images, and volumes defined in a docker-compose.yml file.
    • Example: Stop and remove all services with docker-compose down.
  3. docker-compose build
    • Purpose: Build or rebuild services as defined in the docker-compose.yml file.
    • Example: Build the services with docker-compose build.
  4. docker-compose ps
    • Purpose: List the status of all services defined in a docker-compose.yml file.
    • Example: View the status of services with docker-compose ps.
  5. docker-compose logs
    • Purpose: View logs output by services.
    • Example: Check logs for services with docker-compose logs.