Interview's MateDocker
Interview's Mate

Docker

Compose · Networking · Security · Ops

Visualizer

DSA Visualizer
All 32 lessons
Custom Playground
Try your own examples

Learning

System Design
31 architecture topics
SQL Learning
160 foundations → production topics
AI Engineering
160 LLM → backend topics
Data Science
160 Python → MLOps topics
Data Engineer
160 SQL → streaming topics
Docker
160 Docker learning topics
Kubernetes
160 K8s fundamentals → cloud
Java LearnSoon
Coming soon
Python Learn
Interview Q&A & internals
JavaScript Learn
Closures, async & DOM Q&A
Tech Learn
Search, practise & track Q&A
AI DSA Practice
Solve coding problems
Theme
Home
Docker topics
Lesson 8/ 160
01. Containerization & Docker Fundamentals

Running Your First Container (docker run, -it, -d)

Launch interactive and detached containers with docker run flags that matter day one.

Beginnerdocker run · -it3 quiz questions
Practice DevOps Q&A
In plain wordsDiagramTheoryStep by stepWhen to use itInterview cluesMistakes to avoidQuiz (3)Practice

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.27 for 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

Loading interactive 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.

Concrete docker run setup

docker run --rm -it --name shell alpine:3.21 /bin/sh; docker run -d --name web -p 8080:80 nginx:1.27

Key takeaway: Prove -it with 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.

Deliberate failure test

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.

Key takeaway: Interactive flags improve terminal debugging but are irrelevant for normal services; detached mode needs explicit logs and health checks.

How it works, step by step

Step 1 of 4
Design

1. 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.

Run a foreground program interactively with -it, or put a long-lived service in the background with -d.
Key numbers at this step
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 web in 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

Explicit docker run
Best for: Launch interactive and detached containers with docker run flags that matter day one, with a reviewed runbook.

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.

Ambient default
Best for: Short-lived -it experiments with low blast radius.

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.

Managed -d
Best for: Teams that centralize -d policy across many workloads.

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.27 as proof and skipping docker 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.27 address 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?
Interview tip: Give one concrete operation-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

Loading quiz

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.

Open DevOps Q&A →Tech Learn Q&A
Previous topic
Pulling, Inspecting & Removing Images
Next topic
Container Lifecycle Management (start, stop, restart, pause, rm)