Running Containers
docker run flags, ports, start/stop/remove
docker run — The One Command That Does It All
docker run is the command you will use the most. It does three things in one go: finds the image, creates a container from it, and starts it.
docker run nginx:1.25
Run that and… nothing visible happens. The terminal just hangs. That is correct — nginx is a web server that keeps running until you stop it. Press Ctrl + C to stop it.
Now let us add flags one at a time to make this actually useful.
-d: Run in the Background
The -d flag stands for detached. It starts the container in the background so your terminal is free to use.
docker run -d nginx:1.25
Docker prints the container ID and gives you your prompt back:
3f4a8b12cd90e1a2...
-p: Open a Port
A container is isolated — you cannot reach it from your browser yet. The -p flag creates a tunnel: your machine port → container port.
docker run -d -p 8080:80 nginx:1.25
Read -p 8080:80 as: “when I visit port 8080 on my machine, forward the traffic to port 80 inside the container.”
| Side | Port | Meaning |
|---|---|---|
| Left of : | 8080 | Your machine (host) — you choose this |
| Right of : | 80 | Inside the container — nginx listens here |
Now open your browser and go to http://localhost:8080. You will see the nginx welcome page.
—name: Give It a Friendly Name
By default Docker gives your container a random name like sleepy_newton. Use --name to give it something meaningful:
docker run -d -p 8080:80 --name my-webserver nginx:1.25
Now you can refer to the container by name in every subsequent command instead of copying a long ID.
See What Is Running
List all running containers:
docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
3f4a8b12cd90 nginx:1.25 ... 2 minutes ago Up 2 minutes 0.0.0.0:8080->80/tcp my-webserver
To also see containers that have stopped:
docker ps -a
The -a flag means all (running + stopped).
Stop, Start, and Remove
Three simple commands control the lifecycle of a container:
# Stop a running container (graceful shutdown)
docker stop my-webserver
# Start it again
docker start my-webserver
# Remove it completely (must be stopped first)
docker rm my-webserver
If you want to stop and remove in one go, use the -f (force) flag with rm:
docker rm -f my-webserver
-it: Go Inside a Container
Sometimes you want to open a shell inside a running container to look around. The -it flag does that:
docker run -it ubuntu:22.04 bash
This drops you into a bash shell inside an Ubuntu container. You can run commands, look at files, check what is installed — then type exit to leave.
Summary: The Commands You Now Know
| Command | What it does |
|---|---|
docker run -d -p 8080:80 --name web nginx:1.25 | Run nginx in background, expose on port 8080, name it “web” |
docker ps | List running containers |
docker ps -a | List all containers (running + stopped) |
docker stop web | Stop a container |
docker start web | Start a stopped container |
docker rm web | Delete a container |
docker run -it ubuntu bash | Open a shell inside a container |
Next up: stop relying on other people’s images and build your own — with a Dockerfile.