Docker

Nginx di Docker #

Menjalankan Nginx di Docker adalah cara yang sangat praktis untuk eksperimen konfigurasi tanpa mengubah apapun di sistem host, dan juga merupakan cara yang umum digunakan di lingkungan production modern. Image resmi Nginx di Docker Hub adalah salah satu image yang paling banyak diunduh di seluruh ekosistem Docker.

Image Resmi Nginx #

Docker Hub menyediakan image Nginx resmi yang di-maintain oleh tim Nginx dan Docker. Ada beberapa tag yang perlu kamu ketahui:

# Pull image terbaru (sama dengan nginx:latest)
docker pull nginx

# Pull versi stable spesifik (direkomendasikan untuk production)
docker pull nginx:1.24

# Pull versi Alpine — jauh lebih kecil (sekitar 11 MB vs 140 MB)
docker pull nginx:1.24-alpine
# atau
docker pull nginx:alpine

Untuk production, selalu gunakan tag versi spesifik (nginx:1.24 atau nginx:1.24-alpine) daripada latest. Tag latest berubah setiap ada versi baru dan bisa menyebabkan perilaku tidak terduga saat container di-rebuild.


Menjalankan Nginx Dasar #

# Jalankan Nginx dan expose ke port 80 host
docker run -d --name my-nginx -p 80:80 nginx:1.24-alpine

# Cek container berjalan
docker ps

# Buka http://localhost di browser — seharusnya muncul halaman default Nginx

# Lihat log
docker logs my-nginx

# Stop dan hapus container
docker stop my-nginx && docker rm my-nginx

Mounting Konfigurasi Custom #

Container Nginx default menggunakan konfigurasi bawaan. Untuk menggunakan konfigurasi sendiri, mount file atau direktori dari host:

# Buat konfigurasi custom di host
mkdir -p ~/nginx-test/conf

cat > ~/nginx-test/conf/default.conf << 'EOF'
server {
    listen 80;
    server_name localhost;

    location / {
        root /usr/share/nginx/html;
        index index.html;
    }

    location /api/ {
        return 200 '{"status": "ok"}';
        add_header Content-Type application/json;
    }
}
EOF

# Jalankan dengan konfigurasi custom
docker run -d \
  --name my-nginx \
  -p 80:80 \
  -v ~/nginx-test/conf/default.conf:/etc/nginx/conf.d/default.conf:ro \
  nginx:1.24-alpine

Opsi :ro di akhir volume mount berarti read-only — container tidak bisa memodifikasi file konfigurasi.


Mounting File Statis #

Untuk melayani file statis dari host:

# Buat direktori dan file HTML sederhana
mkdir -p ~/nginx-test/html
echo "<h1>Hello from Docker Nginx!</h1>" > ~/nginx-test/html/index.html

# Jalankan dengan file statis dari host
docker run -d \
  --name my-nginx \
  -p 80:80 \
  -v ~/nginx-test/conf/default.conf:/etc/nginx/conf.d/default.conf:ro \
  -v ~/nginx-test/html:/usr/share/nginx/html:ro \
  nginx:1.24-alpine

Dockerfile: Membuat Image Custom #

Untuk deployment yang lebih bersih, buat Dockerfile yang menyertakan konfigurasi dan file statis langsung di dalam image:

# Dockerfile
FROM nginx:1.24-alpine

# Hapus konfigurasi default
RUN rm /etc/nginx/conf.d/default.conf

# Salin konfigurasi custom
COPY nginx.conf /etc/nginx/conf.d/default.conf

# Salin file statis (misalnya hasil build React/Vue)
COPY dist/ /usr/share/nginx/html/

# Expose port 80
EXPOSE 80

# CMD sudah di-set di base image, tidak perlu override
# Build image
docker build -t my-app:1.0 .

# Jalankan
docker run -d -p 80:80 my-app:1.0

Pendekatan ini ideal untuk aplikasi frontend (React, Vue, Angular) — build hasilnya disalin ke dalam image Nginx dan siap di-deploy.


Docker Compose: Nginx sebagai Reverse Proxy #

