Running Nginx in Docker
Nginx is one of the most popular Docker images. This guide covers common use cases.
Quick Start
docker run -d -p 80:80 --name webserver nginx
Visit http://YOUR_SERVER_IP to see the Nginx welcome page.
Serve Custom HTML
mkdir ~/mysite
echo "<h1>Hello World!</h1>" > ~/mysite/index.html
docker run -d -p 80:80 --name webserver -v ~/mysite:/usr/share/nginx/html:ro nginx
Use a Custom nginx.conf
docker run -d -p 80:80 --name webserver -v ~/mysite:/usr/share/nginx/html:ro -v ~/nginx.conf:/etc/nginx/nginx.conf:ro nginx
Nginx + PHP-FPM with Docker Compose
services:
nginx:
image: nginx:alpine
ports:
- "80:80"
volumes:
- ./html:/var/www/html
- ./nginx.conf:/etc/nginx/conf.d/default.conf
depends_on:
- php
php:
image: php:8.2-fpm
volumes:
- ./html:/var/www/html
Get SSL with Certbot + Nginx in Docker
docker run -it --rm -v ~/certbot/conf:/etc/letsencrypt -v ~/certbot/www:/var/www/certbot certbot/certbot certonly --webroot -w /var/www/certbot -d yourdomain.com
Reload Nginx Config (Without Restart)
docker exec webserver nginx -s reload