Docker Deployment Guide for Beginners

*Photo by Wolfgang Weiser on Pexels*
Docker changed how developers deploy software. Instead of “it works on my machine,” Docker packages your application with all its dependencies into a container that runs identically everywhere. This guide walks you through deploying your first containerized application on a Linux server.
## What Is Docker?
Docker is a platform for running applications in isolated environments called containers. A container includes everything the application needs: code, runtime, system tools, libraries, and settings.
### Containers vs. Virtual Machines
| Aspect | Container | Virtual Machine |
|---|---|---|
| Startup time | Seconds | Minutes |
| Size | MBs | GBs |
| Resource usage | Shares host OS | Full OS per VM |
| Isolation | Process-level | Hardware-level |
| Ideal for | Microservices, apps | Full environments |
## Installing Docker on Ubuntu 24.04
“`bash
# Update package index
sudo apt update
# Install prerequisites
sudo apt install -y ca-certificates curl
# Add Docker’s official GPG key
sudo install -m 0755 -d /etc/apt/keyrings
sudo curl -fsSL https://download.docker.com/linux/ubuntu/gpg -o /etc/apt/keyrings/docker.asc
sudo chmod a+r /etc/apt/keyrings/docker.asc
# Add the repository
echo “deb [arch=$(dpkg –print-architecture) signed-by=/etc/apt/keyrings/docker.asc] https://download.docker.com/linux/ubuntu $(. /etc/os-release && echo “$VERSION_CODENAME”) stable” | sudo tee /etc/apt/sources.list.d/docker.list > /dev/null
# Install Docker
sudo apt update
sudo apt install -y docker-ce docker-ce-cli containerd.io
# Verify installation
sudo docker run hello-world
“`
## Docker Compose for Multi-Container Apps
Docker Compose lets you define multiple containers in a single YAML file. This is essential for applications that need a web server, database, and cache.
### Example docker-compose.yml
“`yaml
version: ‘3.8’
services:
web:
image: nginx:latest
ports:
- “80:80”
volumes:
- ./html:/usr/share/nginx/html
depends_on:
- db
db:
image: postgres:16
environment:
POSTGRES_DB: myapp
POSTGRES_USER: user
POSTGRES_PASSWORD: ${DB_PASSWORD}
volumes:
- pgdata:/var/lib/postgresql/data
volumes:
pgdata:
“`
Run with:
“`bash
docker-compose up -d
“`
## Dockerfile: Building Your Own Image
A Dockerfile defines how to build your application image.
### Example Python App Dockerfile
“`dockerfile
FROM python:3.12-slim
WORKDIR /app
COPY requirements.txt .
RUN pip install –no-cache-dir -r requirements.txt
COPY . .
EXPOSE 8000
CMD [“gunicorn”, “–bind”, “0.0.0.0:8000”, “app:app”]
“`
Build and run:
“`bash
docker build -t myapp .
docker run -d -p 8000:8000 myapp
“`
## Essential Docker Commands
“`bash
# List running containers
docker ps
# List all containers (including stopped)
docker ps -a
# View logs
docker logs -f container_name
# Stop a container
docker stop container_name
# Remove unused resources
docker system prune -a
# Execute command in running container
docker exec -it container_name bash
# View resource usage
docker stats
“`
## Common Deployment Patterns
### Pattern 1: Web App + Database
Use Docker Compose to run both services. The web app connects to the database using the service name as hostname.
### Pattern 2: Reverse Proxy + Multiple Apps
Use Nginx as a reverse proxy in one container, routing traffic to multiple application containers behind it.
### Pattern 3: Zero-Downtime Deployment
Use Docker Swarm or a simple blue-green deployment with two container instances and a load balancer.
## Security Best Practices
- Use official images — They are maintained and scanned for vulnerabilities
- Do not run as root — Use `USER` directive in Dockerfiles
- Scan images regularly — `docker scout` or Trivy for vulnerability scanning
- Use .dockerignore — Exclude sensitive files from builds
- Limit resource usage — Set memory and CPU limits on containers
- Never store secrets in images — Use environment variables or secret managers
## Conclusion
Docker simplifies deployment by packaging applications with their dependencies. Start with a single container, move to Docker Compose for multi-service apps, and follow security best practices from day one. The command line examples above work on any Ubuntu 24.04 server.
For more deployment scripts, Docker configurations, and automation tools, visit freeresource.store.
*For more developer tools and resources, visit freeresource.store.*