Install Hugo into a Docker Container

Hugo

This article explains the steps to install Hugo into a docker container. We first create a docker container and install docker into it.

Set up a folder

First, we set up a folder that we will share with the Docker container. In that way, we can edit files locally on the host computer but also access them within the container.

mkdir -p /data/hugo1

Create a new Docker container.

Next, we need to create a new Docker container.

docker run --name hugo1 -v /data/hugo1:/home/data -d -p 1234:1234 -it ubuntu:latest
  • -v /data/hugo1:/home/data: Mounts the local /data/hugo1 folder of the host system with /home/data within the container.
  • -p 1234:1234: Port forwarding of port 1234. This is the port that Hugo uses for its built-in test server.
  • -it … /bin/bash will interactively attach to the bash shell within the container.
  • -d is for detaching the container. If you don’t use -d, docker will stop once you exit if no jobs are running.
  • ubuntu:latest is the image I use for this job.
  • –name hugo1 sets the name of the container to hugo1

[root@server ~]# docker ps -a | grep hugo1
11bb85b62a10   ubuntu:latest   "bash"    About a minute ago   Exited (0) About a minute ago             hugo1

Enter into the container



docker exec -it hugo1  /bin/bash

Install Hugo


cd /home/data/

# install utils that we'll need:
apt-get update -y;
apt-get install curl git -y;

# download deb file
curl -L https://github.com/gohugoio/hugo/releases/download/v0.91.2/hugo_0.91.2_Linux-64bit.deb -o hugo.deb;

# install
apt install ./hugo.deb;

# verify that it worked
hugo version

Create a site



hugo new site quickstart

cd quickstart
git init
git submodule add https://github.com/budparr/gohugo-theme-ananke.git themes/ananke

echo 'theme = "ananke"' >> config.toml

hugo new posts/my-first-post.md

It is best to install ‘screen’ and get in at this point.


apt install screen -y;
screen

Open in browser

Before we can access the site, we need to start Hugo’s built-in web-server inside the container:

hugo server --bind 0.0.0.0 -D

Don’t forget to add –bind 0.0.0.0 here. Otherwise, the server cannot be accessed from the host system, but only from within the Docker container. Open http://IP:1234/ in your browser and you should see the Hugo page.

Restarting the Container

When you restart the container hugo won’t start automatically.

You need to start hugo again.

To attach to bash again, run:



docker exec -it hugo1  /bin/bash

Now start hugo.

screen
cd /home/data/quickstart/
hugo server --bind 0.0.0.0 -D

Here, we discussed the steps to install Hugo into a docker container. We first created a docker container and installed docker into it.

Here is the official website for Hugo.

You can learn how to install docker in CentOS 7 using this link.

Tags:,