๐Ÿš€ Master Docker: Essential Commands and Troubleshooting Tips

Shams RazaShams Raza
3 min read

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

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

TaskCommand
Check Docker versiondocker version
Pull imagedocker pull nginx
Run containerdocker run -d -p 80:80 nginx
List containersdocker ps -a
Stop containerdocker stop <container_id>
Remove containerdocker rm <container_id>
Remove imagedocker 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.


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