D: Docker has revolutionized the way we develop, deploy, and manage applications. Whether you’re just starting out or looking to level up your Docker skills, mastering essential commands is key! This guide covers everything from basic container management to advanced orchestration.
πΉ 1. Getting Started with Docker
Installation & Setup
First, ensure Docker is installed:
docker --version # Check installed version
For installation instructions, visit Docker’s official docs.
Hello World!
Run your first container:
docker run hello-world # Downloads and runs a test image
πΉ 2. Essential Docker Commands
Managing Images
- Pull an image (download from Docker Hub):
docker pull nginx:latest # Downloads the latest Nginx image
- List images:
docker images # Shows all downloaded images
- Remove an image:
docker rmi nginx # Deletes the Nginx image
Running Containers
-
Start a container:
docker run -d -p 8080:80 --name my-nginx nginx # Runs Nginx in detached mode
-d
: Detached mode (runs in background)-p 8080:80
: Maps host port 8080 to container port 80--name
: Assigns a custom name
-
List running containers:
docker ps # Shows active containers docker ps -a # Shows all containers (including stopped ones)
-
Stop & Remove a container:
docker stop my-nginx # Stops the container docker rm my-nginx # Removes the container
Logs & Debugging
- View logs:
docker logs my-nginx # Shows container logs
- Enter a running container:
docker exec -it my-nginx bash # Opens a shell inside the container
πΉ 3. Advanced Docker Commands
Networking
- List networks:
docker network ls
- Create a custom network:
docker network create my-network
Volumes (Persistent Storage)
- Create a volume:
docker volume create my-volume
- Mount a volume in a container:
docker run -v my-volume:/data nginx # Mounts 'my-volume' to '/data'
Docker Compose (Multi-Container Apps)
Create a docker-compose.yml
file:
version: '3'
services:
web:
image: nginx
ports:
- "8080:80"
db:
image: mysql
environment:
MYSQL_ROOT_PASSWORD: example
Then run:
docker-compose up -d # Starts all services
πΉ 4. Pro Tips & Best Practices
β Clean up unused resources:
docker system prune # Removes stopped containers, unused networks, etc.
β Optimize images with multi-stage builds:
# Stage 1: Build
FROM node:14 as builder
WORKDIR /app
COPY . .
RUN npm install && npm run build
# Stage 2: Run
FROM nginx:alpine
COPY --from=builder /app/dist /usr/share/nginx/html
β
Use .dockerignore
to exclude unnecessary files (like node_modules
).
π Conclusion
From basic docker run
to advanced orchestration with Docker Compose, mastering these commands will make you a Docker pro! π
π‘ Next Steps:
- Explore Docker Swarm/Kubernetes for clustering.
- Learn about Docker security best practices.
Got questions? Drop them in the comments! π #Docker #DevOps #Containers