Skip to content

Instantly share code, notes, and snippets.

@samarpanda
Last active August 5, 2023 07:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save samarpanda/af0539f206c18c5cd979b6fff3dc5a4f to your computer and use it in GitHub Desktop.
Save samarpanda/af0539f206c18c5cd979b6fff3dc5a4f to your computer and use it in GitHub Desktop.
CNAME='nginx-8080'
docker stop --time=20 $CNAME
docker rm -f $CNAME
<html>
<h1>WOW Works!</h1>
</html>
#!/bin/bash
# By Samar Panda <https://samarpanda.com/>
# Name of the container
CNAME='nginx-8080'
# Stop the container if running
docker stop --time=20 $CNAME
# Remove the container
docker rm -f $CNAME
# Mounting folder build in the same directory
docker run -itd --name $CNAME --hostname $CNAME -v "$(pwd)"/build:/usr/share/nginx/html -p 8080:80 nginx:1.25-alpine

Static file serving using Nginx + Docker

Quick file serving with lightweight & efficient webserver using nginx:1.25-alpine

Quick project aims to demonstrate the creation, deployment of a lightweight & efficient web server using Nginx running on an Apline Linux based Docker container. The project serve static files from any mounted folder, making an ideal solution for hosting simple website, front-end applications, or any content that needs to be served from a folder / local system.

nginx-serve.sh

  1. Container name CNAME=nginx-8080
  2. Nginx image is used from dockerhub
  3. build folder in current directory is mounted -v "$(pwd)"/build:/usr/share/nginx/html
  4. Any changes in the local folder build will automatically be updated & served by nginx

Usage commands

  1. Create a folder build by mkdir build
  2. Create index.html in the o
  3. Start the nginx container sh nginx-serve.sh
  4. Stop & remove the container sh clean.sh
  5. Login into the container docker exec -it nginx-8080 /bin/sh
  6. Try opening http://localhost:8080 in browser

clean.sh

  1. Stop and remove the container
  2. You can also remove the image docker rm -f nginx:1.25-alpine

By Samar Panda

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment