DevOps & Deployment

Docker Setup & Deployment Guide

Package your app and its dependencies into containers – write a Dockerfile, run services with Compose and deploy the same image to production.

Updated Official documentation ↗

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 needVersion / notes
Windows / macOSDocker Desktop (Windows needs WSL 2)
LinuxDocker Engine + Compose plugin
Hardware4 GB+ RAM recommended for Docker Desktop
BasicsCommand line; the app you want to containerise

3. Local setup

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

    BASH
    curl -fsSL https://get.docker.com | sh
    sudo usermod -aG docker $USER   # log out and in again
    docker version
    docker compose version
  2. 2Run your first container

    hello-world downloads a tiny image and prints a message; nginx shows a web server on port 8080.

    BASH
    docker 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

  1. 1Write a Dockerfile for a Node.js app

    Copy package*.json first so dependency installation is cached between builds; run as the non-root node user.

    Dockerfile
    FROM 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"]
  2. 2Exclude files from the build context

    Keeps images small and secrets out.

    .dockerignore
    node_modules
    npm-debug.log
    .git
    .env
    dist
    Dockerfile
    compose.yaml
  3. 3Build and run the image

    Tag it with a name and version.

    BASH
    docker build -t my-app:1.0 .
    docker run -d --name my-app -p 3000:3000 --env-file .env my-app:1.0
    curl localhost:3000
  4. 4Run the app with a database using Compose

    One file describes all services, networks and volumes. Services reach each other by name (db).

    compose.yaml
    services:
      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:
  5. 5Start and stop the stack

    -d runs in the background; --build rebuilds the app image.

    BASH
    docker 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).

.env
NODE_ENV=production
PORT=3000
JWT_SECRET=change-me

Key dependencies

Package / toolUsed for
DockerfileRecipe for building an image
.dockerignoreFiles left out of the build
compose.yamlMulti-container apps
Docker Hub / GHCR / ECRImage registries
docker buildxMulti-platform builds (amd64 + arm64)

Folder structure

project structure
my-app/
├── src/
├── Dockerfile
├── .dockerignore
├── compose.yaml        # local stack
├── compose.prod.yaml   # production overrides
└── .env

Essential commands

CommandWhat it does
docker build -t name:tag .Build an image
docker run -d -p 3000:3000 name:tagRun a container
docker ps / docker logs -f NAMEList / follow logs
docker exec -it NAME shShell inside a container
docker compose up -d / downStart / stop a stack
docker system pruneRemove unused data

6. Production deployment

Server & hosting requirements

RequirementRecommendation
ServerUbuntu VPS with Docker Engine + Compose plugin, 1–2 GB RAM+
RegistryDocker Hub, GHCR or ECR for your images
PersistenceNamed volumes or managed databases
Proxy + SSLNginx (or Traefik/Caddy) with Let’s Encrypt
LogsLog rotation (max-size)

Deployment steps

  1. 1Push the image to a registry

    Tag it with the registry path and a version (here GitHub Container Registry).

    BASH
    echo $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.0
  2. 2Prepare 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.

    BASH
    ssh 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_IP
  3. 3Install Docker on the server

    Same convenience script; add the deploy user to the docker group.

    BASH
    curl -fsSL https://get.docker.com | sudo sh
    sudo usermod -aG docker deploy
  4. 4Production 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.yaml
    services:
      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" }
  5. 5Start it

    Pull and run. To update, change the tag and run the same commands.

    BASH
    cd /srv/my-app
    docker compose pull
    docker compose up -d
    docker compose ps
  6. 6Install Nginx as a reverse proxy

    Nginx receives requests on ports 80/443 and forwards them to your app on 127.0.0.1:3000. Replace example.com with your domain.

    /etc/nginx/sites-available/example.com
    server {
        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";
        }
    }
  7. 7Enable the site and reload Nginx

    Link the config into sites-enabled, test it and reload. nginx -t must say syntax is ok before you reload.

    BASH
    sudo apt install -y nginx
    sudo ln -s /etc/nginx/sites-available/example.com /etc/nginx/sites-enabled/
    sudo nginx -t
    sudo systemctl reload nginx
  8. 8Open 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.

    BASH
    sudo ufw allow OpenSSH
    sudo ufw allow 'Nginx Full'   # ports 80 and 443 (after installing Nginx)
    sudo ufw enable
    sudo ufw status
  9. 9Point your domain to the server

    In your domain registrar's DNS panel add an A record for @ (and www) with the server's public IP address. DNS changes usually appear within minutes but can take up to 24–48 hours. Check with dig or 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.com
  10. 10Add 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.

    BASH
    sudo 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.

terminal$ docker compose up -d --build[+] Building 14.2s (10/10) FINISHED[+] Running 3/3 ✔ Network my-app_default Created ✔ Container my-app-db-1 Healthy ✔ Container my-app-app-1 Started$ docker compose psNAME IMAGE STATUS PORTSmy-app-app-1 my-app-app Up 12 seconds 0.0.0.0:3000->3000/tcpmy-app-db-1 postgres:17 Up 18 seconds (healthy) 5432/tcp
Figure 1: terminal output of the sample Docker project (section 4).
1Dockerfile2docker build + tag3Push to registry4compose pull/up onserver5Nginx + HTTPS
Figure 2: production deployment flow (section 6).
my-app/├── src/├── Dockerfile├── .dockerignore├── compose.yaml # local stack├── compose.prod.yaml # production overrides└── .env
Figure 3: project folder structure (section 5).

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, not latest).
  • 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).