How to run Nginx with Docker and Docker Compose
Deploy an Nginx web server as a Docker container using docker run and Docker Compose with custom configuration.
Deploy an Nginxweb server inside a Docker container using
docker runfor quick testing or Docker Compose for production-like multi-service setups.
Step-by-Step: Run Nginx with Docker
1. Run Nginx with docker run
docker run -d -p 80:80 --name webserver nginx:latestDocker pulls the official Nginx image, creates a container, and maps host port 80 to container port 80. The
-d flag runs the container in detached (background) mode.
2. Run Nginx with Docker Compose
Create a
docker-compose.yml file:
services:
web:
image: nginx:latest
ports:
- "80:80"
- "443:443"
volumes:
- ./nginx.conf:/etc/nginx/nginx.conf:ro
- ./html:/usr/share/nginx/html:ro
restart: unless-stoppedStart the service:
docker compose up -d3. Verify Nginx Is Running
curl -I http://localhostCommon Issues
Port 80 is already in use— Another process (host Nginx, Apache) occupies port 80. Stop the conflicting service or map to a different host port:
-p 8080:80. See
Docker: port is already allocated.