In this blog, I will go through the steps of building a Docker container image hosting static site content using Nginx server. This container can be used behind any reverse proxy server in self-hosted environment or cloud platforms to serve the static site.

If you are new to Docker, please go through the offical docker installtion guide to install Docker on your local environment. In this example, we will use MacOS, but you can use another operating systems that docker supports such as Windows or Linux.

Once you have docker setup, and suppose all your static site content are located at directory site, run below command to build the docker image.

docker run -d -p 8007:80 -v ${PWD}/site/:/usr/share/nginx/html nginx:alpine

In the above command options:

  • The -d tells the docker run command to run in detached mode
  • The -p bind container’s port 80 to host port 8007
  • The -v mounts the site content from host machine to the container so nginx can access it

Now, open a web browser and visit localhost:8007, you should be able to see your static web site is up and running.

Cannot see the page loading ? Need trouble-shooting. You can start the container in real-time mode and inspect the logs:

docker run -p 8007:80 -v ${PWD}/site/:/usr/share/nginx/html nginx:alpine

If nginx logs shows access denied (403 Forbidden), this could mean the site directory’s current permission setting making it unable to be accessed by the container. Try run this command to grant container access:

chmod -R 755 site