Hugo : how to run my static site in a Docker container
Overview
Using Hugo for several years now and hosting it on Netlify and really happy of the result, I ve been wondering if I could create autoamte the creation of a docker container to publish the static website in a nginx image. So here are my findings !
Using Docker to build the image
Create a dockerfile file in the root of the hugo project. The file will be use 2 images :
- a temporary ‘build’ image will run the hugo engine to create the static content.
- a ‘base’ image will host the nginx engine and the static website. This image will be the final result hosted in our Docker system.
|
|
Isn’t this funny that we only have one variable in this script which is the url of the website. This url must be the url used when accessing the website. Thus in my case as I m going through CloudFlare, I should use ‘ https://myproject.mycloudflare.com ’ .
Run the script to build the image and push it to the local Docker. I m using this convention to name my images and containers :
- hugo-{projectname} : for all images hosting a hugo static site.
- website-{projectname} : for all websites published and running in docker.
|
|
At this stage the Image is created and ready in Docker. Now I m running the container.
|
|
When I am happy with the result on my local machine, I just stop and remove the container.
|
|
While creating the scripts I have found some docker images related to Hugo, but I’ve decided to build mine from scratch. There is no real motivation in my choice, it was really just for the exercise.
Conclusion
After creating a few dockerfile and understanding how it is working, it s really becoming easy to automate the creation of the containers. The image is quite small (55Mb) and working pretty good.