What are the essential Docker commands every developer should know?
Fast recall
Quick revision
Core answer
Interview-ready guide
Interview answer
I'd say the essential Docker commands break into three groups.
First, for building and running, you've got docker build to create images from your Dockerfile and docker run to start containers from those images. When I'm developing a Spring Boot app, I use docker build after every code change and docker run with the -d flag to start it in the background. Second, for monitoring what's happening, there's docker ps to list running containers and docker logs to see application output.
I always use docker logs -f to follow logs in real time when I'm testing new features. The -f flag is crucial because otherwise you're constantly rerunning the command. Third, for debugging and cleanup, docker exec lets you open a shell inside a running container — that's super helpful when logs aren't enough and you need to check if a file exists or test network connectivity.
Then docker stop and docker rm handle shutdown and removal. One mistake I see a lot is people forgetting that images and containers are different — you can't docker rm an image, you need docker rmi for that. Also, it's worth knowing docker system prune to clean up disk space, because Docker caches everything and you can easily end up with 20 gigs of old images after a few weeks.
The key is understanding the image-container relationship — images are like JAR files, containers are like running JVM processes.
What are the essential Docker commands every developer should know
Direct Answer
Essential Docker commands include docker build to create images from Dockerfiles, docker run to start containers, docker ps to list running containers, docker stop and docker rm to halt and remove containers, docker images to view local images, docker logs to read container output, and docker pull to download images from registries like Docker Hub.
Deep Dive & Architecture
Docker commands follow the object lifecycle. docker pull obtains an image, docker build creates one, and docker image ls lists local images. docker run is a convenience that creates and starts a container; docker create and docker start expose those stages separately.
Use docker ps for running containers and docker ps -a for all containers. docker logs, docker inspect, docker stats, and docker exec help diagnose a container. docker stop, docker rm, and docker image rm operate on different objects.
The important skill is not memorizing a long list: first decide whether you are working with an image, a container, a network, a volume, or a build cache, then use --help and inspect before any cleanup command.
Code Example
// View all logs from container
docker logs order-api
// Follow logs in real time (essential during development)
docker logs -f order-api
// See only last 50 lines (avoid scrolling through startup messages)
docker logs --tail 50 order-api
// View logs from last 10 minutes
docker logs --since 10m order-api
// Combine flags for targeted debugging
docker logs --since 5m --tail 100 -f order-api
// Filter logs with grep for specific errors
docker logs order-api | grep ERROR
// View logs with timestamps
docker logs -t order-api
// Save logs to file for analysis
docker logs order-api > order-api-logs.txt
Common Mistakes & Pitfalls
- Confusing images and containers: Freshers try docker rm on an image or docker rmi on a running container, getting cryptic errors. Remember images are templates, containers are instances — you can't remove a template while instances are using it.
- Forgetting the -d flag in docker run: Without detached mode, your terminal hangs and you can't run other commands. You end up with multiple terminal windows instead of running containers in the background.
- Using docker ps without -a: You only see running containers, so when your container crashes immediately after starting, docker ps shows nothing and you think Docker is broken. Always use docker ps -a to see stopped containers and their exit codes.
Check your understanding
Answer these without looking back at the article.
- What does docker run -p 8080:8080 mean?
- How do you view logs from a running Docker container?
- How do you clean up Docker resources (stopped containers, unused images)?
Looking for a structured roadmap?
Every module on this page is also part of the full Java backend interview track.