This article will detail how to build a Docker image.
Principle
- Use Hugo to build the site and generate static files.
- Use Nginx as the server to handle requests.
Dockerfile
Create a Dockerfile in the site root.
1% code/deployment-docker-dockerfile %
- There are two stages to the build, a build stage and a release stage. The release stage contains only the generated static files, keeping the size of the Docker image as small as possible.
- If you have
defaultContentLanguageInSubdirenabled, please uncomment and modify it on demand. - The
HUGO_BASEURLparameter is used to specify thebaseURLof the site during the build, and is generally used when thebaseURLis different from the configuration, so you can remove it yourself if you don’t need it. HUGO_MODULE_PROXYandNPM_CONFIG_REGISTRYare optional build parameters that can be removed if not needed.
Nginx
Create a deploy/nginx/default.conf in the site root:
1% code/deployment-docker-nginx %
Build
1% code/deployment-docker-build %
mysite: the image name.HUGO_BASEURL:thebaseURL.
For users located in China mainland, you need to specify HUGO_MODULE_PROXY and NPM_CONFIG_REGISTRY to build the image successfully and quickly:
1% code/deployment-docker-build-china %
Deployment
There are many ways to deploy Docker image, such as docker run, k8s, etc. This article will only explain docker run.
Run
1% code/deployment-docker-run %
For local testing, you need to change the
baseURLparameter or theHUGO_BASEURLbuild parameter, e.g.http://localhost:2333.
Stop
1% code/deployment-docker-stop %
Start
1% code/deployment-docker-start %
Remove
1% code/deployment-docker-remove %
Comments