A Complete Guide to Docker Commands with Simple Explanations

Author

Kritim Yantra

Apr 12, 2025

A Complete Guide to Docker Commands with Simple Explanations

Docker is a powerful tool for creating, deploying, and managing containers. Whether you're a beginner or an experienced developer, knowing the essential Docker commands is crucial. In this blog, we'll list all the important Docker commands with simple explanations.


1. Docker Container Commands

Run a Container

docker run [OPTIONS] IMAGE [COMMAND] [ARG...]
  • Explanation: Creates and starts a container from an image.
  • Common Options:
    • -d → Run in detached mode (background).
    • -p 80:80 → Map host port 80 to container port 80.
    • --name mycontainer → Assign a name to the container.
    • -v /host/path:/container/path → Mount a volume.
    • -e VAR=value → Set environment variables.

Example:

docker run -d -p 8080:80 --name mynginx nginx

List Running Containers

docker ps
  • Explanation: Shows all running containers.
  • Options:
    • -a → Show all containers (including stopped ones).
    • -q → Only display container IDs.

Example:

docker ps -a

Stop a Container

docker stop CONTAINER_ID/NAME
  • Explanation: Stops a running container gracefully.

Example:

docker stop mynginx

Start a Stopped Container

docker start CONTAINER_ID/NAME
  • Explanation: Starts a stopped container.

Example:

docker start mynginx

Remove a Container

docker rm CONTAINER_ID/NAME
  • Explanation: Deletes a stopped container.
  • Options:
    • -f → Force remove (even if running).

Example:

docker rm mynginx

View Container Logs

docker logs CONTAINER_ID/NAME
  • Explanation: Shows logs from a container.
  • Options:
    • -f → Follow logs in real-time.

Example:

docker logs -f mynginx

Execute a Command Inside a Running Container

docker exec [OPTIONS] CONTAINER COMMAND [ARG...]
  • Explanation: Runs a command inside a running container.
  • Common Options:
    • -it → Interactive mode (for shell access).

Example:

docker exec -it mynginx bash

2. Docker Image Commands

List All Images

docker images
  • Explanation: Lists all locally stored Docker images.

Example:

docker images

Pull an Image from Docker Hub

docker pull IMAGE_NAME:TAG
  • Explanation: Downloads an image from a registry (default: Docker Hub).

Example:

docker pull ubuntu:latest

Remove an Image

docker rmi IMAGE_ID/NAME
  • Explanation: Deletes a Docker image.

Example:

docker rmi nginx

Build an Image from a Dockerfile

docker build -t IMAGE_NAME:TAG PATH_TO_DOCKERFILE
  • Explanation: Creates an image from a Dockerfile.

Example:

docker build -t myapp:1.0 .

3. Docker Network Commands

List Networks

docker network ls
  • Explanation: Shows all Docker networks.

Example:

docker network ls

Create a Network

docker network create NETWORK_NAME
  • Explanation: Creates a custom Docker network.

Example:

docker network create mynetwork

Connect a Container to a Network

docker network connect NETWORK_NAME CONTAINER_NAME
  • Explanation: Attaches a container to a network.

Example:

docker network connect mynetwork mynginx

4. Docker Volume Commands

List Volumes

docker volume ls
  • Explanation: Lists all Docker volumes.

Example:

docker volume ls

Create a Volume

docker volume create VOLUME_NAME
  • Explanation: Creates a named volume for persistent storage.

Example:

docker volume create mydata

Remove a Volume

docker volume rm VOLUME_NAME
  • Explanation: Deletes a volume.

Example:

docker volume rm mydata

5. Docker System Commands

View Docker Disk Usage

docker system df
  • Explanation: Shows disk usage by Docker.

Example:

docker system df

Remove Unused Objects

docker system prune
  • Explanation: Cleans up unused containers, networks, and images.
  • Options:
    • -a → Remove all unused images (not just dangling ones).
    • --volumes → Also prune volumes.

Example:

docker system prune -a --volumes

Conclusion

These are the most essential Docker commands you need to manage containers, images, networks, and volumes. By mastering these, you'll be able to efficiently work with Docker in development and production environments.

Happy Dockering! 🐳

Tags

Docker

Comments

No comments yet. Be the first to comment!

Please log in to post a comment:

Continue with Google

Related Posts