DEV Community

Anusha Kuppili
Anusha Kuppili

Posted on

Mastering Advanced Docker Run: Beyond Basic Container Execution

Most people learn Docker with one command:

docker run ubuntu
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

This means:

docker run redis:latest
Enter fullscreen mode Exit fullscreen mode

That may silently change behavior later when upstream images update.

Always pin versions:

docker run redis:4.0
docker run ubuntu:17.10
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

Now your terminal is free.

Check running containers:

docker ps
Enter fullscreen mode Exit fullscreen mode

Stop later:

docker stop <container_id>
Enter fullscreen mode Exit fullscreen mode

Attach again if needed:

docker attach <container_id>
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

Meaning:

host port = 80

container port = 5000
Enter fullscreen mode Exit fullscreen mode

Now browser traffic to host reaches the container.

Multiple examples:

docker run -p 8080:80 nginx
docker run -p 9000:5000 flaskapp
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

Meaning:

host path stores data permanently

container path uses that storage
Enter fullscreen mode Exit fullscreen mode

Critical for:

databases

Jenkins

logs

uploads

Without volumes, restart means data loss.

Inspect Running Containers Properly
Logs

docker logs <container_name>
Enter fullscreen mode Exit fullscreen mode

Useful for:

  • startup failures

  • runtime output

  • debugging

Inspect

docker inspect <container_name>
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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.
Enter fullscreen mode Exit fullscreen mode

Access Jenkins Two Ways
External Access
http://HOST-IP:8080
Internal Docker IP

Find internal IP:

docker inspect jenkins
Enter fullscreen mode Exit fullscreen mode

Then:

http://172.x.x.x:8080
Enter fullscreen mode Exit fullscreen mode

Retrieve Jenkins Initial Password

After startup:

docker logs <container_name>
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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)