Kasus yang paling umum di production adalah menggunakan Nginx sebagai reverse proxy di depan aplikasi lain dalam satu stack Docker Compose:

# docker-compose.yml
version: '3.8'

services:
  nginx:
    image: nginx:1.24-alpine
    ports:
      - "80:80"
      - "443:443"
    volumes:
      - ./nginx/conf.d:/etc/nginx/conf.d:ro
      - ./nginx/ssl:/etc/nginx/ssl:ro
      - ./nginx/logs:/var/log/nginx
    depends_on:
      - app
    restart: unless-stopped

  app:
    image: node:18-alpine
    working_dir: /app
    volumes:
      - ./app:/app
    command: node server.js
    # Tidak expose port ke host — hanya accessible dari dalam network Docker
    expose:
      - "3000"
    restart: unless-stopped

  db:
    image: postgres:15-alpine
    environment:
      POSTGRES_DB: myapp
      POSTGRES_USER: user
      POSTGRES_PASSWORD: password
    volumes:
      - postgres_data:/var/lib/postgresql/data
    restart: unless-stopped

volumes:
  postgres_data:

Konfigurasi Nginx untuk stack ini:

# nginx/conf.d/app.conf
server {
    listen 80;
    server_name example.com;

    location / {
        # 'app' adalah nama service di docker-compose
        # Docker DNS resolution menangani resolusi nama ini
        proxy_pass http://app:3000;
        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;
    }
}
# Jalankan seluruh stack
docker compose up -d

# Lihat log semua service
docker compose logs -f

# Lihat log Nginx saja
docker compose logs -f nginx

# Stop semua service
docker compose down

Reload Konfigurasi di Container #

Ketika kamu mengubah konfigurasi Nginx di container yang sedang berjalan:

# Test konfigurasi dulu
docker exec my-nginx nginx -t

# Reload tanpa restart container (zero-downtime)
docker exec my-nginx nginx -s reload

# Atau jika menggunakan docker-compose:
docker compose exec nginx nginx -t
docker compose exec nginx nginx -s reload

Tips Umum Nginx di Docker #

Jangan run sebagai root di production. Image Nginx resmi sudah menjalankan worker processes sebagai user nginx, tapi master process berjalan sebagai root untuk bind port 80/443. Jika kamu butuh security yang lebih ketat, bisa override dengan user non-root dan gunakan port > 1024.

Gunakan tmpfs untuk cache. Jika Nginx dikonfigurasi untuk caching, mount direktori cache sebagai tmpfs (in-memory) untuk performa lebih baik:

# docker-compose.yml
services:
  nginx:
    image: nginx:1.24-alpine
    tmpfs:
      - /var/cache/nginx

Log ke stdout/stderr. Image Nginx resmi sudah mengkonfigurasi ini — access.log diarahkan ke /dev/stdout dan error.log ke /dev/stderr, sehingga docker logs langsung menampilkan Nginx logs.

# Cek konfigurasi log default di image resmi
docker run --rm nginx:1.24-alpine cat /etc/nginx/nginx.conf
# ...
# access_log  /var/log/nginx/access.log  main;
# Dan /var/log/nginx/access.log → symlink ke /dev/stdout

Ringkasan #

  • Gunakan tag versi spesifik (nginx:1.24-alpine) bukan latest untuk production — lebih predictable.
  • Alpine variant (~11 MB) jauh lebih kecil dari variant Debian (~140 MB) dan cukup untuk hampir semua kasus.
  • Mount konfigurasi dengan :ro (read-only) untuk keamanan tambahan.
  • Dalam Docker Compose, gunakan nama service sebagai hostname di proxy_pass — Docker DNS menangani resolusinya.
  • docker exec nginx nginx -s reload untuk reload konfigurasi tanpa restart container.
  • Image resmi sudah mengarahkan log ke stdout/stderr — langsung bisa dibaca via docker logs.

← Sebelumnya: CentOS / RHEL   Berikutnya: Compile dari Source →

About | Author | Content Scope | Editorial Policy | Privacy Policy | Disclaimer | Contact