Running Your First Container
After understanding Docker architecture, images, and containers, it is finally time to run a real container.
This is where many beginners execute their first Docker command:
docker run nginx
But instead of blindly memorizing commands, the goal here is to understand:
- what the command does
- what happens internally
- how containers behave
- why Docker feels different from traditional software
The docker run Command
The most common Docker command is:
docker run nginx
This command looks simple, but internally Docker performs multiple operations.
Simplified workflow:
Find Image
↓
Create Container
↓
Configure Networking
↓
Start Process
If the image does not exist locally, Docker automatically downloads it first.
What Happens Internally
When running:
docker run nginx
Docker typically performs these steps:
1. Check local images
2. Pull image if missing
3. Create writable container layer
4. Configure networking
5. Start container process
6. Attach terminal output
This entire process often happens in seconds.
Your First Download
The first time you run an image, Docker downloads it from a registry.
For example:
Docker Hub
↓
Downloads nginx image
↓
Stores image locally
This process is called:
pulling an image
After the image is downloaded once, future containers start much faster because the image already exists locally.
Understanding Container Processes
One extremely important Docker concept is:
A container runs a process.
For example:
docker run nginx
starts the Nginx process inside the container.
If the main process stops:
the container stops
This behavior is very different from virtual machines.
Containers are process-focused, not machine-focused.
Running a Container in Detached Mode
By default, Docker attaches the terminal to the container output.
This can feel strange initially.
A very common option is:
docker run -d nginx
The -d flag means:
detached mode
This allows the container to run in the background.
Listing Running Containers
To see running containers:
docker ps
Example output:
CONTAINER ID IMAGE STATUS
a1b2c3d4 nginx Up 10 seconds
This shows:
- container ID
- image used
- current state
Containers that are currently active appear here.
Listing All Containers
Stopped containers are hidden by default.
To see all containers:
docker ps -a
This helps visualize container lifecycle behavior.
For example:
Running
Stopped
Exited
Deleted
Naming Containers
Docker automatically generates random container names.
Example:
focused_turing
sleepy_panda
You can assign custom names using:
docker run --name web nginx
Now the container is easier to manage.
Stopping a Container
To stop a running container:
docker stop web
Docker sends a termination signal to the main process.
Simplified flow:
Running Container
↓
Stop Signal
↓
Container Stops
The container still exists after stopping.
It is simply inactive.
Starting a Stopped Container
To start the container again:
docker start web
This reuses the existing container.
Docker does not create a new one here.
This distinction is important.
Removing a Container
To delete a container:
docker rm web
Simplified lifecycle:
Create
↓
Run
↓
Stop
↓
Remove
Once removed, the container no longer exists.
However:
the image still remains locally
This is another important distinction between images and containers.
Interactive Containers
Some containers allow interactive terminal sessions.
Example:
docker run -it ubuntu bash
Flags:
-i = interactive
-t = terminal
This launches a shell inside the container.
You are now interacting directly with the container environment.
Exiting a Container
Inside interactive containers:
exit
stops the shell process.
Because the shell was the main process:
the container also stops
This reinforces one of Docker’s most important ideas:
Containers exist to run processes.
Viewing Container Logs
To see container output:
docker logs web
Logs are extremely important for:
- debugging
- monitoring
- troubleshooting
Container logs often replace traditional server log management approaches.
Inspecting Containers
Docker also provides detailed inspection information.
Example:
docker inspect web
This reveals:
- networking
- volumes
- environment variables
- container configuration
- runtime metadata
This command becomes very useful later.
Why Docker Feels Different
Traditional software installation often looks like this:
Install Software
↓
Modify Server
↓
Permanent Changes
Docker works differently:
Start Container
↓
Use Container
↓
Replace Container
Containers are designed to be replaceable.
This changes how modern infrastructure is managed.
Common Beginner Mistake
One common beginner mistake is assuming:
Stopping a container deletes it
It does not.
Stopping only changes the container state.
The container still exists until explicitly removed.
Infrastructure Thinking
Modern infrastructure increasingly treats containers as temporary compute units.
For example:
Application Update
↓
Replace Old Containers
↓
Start New Containers
Instead of manually modifying running systems, infrastructure becomes reproducible and automated.
This approach became foundational for cloud-native systems.
Why This Matters
Understanding basic container lifecycle behavior is critical before learning:
- networking
- volumes
- Docker Compose
- Dockerfiles
- Kubernetes
Almost every Docker workflow depends on creating, running, stopping, and replacing containers.
Without this understanding, Docker can feel unpredictable.
Key Takeaways
docker runcreates and starts containers- Containers run processes
- If the main process stops, the container stops
- Images and containers are different things
- Containers can run in attached or detached mode
- Containers can be started, stopped, and removed
- Logs and inspection tools help debug containers
- Modern infrastructure treats containers as replaceable units