Learn Docker & Docker Compose
A concise guide to Docker and Docker Compose for containerizing and managing applications.*
Introduction
Docker lets you package applications into containers—lightweight, portable, and consistent environments. Docker Compose allows you to define and run multi-container applications.
1. Check Docker Installation
docker --version # Show Docker version
docker info # Display Docker system-wide information
Use these to verify that Docker is correctly installed and running.
2. Basic Docker Commands
docker run hello-world # Run a test container to verify setup
docker ps # List running containers
docker ps -a # List all containers (running and stopped)
docker images # List downloaded container images
docker rm <container_id> # Remove a stopped container
docker rmi <image_id> # Remove an image
These commands help you work with containers and manage their lifecycle.
3. Running Containers
docker run -it ubuntu bash # Run an interactive shell inside Ubuntu
-it
makes the container interactive (like SSH), and bash
is the shell command you want to run.
docker run -d -p 8080:80 nginx # Run Nginx in detached mode and map port 80 to 8080
-d
runs in the background (detached), and -p
maps ports from container to host.
4. Building Images
# Dockerfile
FROM python:3.10-slim
COPY . /app
WORKDIR /app
RUN pip install -r requirements.txt
CMD ["python", "app.py"]
docker build -t my-python-app . # Build image from Dockerfile in current directory
docker run my-python-app # Run container from the built image
This defines and builds a custom image for your Python app.
5. Docker Volumes
docker volume create mydata # Create a volume
docker run -v mydata:/data ubuntu # Mount volume into container
Volumes are used for persistent data storage independent of the container lifecycle.
6. Docker Compose Basics
# docker-compose.yml
version: "3.8"
services:
web:
image: nginx
ports:
- "8080:80"
redis:
image: redis
docker-compose up # Start services defined in the compose file
docker-compose down # Stop and remove containers
Compose allows you to manage multi-container setups with a single file.
7. Common Compose Options
docker-compose up -d # Run in detached mode
docker-compose ps # List running services
docker-compose logs # View logs from services
docker-compose exec web bash # Execute command in running container
Useful for monitoring and interacting with your services.
8. Environment Variables
# .env
APP_ENV=production
# docker-compose.yml
services:
app:
image: my-app
environment:
- APP_ENV=${APP_ENV}
Using .env
files helps manage config across environments cleanly.
9. Docker Networks
docker network create my-net # Create a custom network
docker run --network my-net ... # Connect container to network
Containers on the same custom network can communicate using service names.
10. Clean Up Resources
docker system prune # Remove unused data (containers, images, networks)
docker-compose down --volumes # Remove volumes too
Use this to reclaim space and avoid clutter.
Conclusion
Docker and Docker Compose simplify application packaging and multi-service orchestration. Learn these tools to build reproducible and scalable environments.
Happy containerizing! 🐳