Running Your First Container (docker run, -it, -d)
Launch interactive and detached containers with docker run flags that matter day one.
In plain words
Run a foreground program interactively with -it, or put a long-lived service in the background with -d.
Think of it like…
Starting a foreground meeting with -it keeps you in the room; starting a service with -d is like launching a background appliance and checking its status light later.
Where you’ve seen it
In practice, docker run --rm -it --name shell alpine:3.21 /bin/sh; docker run -d --name web -p 8080:80 nginx:1.27 is the concrete move for this job: launch interactive and detached containers with docker run flags that matter day one. The release is not accepted until docker ps -a --filter name=^/web$ --format {{.Status}} and docker logs web confirms the result.
Words you’ll hear
- docker run
- The implementation control in
docker run --rm -it --name shell alpine:3.21 /bin/sh; docker run -d --name web -p 8080:80 nginx:1.27for launch interactive and detached containers with docker run flags that matter day one. - -it
- The result to observe with
docker ps -a --filter name=^/web$ --format {{.Status}} and docker logs web, rather than infer from a successful command alone. - -d
- The production boundary exposed when using -d for a one-shot command and waiting for a container that exits immediately, or omitting -it and expecting an interactive shell.
Visual diagram
Theory & deep dive
docker run control path
The concrete mechanism is docker run --rm -it --name shell alpine:3.21 /bin/sh; docker run -d --name web -p 8080:80 nginx:1.27. The observable result is docker ps -a --filter name=^/web$ --format {{.Status}} and docker logs web; if those disagree, inspect the runtime evidence before changing unrelated settings.
Launch interactive and detached containers with docker run flags that matter day one. Keep docker run, -it and -d in the same deployment record so the intent and the observed state cannot drift apart.
docker run --rm -it --name shell alpine:3.21 /bin/sh; docker run -d --name web -p 8080:80 nginx:1.27
docker ps -a --filter name=^/web$ --format {{.Status}} and docker logs web, not with command success alone.-d failure boundary
The process after the image name is PID 1, not an optional detail: when it exits, the container stops. Developers often mistake a dead service for a Docker networking problem when they forgot to inspect the one-shot process and its exit code. The narrow failure to design for is using -d for a one-shot command and waiting for a container that exits immediately, or omitting -it and expecting an interactive shell.
Accept this compromise explicitly: Interactive flags improve terminal debugging but are irrelevant for normal services; detached mode needs explicit logs and health checks. The controlled check-docker ps -a --filter name=^/web$ --format {{.Status}} and docker logs web-also identifies the point where a different design is safer.
Exercise this failure safely: using -d for a one-shot command and waiting for a container that exits immediately, or omitting -it and expecting an interactive shell.
How it works, step by step
Step 1 of 41. State the boundary
Write down why the workload needs this: The process after the image name is PID 1, not an optional detail: when it exits, the container stops. Developers often mistake a dead service for a Docker networking problem when they forgot to inspect the one-shot process and its exit code.
- Intent
- docker run
When to use it & trade-offs
What it is
docker run combines image lookup, container creation and start. -i keeps standard input open and -t allocates a pseudo-TTY; -d instead detaches the client while the container keeps running as long as its configured process stays alive.
Why it matters: The process after the image name is PID 1, not an optional detail: when it exits, the container stops. Developers often mistake a dead service for a Docker networking problem when they forgot to inspect the one-shot process and its exit code.
When to reach for it
- The fit is strongest for this job: launch interactive and detached containers with docker run flags that matter day one. Pair it with
docker ps -a --filter name=^/web$ --format {{.Status}} and docker logs webin the runbook. - Production docker run changes should make -it observable through
docker run --rm -it --name shell alpine:3.21 /bin/sh; docker run -d --name web -p 8080:80 nginx:1.27, rather than trusting the runtime default. - Keep a rollback example for the case where using -d for a one-shot command and waiting for a container that exits immediately, or omitting -it and expecting an interactive shell.
When to avoid or be careful
- Do not accept command success while the system can still fail by using -d for a one-shot command and waiting for a container that exits immediately, or omitting -it and expecting an interactive shell.
- Avoid erasing -d protection merely to dodge this cost: “Interactive flags improve terminal debugging but are irrelevant for normal services; detached mode needs explicit logs and health checks”.
- Do not replace the evidence path-
docker ps -a --filter name=^/web$ --format {{.Status}} and docker logs web-with a screenshot or a mutable tag.
docker run --rm -it --name shell alpine:3.21 /bin/sh; docker run -d --name web -p 8080:80 nginx:1.27: practical choices
Strengths: docker run combines image lookup, container creation and start. -i keeps standard input open and -t allocates a pseudo-TTY; -d instead detaches the client while the container keeps running as long as its configured process stays alive.
Watch out: Interactive flags improve terminal debugging but are irrelevant for normal services; detached mode needs explicit logs and health checks.
Strengths: It can begin with docker run --rm -it --name shell alpine:3.21 /bin/sh; docker run -d --name web -p 8080:80 nginx:1.27 and fewer surrounding controls.
Watch out: It silently accepts using -d for a one-shot command and waiting for a container that exits immediately, or omitting -it and expecting an interactive shell, which makes the result hard to reproduce after promotion.
Strengths: The process after the image name is PID 1, not an optional detail: when it exits, the container stops. Developers often mistake a dead service for a Docker networking problem when they forgot to inspect the one-shot process and its exit code.
Watch out: The platform adds an ownership and debugging layer; retain docker ps -a --filter name=^/web$ --format {{.Status}} and docker logs web as the direct check.
Interview signals & numbers to know
When this topic comes up
- docker run --rm -it --name shell alpine:3.21 /bin/sh; docker run -d --name web -p 8080:80 nginx:1.27
- docker ps -a --filter name=^/web$ --format {{.Status}} and docker logs web
- Failure signature: using -d for a one-shot command and waiting for a container that exits immediately, or omitting -it and expecting an interactive shell
- Trade-off: Interactive flags improve terminal debugging but are irrelevant for normal services; detached mode needs explicit logs and health checks.
Benchmarks & rules of thumb
- 1 main process
- is the container lifecycle anchor
- 2 TTY flags
- -i plus -t are usually combined for shells
- 0 container time
- is allocated after a foreground process exits
Common mistakes & follow-ups
Common interview mistakes
- Treating a successful
docker run --rm -it --name shell alpine:3.21 /bin/sh; docker run -d --name web -p 8080:80 nginx:1.27as proof and skippingdocker ps -a --filter name=^/web$ --format {{.Status}} and docker logs web. - Using -d for a one-shot command and waiting for a container that exits immediately, or omitting -it and expecting an interactive shell
- Optimizing docker run without accepting that interactive flags improve terminal debugging but are irrelevant for normal services; detached mode needs explicit logs and health checks.
Likely interviewer follow-ups
- Why does
docker run --rm -it --name shell alpine:3.21 /bin/sh; docker run -d --name web -p 8080:80 nginx:1.27address docker run here rather than through application code? - How would you distinguish healthy -it from the failure “using -d for a one-shot command and waiting for a container that exits immediately, or omitting -it and expecting an interactive shell” using
docker ps -a --filter name=^/web$ --format {{.Status}} and docker logs web? - When would this trade-off-“Interactive flags improve terminal debugging but are irrelevant for normal services; detached mode needs explicit logs and health checks”-justify a different -d design?
docker run --rm -it --name shell alpine:3.21 /bin/sh; docker run -d --name web -p 8080:80 nginx:1.27-and one proof-docker ps -a --filter name=^/web$ --format {{.Status}} and docker logs web. Then name the boundary: using -d for a one-shot command and waiting for a container that exits immediately, or omitting -it and expecting an interactive shell.Knowledge check
Put it into practice
Practice related interview Q&A
Apply Running Your First Container (docker run, -it, -d) with generative AI interview Q&A and hands-on practice.