Docker
/Beginner
docker run
Definition
The most common Docker CLI command. It creates a brand new container from a specified image and immediately starts it.
Explain Like I'm New
The magic button to start an app. It tells Docker: 'Take this blueprint, build a box from it, and turn the box on.'
Real World Example
Typing `docker run -p 8080:80 nginx`. Docker instantly spins up a live NGINX web server on your computer, accessible on port 8080.
Common Use Cases
- •Starting applications
- •Testing images
Crucial `docker run` Flags
| Flag | Example | Description |
|---|---|---|
| -d | `docker run -d nginx` | Detached mode. Runs the container safely in the background instead of freezing your terminal. |
| -p | `docker run -p 8080:80 nginx` | Port mapping. Maps Port 8080 on your laptop to Port 80 inside the container. |
| -v | `docker run -v /data:/app db` | Volume mounting. Maps a folder on your laptop's hard drive directly into the container. |
| --name | `docker run --name web nginx` | Assigns a custom, human-readable name to the container instead of a random hash. |
| -e | `docker run -e DB_PASS=123 db` | Environment variables. Passes secret passwords or configs directly into the running container. |
Interview Questions
basic
- What does the `-d` flag do when using `docker run`?
intermediate
- What is the difference between `docker create` and `docker run`?