๐ Master Docker: Essential Commands and Troubleshooting Tips


Author: Shams Raza
Tags: #Docker
#DevOps
#Containers
#Linux
#Troubleshooting
Reading Time: ~7 min
GitHub: Docker-Learn
Containers are now a standard in modern application development, and Docker is the most popular containerization platform. Whether you're a beginner or looking to level up your Docker skills, this guide walks you through:
โ
Docker basics
โ
Useful commands (Beginner โ Advanced)
โ
Common Docker issues and how to fix them
๐ณ What is Docker?
Docker is an open-source platform to build, ship, and run applications inside containers. It packages software with all dependencies, ensuring consistent environments across development, testing, and production.
๐งฑ Docker Basics
๐ฆ 1. Install Docker
Windows/Mac: Install Docker Desktop from https://www.docker.com/products/docker-desktop
Linux:
sudo apt update
sudo apt install docker.io -y
sudo systemctl enable --now docker
Add your user to the Docker group:
sudo usermod -aG docker $USER
๐ ๏ธ 2. Basic Docker Commands
Task | Command |
Check Docker version | docker version |
Pull image | docker pull nginx |
Run container | docker run -d -p 80:80 nginx |
List containers | docker ps -a |
Stop container | docker stop <container_id> |
Remove container | docker rm <container_id> |
Remove image | docker rmi <image_id> |
Example:
docker run -it ubuntu bash
โ๏ธ Intermediate Docker Commands
๐งฐ Image Building
Create a Dockerfile
:
FROM python:3.11-slim
WORKDIR /app
COPY . .
RUN pip install -r requirements.txt
CMD ["python", "app.py"]
Build and tag the image:
docker build -t my-python-app .
docker images
๐งช Docker Volumes
docker volume create my_data
docker run -v my_data:/data busybox
Useful for persisting data across container restarts.
๐งโ๐คโ๐ง Docker Networking
docker network create my_net
docker run -d --name app1 --network my_net nginx
docker run -it --network my_net busybox
Now from busybox
you can ping app1
.
๐ง Advanced Docker Commands
๐งญ Multi-Stage Builds
Efficiently build lean images:
# stage 1
FROM golang:1.19 as builder
WORKDIR /src
COPY . .
RUN go build -o app .
# stage 2
FROM alpine
COPY --from=builder /src/app /app
CMD ["/app"]
๐งช Health Checks
HEALTHCHECK CMD curl --fail http://localhost:80 || exit 1
Add this in Dockerfile to check container health.
๐ณ Docker Compose (for multi-container apps)
docker-compose.yml
:
version: "3.9"
services:
web:
image: nginx
ports:
- "80:80"
app:
build: .
volumes:
- .:/app
depends_on:
- web
Run with:
docker-compose up --build
๐งฏ Docker Troubleshooting
โ Error: "Permission denied" when using Docker
Fix:
sudo usermod -aG docker $USER
newgrp docker
โ Container exits immediately
Check logs:
docker logs <container_id>
Run in interactive mode to debug:
docker run -it <image> /bin/bash
โ Port already in use
Check and kill:
sudo lsof -i :80
sudo kill -9 <PID>
โ Cannot connect to Docker daemon
Check service:
sudo systemctl status docker
sudo systemctl restart docker
โ Image too large
Use Alpine base:
FROM python:3.11-alpine
Clean up:
docker system prune
docker builder prune
๐ Conclusion
Docker makes development and deployment faster, more consistent, and scalable. Mastering its commands and troubleshooting skills helps you become a better DevOps engineer or backend developer.
โ Start small, build fast, and troubleshoot confidently.
๐ Bonus Resources
๐ฌ Have questions or tips to share? Drop them in the comments below!
๐ Follow me for more DevOps and cloud articles.
Subscribe to my newsletter
Read articles from Shams Raza directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by

Shams Raza
Shams Raza
๐น Azure Hybrid Cloud Engineer | DevOps Designing, automating, and scaling secure infrastructure across on-prem and Azure and AWS cloud infra. โ๏ธ๐ง Skilled in IaC (Terraform), CI/CD (GitHub Actions, Azure DevOps), scripting (PowerShell, Bash), and containerization (Docker, Kubernetes) ๐ณโ๏ธ Passionate about bridging the gap between legacy systems and modern cloud-native solutions ๐ก๐ On a mission to automate everything and simplify the complex.