1.02 Basic Docker Commands
Docker commands are used to run containers, list containers, stop containers, remove containers, manage images, download images, and execute commands inside running containers.
Goal
Learn the most common Docker CLI commands used in daily work and understand what happens when a container starts, stops, exits, or runs in the background.
Command Summary
| Task | Command |
|---|---|
| Run a container | docker run <image> |
| List running containers | docker ps |
| List all containers | docker ps -a |
| Stop a container | docker stop <container> |
| Remove a stopped container | docker rm <container> |
| List images | docker images |
| Remove image | docker rmi <image> |
| Pull image only | docker pull <image> |
| Execute command inside container | docker exec <container> <command> |
| Run container in background | docker run -d <image> |
| Attach to running container | docker attach <container> |
Tip
In Docker commands, you can use either the container name or the first few unique characters of the container ID.
docker run β Start a Container
The docker run command starts a container from an image.
If the image is not available locally, Docker pulls it from Docker Hub first.
Unable to find image 'nginx:latest' locally
latest: Pulling from library/nginx
Status: Downloaded newer image for nginx:latest
After the image is downloaded, Docker starts a container from it.
Note
Docker pulls the image only the first time. Later runs reuse the local image unless a newer image is explicitly pulled.
docker ps β List Running Containers
Use docker ps to list only currently running containers.
Typical output shows:
| Column | Meaning |
|---|---|
| CONTAINER ID | Unique container ID |
| IMAGE | Image used to create the container |
| COMMAND | Main command running inside the container |
| CREATED | When the container was created |
| STATUS | Current state |
| PORTS | Published ports |
| NAMES | Auto-generated or custom container name |
Example
Docker automatically gives each container a random name, such as silly_sammet, unless you provide one using --name.
docker ps -a β List All Containers
Use docker ps -a to list both running and stopped containers.
This is useful when a container exits immediately and does not appear in docker ps.
Tip
If you ran a container but cannot see it with docker ps, check docker ps -a.
docker stop β Stop a Container
Use docker stop to stop a running container.
You can also use the container ID.
After stopping, the container moves to an exited state.
Note
docker stop stops the container, but it does not delete it.
docker rm β Remove a Container
Use docker rm to permanently remove a stopped container.
Then verify:
Warning
You cannot normally remove a running container unless you stop it first or force remove it.
docker images β List Images
Use docker images to list images stored locally on the Docker host.
Example output:
| Repository | Tag | Size |
|---|---|---|
| nginx | latest | 109MB |
| redis | latest | 107MB |
| ubuntu | latest | 112MB |
| alpine | latest | 4.14MB |
Note
Images remain on the host even after containers are stopped or removed.
docker rmi β Remove Images
Use docker rmi to remove an image.
Warning
Before removing an image, delete all containers created from that image.
Correct cleanup order:
Production caution
Do not remove images from production nodes blindly. Existing running containers may continue running, but future restarts or scaling may fail if the image is no longer available locally and the registry is unreachable.
docker pull β Download an Image Only
Use docker pull to download an image without starting a container.
This is useful when you want the image ready before running containers.
Tip
In production, pre-pulling images can reduce startup delay during deployments.
Why Some Containers Exit Immediately
If you run Ubuntu like this:
The container may exit immediately.
This happens because containers live only as long as their main process is running. Ubuntu is just a base OS image. It does not run a long-running service by default.
Note
A container is not meant to behave like a full virtual machine. It is meant to run a specific process.
To keep it alive temporarily, run a command:
This container runs for 5 seconds and then exits.
Another example:
Append a Command to docker run
You can override or append the command that runs inside the container.
Meaning:
Example
This is useful for testing, debugging, or running short-lived jobs.
docker exec β Execute a Command in a Running Container
Use docker exec to run a command inside an already running container.
Example:
Note
docker exec works only when the container is running.
For interactive shell access:
or, if Bash exists:
Tip
Minimal images like Alpine may not have Bash. Use sh instead.
Attached Mode vs Detached Mode
By default, docker run runs in attached mode.
You will see the application logs directly in the terminal.
In attached mode:
- terminal stays connected to the container output
Ctrl + Cusually stops the container- useful for testing and learning
docker run -d β Detached Mode
Use -d to run the container in the background.
Docker returns the container ID and gives the terminal back immediately.
Production habit
Most long-running services are started in detached mode or managed by orchestration tools like Kubernetes.
docker attach β Attach Back to a Container
Use docker attach to connect back to a running container.
Example:
Warning
Be careful with Ctrl + C after attaching. It may stop the container depending on how the container process handles signals.
Useful Command Patterns
Production Best Practices
Do
- Use meaningful container names for local testing with
--name. - Use pinned image tags instead of relying on
latest. - Run long-running containers in detached mode.
- Check logs before deleting failed containers.
- Remove unused stopped containers and dangling images regularly.
- Use
docker pullbefore planned deployments if startup time matters. - Use
docker execfor quick debugging, not permanent fixes. - Treat containers as disposable and recreate them from images.
Don't
- Do not make manual changes inside a running container and expect them to persist.
- Do not use containers as full virtual machines.
- Do not remove images before checking dependent containers.
- Do not store important data only inside a container filesystem.
- Do not use random container names in scripts.
- Do not run production containers without logs and restart strategy.
- Do not depend on
latesttag in production.
Common Troubleshooting
Container exits immediately
Check what command the image runs by default.
Cannot remove image
A container still depends on it.
Quick Cleanup Commands
Remove one stopped container:
Remove one image:
Remove all stopped containers:
Remove unused images:
Warning
Prune commands delete resources. Use them carefully, especially on shared or production systems.
Quick Revision
docker run nginx
Starts an nginx container. Pulls image if missing.
docker ps
Shows running containers.
docker ps -a
Shows running and stopped containers.
docker stop <container>
Stops a running container.
docker rm <container>
Removes a stopped container.
docker images
Lists local images.
docker rmi <image>
Removes an image.
docker pull <image>
Downloads image without running it.
docker exec <container> <command>
Runs a command inside a running container.
docker run -d <image>
Runs container in background.
docker attach <container>
Attaches terminal to a running container.
Quote
A container lives only as long as the main process inside it is running.
Exam / Interview Points
Explain docker run
docker run creates and starts a container from an image. If the image is not available locally, Docker pulls it from a registry first.
Explain docker ps vs docker ps -a
docker ps shows only running containers. docker ps -a shows all containers, including stopped or exited containers.
Explain image vs container
An image is a template or package. A container is a running instance of that image.
Explain why Ubuntu container exits
The Ubuntu image does not run a long-running process by default. Since the main process exits immediately, the container also exits.