Skip to main content

Command Palette

Search for a command to run...

Essential Docker Commands with Examples

Published
β€’3 min readβ€’View as Markdown
Essential Docker Commands with Examples

Docker Image Commands

πŸ”Ή docker pull – Download an Image

Pulls an image from Docker Hub.

docker pull nginx

This downloads the official nginx image locally.

docker images – List Images

docker images

Shows all images available on your system.


πŸ”Ή docker rmi – Remove an Image

docker rmi nginx

Deletes the image if no container is using it.

Docker Container Commands

πŸ”Ή docker run – Create & Start a Container

docker run nginx

This command:

  1. Pulls the image (if not present)

  2. Creates a container

  3. Starts it


πŸ”Ή Run Container in Detached Mode

docker run -d nginx

Runs the container in the background.


πŸ”Ή Run Container with Port Mapping

docker run -d -p 8080:80 nginx

Access nginx in browser:

http://localhost:8080

πŸ“‹ Container Management Commands

πŸ”Ή docker ps – List Running Containers

docker ps

πŸ”Ή List All Containers (Including Stopped)

docker ps -a

πŸ”Ή docker stop – Stop a Container

docker stop <container_id>

πŸ”Ή docker start – Start a Stopped Container

docker start <container_id>

πŸ”Ή docker restart – Restart a Container

docker restart <container_id>

πŸ”Ή docker rm – Remove a Container

docker rm <container_id>

To force remove:

docker rm -f <container_id>

πŸ” Inspecting Containers

πŸ”Ή docker logs – View Container Logs

docker logs <container_id>

Very useful for debugging application issues.


πŸ”Ή docker exec – Access Running Container

docker exec -it <container_id> /bin/bash

Now you’re inside the container.

Exit:

exit

πŸ”Ή docker inspect – Detailed Container Info

docker inspect <container_id>

Shows:

  • IP address

  • Volumes

  • Network details

  • Configuration


🧹 Cleanup Commands (Very Important)

πŸ”Ή Remove All Stopped Containers

docker container prune

πŸ”Ή Remove Unused Images

docker image prune

πŸ”Ή Remove Everything (Danger ⚠️)

docker system prune -a

Deletes:

  • All stopped containers

  • Unused images

  • Networks

  • Cache

πŸ‘‰ Use carefully.


🧠 Understanding Container Lifecycle

A Docker container goes through these stages:

Created β†’ Running β†’ Stopped β†’ Deleted

Commands mapping:

  • Create & start β†’ docker run

  • Stop β†’ docker stop

  • Start again β†’ docker start

  • Delete β†’ docker rm

Understanding this lifecycle helps in troubleshooting.


🌍 Real DevOps Scenario

Run a Temporary Debug Container

docker run -it ubuntu /bin/bash

Use cases:

  • Debug networking

  • Test commands

  • Check OS behavior

Exit β†’ container stops automatically.

Must-Know Commands Summary

CommandPurpose
docker pullDownload image
docker runCreate & start container
docker psList containers
docker imagesList images
docker logsView logs
docker execAccess container
docker stop/startControl containers
docker rm/rmiCleanup

Conclusion

Docker commands are the foundation of container management.
Once you’re comfortable with these commands, Docker stops feeling complex and starts feeling powerful.