What is the difference between docker run, docker start, and docker exec?
Learn how docker run, docker start, and docker exec differ — creating, resuming, and entering containers — with clear commands, examples, and interview tips.
Expected Interview Answer
docker run creates a brand-new container from an image and starts it, docker start restarts an existing stopped container, and docker exec runs an extra command inside a container that is already running.
docker run is the only one of the three that instantiates a fresh container — it pulls the image if needed, allocates a filesystem layer, and launches the container's main process. docker start takes a container that already exists (identified by name or ID) and boots it again, preserving its previous configuration and writable layer. docker exec attaches a new process to a container whose main process is still alive, which is how you open a shell or run diagnostics without disturbing the primary workload.
- run is for creating and launching from an image
- start is for resuming an existing stopped container without recreating it
- exec is for inspecting or acting inside a live container
- Knowing the difference avoids accidentally spawning duplicate containers
- exec keeps the main process untouched during debugging
AI Mentor Explanation
docker run is picking a fresh player from the squad and sending them out to open the innings — a new batter now at the crease. docker start is the same batter who was earlier retired-hurt walking back out to resume their knock. docker exec is the physio jogging onto the field mid-over to check on a player who is still batting, without ending the innings or replacing anyone.
Step-by-Step Explanation
Step 1
Create and launch with run
docker run alpine echo hi pulls the image if missing, creates a new container, and starts its main process.
Step 2
Stop the container
docker stop <id> gracefully halts the main process, leaving the container in an exited state, not deleted.
Step 3
Resume with start
docker start <id> reboots that same exited container using its original config and writable layer.
Step 4
Enter with exec
docker exec -it <id> sh spawns an interactive shell as an additional process inside the running container.
Step 5
Observe the distinction
run adds a new container to docker ps -a each time; start and exec reuse an existing one.
What Interviewer Expects
- Clear grasp that run creates, start resumes, exec enters
- Understanding that only run instantiates from an image
- Awareness that exec needs an already-running container
- Knowledge that start preserves the container's writable layer
- Ability to give the correct command flags (-it) for an interactive shell
Common Mistakes
- Thinking docker run and docker start are interchangeable
- Using docker exec on a stopped container (it fails)
- Running docker run repeatedly and unknowingly piling up dead containers
- Believing exec restarts or replaces the main process
- Forgetting -it flags when an interactive shell is needed
Best Answer (HR Friendly)
“docker run makes a new container from an image and starts it, docker start switches a stopped container back on, and docker exec lets you run a command inside a container that is already running. Think of run as building and launching, start as resuming, and exec as stepping inside a live one.”
Code Example
# run: create a NEW container from an image and start it
docker run --name web -d nginx
# stop it (container still exists, just exited)
docker stop web
# start: boot the SAME existing container again
docker start web
# exec: run an extra command inside the RUNNING container
docker exec -it web sh
# proof: run creates a new one each time; start/exec reuse
docker run -d nginx # another container appears
docker ps -aFollow-up Questions
- What happens if you run docker exec against a stopped container?
- How does docker run --rm change the container lifecycle?
- What is the difference between docker start and docker restart?
- How can you attach to a container's main process instead of exec-ing a new one?
- Why might repeated docker run commands exhaust disk space?
MCQ Practice
1. Which command creates a brand-new container from an image?
docker run is the only one that instantiates a new container from an image and then starts it.
2. What does docker exec require to succeed?
docker exec attaches a new process to a live container, so the target must currently be running.
3. What does docker start do to an exited container?
docker start resumes an existing stopped container, preserving its writable layer and original settings.
Flash Cards
docker run — Creates a new container from an image and starts it.
docker start — Resumes an existing stopped container without recreating it.
docker exec — Runs an extra command/process inside an already-running container.
Can exec target a stopped container? — No — the container must be running or exec fails.
Which command piles up dead containers if repeated? — docker run — each invocation creates a new container.