π³ Day 3: Mastering docker run Command - With examlpes!

Table of contents
- π¬ Video Demonstration
- πΉ Abbreviation
- πΉ Basic Usage
- πΉ Interactive & Detached Modes
- πΉ Custom Images and Tags
- πΉ Networking Options
- πΉ Volume Mounts (Persistent Storage)
- πΉ Environment Variables & User Settings
- πΉ Port Mapping
- πΉ Working Directory & Entrypoint
- πΉ Resource Limits
- πΉ Restart Policies
- πΉ Advanced Use Cases
- πΉ Helpful Tips π‘
- β Most Common Flags Reference (Cheat Sheet)

Docker's run
command is one of the most essential and versatile tools in a developer's container toolbox. Below is a comprehensive list of docker run
options, examples, and use cases β perfect for beginners and intermediate Docker users!
π¬ Video Demonstration
πΉ Abbreviation
dia = `DevOps in Action`
πΉ Basic Usage
# π Run a container & remove it after execution
docker run --rm ubuntu echo "Hello from container"
# π₯οΈ Run and ping a public DNS (Google) from busybox container
docker run --rm busybox ping 8.8.8.8
πΉ Interactive & Detached Modes
# π Run nginx demo container (foreground by default)
docker run nginx
# π§ͺ Run with interactive terminal
docker run -it ubuntu
# π Detached (background) + interactive
docker run -itd --name=dia ubuntu:latest /bin/bash
## -i: Keep STDIN open even if not attached.
## -t: Allocate a pseudo-TTY (terminal).
# Runs the container in the foreground, attaching your terminal to the container's shell. You can interact with it directly (e.g., run commands like bash).
# The container stops when you exit the shell (e.g., by typing exit).
## -d: Run container in detached mode (i.e., in the background).
# Includes the -d (detached) flag, which runs the container in the **background**.
# The container starts and runs independently of your terminal. You won't get an interactive shell unless you explicitly attach to it later (e.g., using docker attach or docker exec).
## Key Difference:
# -it: Runs the container in the foreground with an interactive terminal.
# -itd: Runs the container in the background, detached from your terminal.
πΉ Custom Images and Tags
# π οΈ network-debug-toolss with specific tag
docker run -it --rm --name=dia ibraransaridocker/network-debug-tools:latest /bin/bash
πΉ Networking Options
# π Use a custom network
docker network create dia_network
docker run -it --rm --network=dia_network ubuntu
# π§· Custom DNS
docker run -it --name=dia --dns=8.8.8.8 ubuntu:latest /bin/bash
#cat /etc/resolv.conf
# π§ Network alias
docker run -it --rm --name=dia --network=dia_network --network-alias=web ibraransaridocker/network-debug-tools /bin/bash
# Inspect the Container alias: docker inspect dia
docker run -it --rm --name=dia1 --network=dia_network --network-alias=db ibraransaridocker/network-debug-tools /bin/bash
# From the second container, ping the alias: ping web or db
## The --network-alias is helpful when:
#- You're using service discovery among microservices (e.g., web, db, cache) without needing fixed container names or IPs.
#- You want to refer to a container by a specific hostname within a custom Docker network.
πΉ Volume Mounts (Persistent Storage)
# π½ Mount volume from host to container
docker run -it --rm --name=dia -p 8080:80 ibraransaridocker/nginx-demo
docker run -it --rm --name=dia -p 8080:80 -v $(pwd)/html:/usr/share/nginx/html/ nginx
# π Read-only volume mount
docker run -itd --name=dia -p 8080:80 -v $(pwd)/html:/usr/share/nginx/html:ro nginx
# docker exec -it dia bash
# cd /usr/share/nginx/html
# echo "Test" >> index.html
πΉ Environment Variables & User Settings
# πΏ Set environment variable
docker run -it --name=dia -e user=devopsinaction ubuntu env
docker run -itd --name=dia -e user=devopsinaction ubuntu /bin/bash
# docker exec -it dia bash
# printenv
# π€ Run container with specific UID & GID
docker run -it --name=dia -u 1000:1000 ubuntu /bin/bash
# id
πΉ Port Mapping
docker run -it --rm --name=dia -p 8080:80 ibraransaridocker/nginx-demo
docker run -it --rm --name=dia -p 8080:80 -p 8081:80 ibraransaridocker/nginx-demo
docker run -it --rm --name=dia -p 8080:80 -p 8000-8005:8000-8005 ibraransaridocker/nginx-demo
πΉ Working Directory & Entrypoint
# π Set working directory to /app
docker run -it --name=dia -w /app node:alpine sh
# πͺ Override the default entrypoint
docker run -it --name=dia --entrypoint=/bin/sh ubuntu:latest
πΉ Resource Limits
# π§ Limit memory and CPU usage
docker run -it --name=dia --memory="256m" --cpus="1.0" ibraransaridocker/network-debug-tools
docker stats dia
#load test inside container
# apt update
# apt install stress
# stress --cpu 1 --vm 1 --vm-bytes 300M --timeout 30s
πΉ Restart Policies
# π Auto-restart container on failure
docker run -dit --name=dia --restart=always nginx
πΉ Advanced Use Cases
# β€οΈ Set custom hostname
docker run -itd --name=dia --hostname=webserver ubuntu:latest /bin/bash
# docker exec -it dia bash
# hostname
# cat /etc/hostname
# π Add health check
docker run -itd --name=dia --health-cmd="curl --fail http://localhost || exit 1" --health-interval=30s nginx
# π Share hostβs PID namespace
docker run -itd --name=dia --pid=host ubuntu:latest /bin/bash
πΉ Helpful Tips π‘
Use
--rm
for disposable containers.Combine
-itd
for interactive and background usage.Bind mount volumes (
-v
) for local development.Use
--env
or--env-file
to pass environment variables in bulk.Health checks help in container orchestration.
β Most Common Flags Reference (Cheat Sheet)
Flag | Description |
-it | Interactive terminal |
--rm | Remove after exit |
-d | Run in background (detached) |
-p | Port mapping |
-v | Mount volume |
--name | Set container name |
--env , -e | Set environment variable |
--network | Connect to a custom network |
--cpus / --memory | Set resource limits |
--restart | Set restart policy |
--entrypoint | Override entry command |
Happy Dockering! π³β¨
Subscribe to my newsletter
Read articles from Ibrar Ansari directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by

Ibrar Ansari
Ibrar Ansari
Hello! I'm a DevOps Engineer dedicated to continuous learning and contributing to the tech community. If you find value in what I share, feel free to spread the word to others who might benefit as well. Your support is greatly appreciated!