Skip to content

Docker Containers Management

Getting containers information on Docker Host

List running containers

docker container ls
docker ps

List all containers

docker container ls -a
docker ps -a

List Container IDs of running container

docker container ls -q

List container IDs of all containers

docker container ls -aq

Creating Docker container

docker container create <image-name>

🛠️ Core Container Identity

Option Example Description
--name --name my-nginx Assigns a name to the container
--hostname --hostname webhost Sets internal hostname
--domainname --domainname example.com Sets domain name
--mac-address --mac-address 02:42:ac:11:65:43 Sets MAC address
--add-host --add-host db.local:192.168.1.100 Adds custom host-to-IP mapping

🧠 Resource Limits

Option Example Description
--cpus --cpus="1.5" Limits CPU usage
--cpu-shares --cpu-shares=512 Relative CPU weight
--cpu-quota --cpu-quota=50000 Microseconds per period
--memory --memory=512m RAM limit
--memory-swap --memory-swap=1g RAM + swap limit
--memory-reservation --memory-reservation=256m Soft memory limit
--kernel-memory --kernel-memory=50m Kernel memory cap
--device-read-bps --device-read-bps=/dev/sda:1mb Disk read rate
--device-write-bps --device-write-bps=/dev/sda:1mb Disk write rate
--blkio-weight --blkio-weight=500 Block IO weight

📦 Storage & Volumes

Option Example Description
--volume -v /data:/app/data Bind mount
--mount --mount type=bind,source=/data,target=/app/data Advanced mount
--tmpfs --tmpfs /run Mount tmpfs
--storage-opt --storage-opt size=1G Storage driver config

🌐 Networking

Option Example Description
--network --network=host Network mode
--ip --ip=192.168.1.50 Static IP
--ip6 --ip6=2001:db8::1 Static IPv6
--dns --dns=8.8.8.8 Custom DNS
--dns-search --dns-search=example.com DNS search domain
--expose --expose=8080 Exposes port (internal only)
--publish -p 8080:80 Maps container port to host
--link --link db:dbhost Legacy container linking

🧾 Miscellaneous

Option Example Description
--label --label team=infra Adds metadata
--restart --restart=always Restart policy
--log-driver --log-driver=json-file Logging backend
--log-opt --log-opt max-size=10m Logging options
--pid --pid=host Shares host PID namespace
--ipc --ipc=host Shares IPC namespace
--userns --userns=host Shares user namespace
--shm-size --shm-size=256m Size of /dev/shm
--stop-signal --stop-signal=SIGTERM Signal to stop
--stop-timeout --stop-timeout=30 Grace period before kill

Starting a docker container

docker container start <container-name>
Option Example Description
--attach / -a docker start -a mycontainer Attach STDOUT/STDERR and stream logs to terminal
--interactive / -i docker start -ai mycontainer Attach and keep STDIN open (interactive mode)
--detach-keys docker start --detach-keys="ctrl-x,ctrl-y" mycontainer Override default detaches key sequence
--checkpoint docker start --checkpoint=webchkpt mycontainer Restore container from a checkpoint (experimental)
--checkpoint-dir docker start --checkpoint-dir=/mnt/checkpoints mycontainer Directory for checkpoint files

Run Docker container

docker container run <image-name/container-name>

All options in docker container create and docker container start can be used

Remove container when it exits

docker container run -rm image-name

Stopping docker container

docker container stop <container-name/id>

Stop all running containers

docker container stop $(docker container ls -q)

Remove Containers

docker container rm <container-name>

Remove all stopped containers

docker container prune

Remove all containers

docker container rm $(docker container ls -aq)

Restart policies of containers

Scenario no always unless-stopped on-failure
Crashes with error
Manual stop (docker stop)
Host reboot
Retry limit ✅ (with :max-retries)

Copy files

From host to container

docker container cp /host/file/path container-name:/path

From container to hosts

docker container cp container-name:/path /local/file/path

:::info destination path mentioned need to be existing, else it fails :::


Container port mapping -p or —publish

docker run -p 3000:3000 image-name

Mapping a port to a specific network interface

docker run -p 10.0.0.2:8080:80 image-name

Random port Mapping

docker run -p 80 image-name

:::info -p 80 will map any port from host to port 80 on container
The port ranges between 32768 to 60999
This is defined in /proc/sys/net/ipv4/ip_local_port_range :::

Publishing UDP ports

:::info By default, port published will be supporting TCP only :::

docker run -p 53:53/udp image-name