🐳 Day 3: Mastering docker run Command - With examlpes!

Ibrar AnsariIbrar Ansari
4 min read

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

Watch on Youtube


πŸ”Ή 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)

FlagDescription
-itInteractive terminal
--rmRemove after exit
-dRun in background (detached)
-pPort mapping
-vMount volume
--nameSet container name
--env, -eSet environment variable
--networkConnect to a custom network
--cpus / --memorySet resource limits
--restartSet restart policy
--entrypointOverride entry command

Happy Dockering! 🐳✨

0
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!