Most people learn Docker with one command:
docker run ubuntu
And then immediately hit confusion:
Why did the container exit instantly?
That single moment actually opens the door to understanding how Docker really works.
This guide takes you beyond basic execution and into the practical flags every DevOps engineer should know for real container management.
Why a Container Exits Immediately
When you run:
docker run ubuntu
Docker pulls the image, starts a container, and exits almost immediately.
Why?
Because Docker containers live only as long as the main foreground process runs.
Ubuntu itself does not start a continuous service by default, so the container stops right away.
You can verify:
docker ps -a
You’ll see the container in exited state.
Use Exact Image Versions Instead of latest
A very common production mistake is relying on implicit latest tags.
Example:
docker run redis
This means:
docker run redis:latest
That may silently change behavior later when upstream images update.
Always pin versions:
docker run redis:4.0
docker run ubuntu:17.10
Why this matters:
predictable builds
stable deployments
easier rollback
fewer production surprises
Run One-Off Commands Inside Containers
Docker is not only for long-running services.
You can execute quick commands directly:
docker run ubuntu cat /etc/os-release
Docker flow:
Start container
Execute command
Print output
Exit container
Perfect for quick inspection.
Detached Mode (-d) for Background Execution
To keep a container running in background:
docker run -d nginx
Now your terminal is free.
Check running containers:
docker ps
Stop later:
docker stop <container_id>
Attach again if needed:
docker attach <container_id>
Detached mode is essential for:
web servers
APIs
background jobs
CI agents
Interactive Mode (-it) Explained Properly
A lot of beginners use -it without understanding why.
Correct usage:
docker run -it ubuntu bash
What happens:
-i keeps STDIN open
-t allocates terminal
Without this combination, interactive programs fail or exit immediately.
Port Mapping (-p) Makes Containers Reachable
Containers are isolated.
A service inside container on port 5000 is invisible externally until mapped.
Example:
docker run -p 80:5000 myapp
Meaning:
host port = 80
container port = 5000
Now browser traffic to host reaches the container.
Multiple examples:
docker run -p 8080:80 nginx
docker run -p 9000:5000 flaskapp
Volume Mapping (-v) Prevents Data Loss
Container filesystem disappears when container is removed.
To persist data:
docker run -v /host/data:/var/lib/mysql mysql
Meaning:
host path stores data permanently
container path uses that storage
Critical for:
databases
Jenkins
logs
uploads
Without volumes, restart means data loss.
Inspect Running Containers Properly
Logs
docker logs <container_name>
Useful for:
startup failures
runtime output
debugging
Inspect
docker inspect <container_name>
Returns full JSON:
network settings
mounted volumes
IP address
environment variables
Real Production Example: Jenkins in Docker
This is where all flags combine.
docker run \
-d \
-p 8080:8080 \
-p 50000:50000 \
-v /root/my-jenkins-data:/var/jenkins_home \
-u root \
jenkins
Breakdown:
-d
Run Jenkins in background
-p 8080:8080
Expose Jenkins UI
-p 50000:50000
Agent communication
-v
Persist Jenkins config and jobs
-u root
Override default permissions when required
This single command represents real-world Docker thinking.
Access Jenkins Two Ways
External Access
http://HOST-IP:8080
Internal Docker IP
Find internal IP:
docker inspect jenkins
Then:
http://172.x.x.x:8080
Retrieve Jenkins Initial Password
After startup:
docker logs <container_name>
Docker prints:
Please use the following password to proceed:
That unlocks Jenkins setup.
Because volume is mapped, restarting container preserves everything.
Essential Docker Run Flags Cheat Sheet
Flag Purpose Example
:tag Exact version ubuntu:17.10
-d Detached mode docker run -d nginx
-it Interactive terminal docker run -it ubuntu bash
-p Port mapping -p 8080:80
-v Volume persistence -v /data:/var/lib/mysql
docker logs View output docker logs app
docker inspect Full config docker inspect app
Final Thought
Docker looks simple until you realize docker run is actually infrastructure design compressed into one command.
Once you understand flags deeply, container behavior stops feeling magical and starts becoming predictable.
That is where real DevOps confidence begins.
Top comments (0)