Docker - Setting NGINX
Docker - Setting up NGINX
Section titled “Docker - Setting up NGINX”NGINX is a popular, high-performance web server, reverse proxy, load balancer, and cache. Docker makes it incredibly easy to run Nginx instances for serving static content, proxying requests to backend applications, or testing configurations.
Running a Basic Nginx Container
Section titled “Running a Basic Nginx Container”Step 1: Pull the official Nginx image. Using an Alpine-based image is often preferred for its small size.
# Pull the stable Nginx image based on Alpine Linuxdocker pull nginx:stable-alpineStep 2: Run the Nginx container, mapping port 80 (default HTTP port) inside the container to a port on your host machine (e.g., port 8080).
# Run Nginx detached (-d), name it 'my-nginx-server'# Map host port 8080 to container port 80 (-p)docker run -d --name my-nginx-server -p 8080:80 nginx:stable-alpineStep 3: Test the server. Open your web browser and navigate to http://localhost:8080 (or http://<your-docker-host-ip>:8080).
You should see the default Nginx welcome page: “Welcome to nginx!” This confirms the container is running and serving content.
Serving Custom Static Content
Section titled “Serving Custom Static Content”You can easily serve your own HTML, CSS, and JavaScript files by mounting a local directory containing your content into the default Nginx web root directory (/usr/share/nginx/html) inside the container.
Step 4: Create a local directory (e.g., my-static-content) and place an index.html file inside it.
# Example ./my-static-content/index.html<!DOCTYPE html><html><head><title>My Static Site</title></head><body><h1>Hello from my custom content!</h1></body></html>Step 5: Run the Nginx container, mounting your local directory.
# Stop and remove the previous container if it's still running# docker stop my-nginx-server && docker rm my-nginx-server
# Run Nginx, mounting local './my-static-content' to container's web root# Map host port 8081 to container port 80docker run -d --name my-custom-nginx -p 8081:80 \ -v "${PWD}/my-static-content":/usr/share/nginx/html:ro \ nginx:stable-alpineExplanation:
-v "${PWD}/my-static-content":/usr/share/nginx/html:ro: Mounts themy-static-contentdirectory from your current host working directory (${PWD}) into the/usr/share/nginx/htmldirectory inside the container.:ro: Mounts the volume in read-only mode, which is good practice for serving static content – the container cannot modify your host files.
Step 6: Test again. Navigate to http://localhost:8081 in your browser. You should now see the content from your custom index.html file.
Using Custom Nginx Configuration
Section titled “Using Custom Nginx Configuration”For more complex setups (like reverse proxying or custom server blocks), you can mount a custom Nginx configuration file.
Step 7: Create a local configuration file (e.g., my-nginx.conf).
# Example my-nginx.conf (very basic)server { listen 80; server_name localhost;
location / { root /usr/share/nginx/html; index index.html index.htm; }
location /api/ { # Example reverse proxy configuration # proxy_pass http://backend-service:3000; }}Step 8: Run the Nginx container, mounting your custom configuration file over the default one (/etc/nginx/conf.d/default.conf).
docker run -d --name my-configured-nginx -p 8082:80 \ -v "${PWD}/my-static-content":/usr/share/nginx/html:ro \ -v "${PWD}/my-nginx.conf":/etc/nginx/conf.d/default.conf:ro \ nginx:stable-alpineNow Nginx inside the container will use your my-nginx.conf settings. This allows you to define custom server blocks, proxy rules, SSL configurations, etc., without needing to build a custom Nginx image for every configuration change (though building an image with the config baked in is also a valid approach).