1. Introduction & use cases
Docker packages an application with everything it needs – runtime, libraries, system tools, config – into an image. A running image is a container: an isolated process that behaves the same on your laptop, in CI and on the server. “Works on my machine” problems disappear.
A Dockerfile describes how to build the image; Docker Compose runs several containers (app, database, cache) together from one YAML file. Images are stored in registries such as Docker Hub, GitHub Container Registry or Amazon ECR.
Where it is used
- Identical development, test and production environments
- Running databases and tools locally without installing them
- Microservices and Kubernetes deployments
- CI pipelines that build and test in containers
2. Prerequisites
| You need | Version / notes |
|---|---|
| Windows / macOS | Docker Desktop (Windows needs WSL 2) |
| Linux | Docker Engine + Compose plugin |
| Hardware | 4 GB+ RAM recommended for Docker Desktop |
| Basics | Command line; the app you want to containerise |
3. Local setup
1Install Docker
Windows/macOS: install Docker Desktop from docker.com and start it. Linux: use Docker’s convenience script (or the official apt repository), then allow your user to run Docker.
BASHcurl -fsSL https://get.docker.com | sh sudo usermod -aG docker $USER # log out and in again docker version docker compose version2Run your first container
hello-worlddownloads a tiny image and prints a message; nginx shows a web server on port 8080.BASHdocker run hello-world docker run -d --name web -p 8080:80 nginx docker ps docker logs web docker rm -f web
4. Create & run a project
1Write a Dockerfile for a Node.js app
Copy
package*.jsonfirst so dependency installation is cached between builds; run as the non-rootnodeuser.DockerfileFROM node:lts-alpine WORKDIR /app ENV NODE_ENV=production COPY package*.json ./ RUN npm ci --omit=dev COPY . . USER node EXPOSE 3000 CMD ["node", "server.js"]2Exclude files from the build context
Keeps images small and secrets out.
.dockerignorenode_modules npm-debug.log .git .env dist Dockerfile compose.yaml3Build and run the image
Tag it with a name and version.
BASHdocker build -t my-app:1.0 . docker run -d --name my-app -p 3000:3000 --env-file .env my-app:1.0 curl localhost:30004Run the app with a database using Compose
One file describes all services, networks and volumes. Services reach each other by name (
db).compose.yamlservices: app: build: . ports: - "3000:3000" env_file: .env environment: DATABASE_URL: postgres://app:secret@db:5432/app depends_on: db: condition: service_healthy db: image: postgres:17 environment: POSTGRES_USER: app POSTGRES_PASSWORD: secret POSTGRES_DB: app volumes: - db-data:/var/lib/postgresql/data healthcheck: test: ["CMD-SHELL", "pg_isready -U app"] interval: 5s retries: 10 volumes: db-data:5Start and stop the stack
-druns in the background;--buildrebuilds the app image.BASHdocker compose up -d --build docker compose ps docker compose logs -f app docker compose down # add -v to delete volumes too
✅ When it works you should see output like Figure 1.
5. Development configuration
Environment variables
Pass configuration at runtime with --env-file, environment: or env_file: in Compose – do not bake secrets into images (anyone with the image can read them).
NODE_ENV=production
PORT=3000
JWT_SECRET=change-meKey dependencies
| Package / tool | Used for |
|---|---|
Dockerfile | Recipe for building an image |
.dockerignore | Files left out of the build |
compose.yaml | Multi-container apps |
Docker Hub / GHCR / ECR | Image registries |
docker buildx | Multi-platform builds (amd64 + arm64) |
Folder structure
my-app/
├── src/
├── Dockerfile
├── .dockerignore
├── compose.yaml # local stack
├── compose.prod.yaml # production overrides
└── .envEssential commands
| Command | What it does |
|---|---|
docker build -t name:tag . | Build an image |
docker run -d -p 3000:3000 name:tag | Run a container |
docker ps / docker logs -f NAME | List / follow logs |
docker exec -it NAME sh | Shell inside a container |
docker compose up -d / down | Start / stop a stack |
docker system prune | Remove unused data |
6. Production deployment
Server & hosting requirements
| Requirement | Recommendation |
|---|---|
| Server | Ubuntu VPS with Docker Engine + Compose plugin, 1–2 GB RAM+ |
| Registry | Docker Hub, GHCR or ECR for your images |
| Persistence | Named volumes or managed databases |
| Proxy + SSL | Nginx (or Traefik/Caddy) with Let’s Encrypt |
| Logs | Log rotation (max-size) |
Deployment steps
1Push the image to a registry
Tag it with the registry path and a version (here GitHub Container Registry).
BASHecho $GITHUB_TOKEN | docker login ghcr.io -u you --password-stdin docker build -t ghcr.io/you/my-app:1.0.0 . docker push ghcr.io/you/my-app:1.0.02Prepare an Ubuntu server
Create a VPS (AWS EC2, DigitalOcean, Hostinger VPS, Linode …) with Ubuntu 24.04 LTS, at least 1 vCPU and 1–2 GB RAM. Log in with SSH, update the system and create a non-root user for the app.
BASHssh root@YOUR_SERVER_IP apt update && apt upgrade -y adduser deploy usermod -aG sudo deploy # copy your SSH key to the new user, then log in as deploy rsync --archive --chown=deploy:deploy ~/.ssh /home/deploy exit ssh deploy@YOUR_SERVER_IP3Install Docker on the server
Same convenience script; add the deploy user to the docker group.
BASHcurl -fsSL https://get.docker.com | sudo sh sudo usermod -aG docker deploy4Production Compose file
Use the pushed image, restart automatically, expose the port only on localhost (Nginx publishes it) and limit log size.
/srv/my-app/compose.yamlservices: app: image: ghcr.io/you/my-app:1.0.0 restart: unless-stopped env_file: .env ports: - "127.0.0.1:3000:3000" logging: driver: json-file options: { max-size: "10m", max-file: "3" }5Start it
Pull and run. To update, change the tag and run the same commands.
BASHcd /srv/my-app docker compose pull docker compose up -d docker compose ps6Install Nginx as a reverse proxy
Nginx receives requests on ports 80/443 and forwards them to your app on
127.0.0.1:3000. Replaceexample.comwith your domain./etc/nginx/sites-available/example.comserver { listen 80; server_name example.com www.example.com; location / { proxy_pass http://127.0.0.1:3000; proxy_http_version 1.1; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header X-Forwarded-Proto $scheme; proxy_set_header Upgrade $http_upgrade; proxy_set_header Connection "upgrade"; } }7Enable the site and reload Nginx
Link the config into
sites-enabled, test it and reload.nginx -tmust say syntax is ok before you reload.BASHsudo apt install -y nginx sudo ln -s /etc/nginx/sites-available/example.com /etc/nginx/sites-enabled/ sudo nginx -t sudo systemctl reload nginx8Open only the ports you need
Allow SSH, HTTP and HTTPS through the firewall. Application ports (3000, 8000 …) stay closed to the internet – Nginx forwards traffic to them.
BASHsudo ufw allow OpenSSH sudo ufw allow 'Nginx Full' # ports 80 and 443 (after installing Nginx) sudo ufw enable sudo ufw status9Point your domain to the server
In your domain registrar's DNS panel add an A record for
@(andwww) with the server's public IP address. DNS changes usually appear within minutes but can take up to 24–48 hours. Check withdigor an online DNS checker.OUTPUT# Type Name Value TTL # A @ 203.0.113.10 3600 # A www 203.0.113.10 3600 dig +short example.com10Add free HTTPS with Let’s Encrypt
Certbot gets a free SSL certificate, adds it to the Nginx config and redirects HTTP to HTTPS. Certificates renew automatically (a systemd timer runs twice a day); test renewal once with
--dry-run.BASHsudo apt install -y certbot python3-certbot-nginx sudo certbot --nginx -d example.com -d www.example.com sudo certbot renew --dry-run
🗺 The whole path from your code to the live site is drawn in the deployment diagram.
7. Images & screenshots
Illustrations drawn for this guide from the commands above. Product dashboards change often, so for screens such as cloud consoles follow the step text and the official documentation.
8. Common errors & solutions
permission denied while trying to connect to the Docker daemon socket
Add your user to the docker group (sudo usermod -aG docker $USER) and log in again.
Bind for 0.0.0.0:3000 failed: port is already allocated
Another container or process uses the port. Stop it or map a different host port (-p 3001:3000).
App cannot reach the database at localhost
Inside a container localhost is the container itself. Use the Compose service name (db) as host.
Container exits immediately
Check docker logs NAME. The main process must keep running in the foreground (no &, no daemon mode).
exec format error
The image was built for another CPU (arm64 vs amd64). Build for the server’s platform: docker buildx build --platform linux/amd64.
Disk full on the server
Old images and logs pile up. docker system prune -a and set log rotation.
Best practices
- Use small official base images and pin versions (
postgres:17, notlatest). - Multi-stage builds: build tools in one stage, only runtime files in the final image.
- Run as a non-root user.
- Keep secrets out of images; pass them at runtime.
- Scan images for vulnerabilities (
docker scout, Trivy).