First of all, I’m new to the Docker and the Hugo (I know it isn’t the best combination), but I desire to learn.
So today, when I heard for the Hugo I wanted to start creating my pages (to know/learn it) through it (theory and practice).
But perhaps for obvious reasons, I want to set up the Ηugo inside a Docker container ( for development and production )
So I created the following Dockerfile
:
# I use an official latest Debian GNU/Linux distribution as a parent image.
FROM debian:buster
ENV DEBIAN_FRONTEND noninteractive
# --------------------------------------------= System configuration. =--------------------------------------------
ARG UID
ARG GID
RUN groupadd -r container-user -g $GID && useradd -ms /bin/bash container-user -u $UID -g $GID
# ========================================= End of system configuration. =========================================
# --------------------= System requirements. =--------------------------------------------
# System update.
RUN apt-get update
# Install system packages :
RUN apt-get install -y apt-utils vim tree htop git python3 python3-pip hugo
# Install python packages :
RUN pip3 install --upgrade pip
RUN pip3 install pygments
# System update & upgrade.
RUN apt-get update && apt-get dist-upgrade -y && apt-get autoremove -y && apt-get autoclean -y && apt-get clean -y
# ========================================= End of system requirements. =========================================
# --------------------------------------------= Ports & Networking. =---------------------------------------------
# Make port 80 available to the world outside this container
EXPOSE 80 1313
# ========================================== End of ports & networking. ==========================================
USER container-user
# Set the working directory to /home/container-user/hugowebsites/
WORKDIR /home/container-user/hugowebsites/
# Copy the current directory contents (from the host) into the container at /home/container-user/hugowebsites/
COPY ./hugo_website /home/container-user/hugowebsites
# Run app when the container launches ( if I want it or not )
CMD ["tail", "-f", "/dev/null"]
## Some tests :
#CMD ["hugo", "server", "--buildDrafts", "--watch", "--bind=0.0.0.0", "--port=80"]
#CMD ["hugo", "server", "--buildDrafts", "--watch", "--bind=0.0.0.0"]
#CMD ["hugo", "server", "--buildDrafts", "--watch"]
My docker-compose.yml
file :
version: "3"
services:
hugopage:
build:
context: .
args:
UID: ${UID} # I also have a .env file for this two args,
GID: ${GID} # but I think that user trick isn't necessary.
# Anyway, I just saw it and did it. They reported how better
# communication with the user of the host and the container user is achieved.
container_name: hugo_website
ports:
- "80:80"
- "13130:1313"
volumes:
- ./hugo_website:/home/container-user/hugowebsite:delegated
As you understand, I was connected to the container with the following command:
docker exec -u container-user -it hugo_website /bin/bash
and within the /home/container-user/hugowebsites
directory, execute :
hugo new site myfirstsite
I put my theme, and trying out different things and the time has come to want to run the Hugo web server to see my page. So, I wanted to run the server through the container as I was connected to it.
So, I run the following command:
hugo server --buildDrafts --watch
Output
| EN
+------------------+----+
Pages | 15
Paginator pages | 0
Non-page files | 0
Static files | 16
Processed images | 0
Aliases | 0
Sitemaps | 1
Cleaned | 0
Total in 8 ms
Watching for changes in /home/container-user/hugowebsites/myfirstsite/{content,data,layouts,static,themes}
Watching for config changes in /home/container-user/hugowebsites/myfirstsite/config.toml
Environment: "development"
Serving pages from memory
Running in Fast Render Mode. For full rebuilds on change: hugo server --disableFastRender
Web Server is available at //localhost:1313/ (bind address 127.0.0.1)
Press Ctrl+C to stop
But unfortunately I can not see my static hugo page from the host machine.
Why ?
Only when I ran the following command I was able to see the page from host machine - outside from the container - :
hugo server --buildDrafts --watch --bind 192.168.48.2 --baseURL http://192.168.48.2
or
hugo server --buildDrafts --watch --bind $(hostname -I | awk '{print $1}') --baseURL http://$(hostname -I | awk '{print $1}'
Output
hugo server --buildDrafts --watch --bind 192.168.64.2 --baseURL http://192.168.64.2
| EN
+------------------+----+
Pages | 15
Paginator pages | 0
Non-page files | 0
Static files | 16
Processed images | 0
Aliases | 0
Sitemaps | 1
Cleaned | 0
Total in 7 ms
Watching for changes in /home/container-user/hugowebsites/myfirstsite/{content,data,layouts,static,themes}
Watching for config changes in /home/container-user/hugowebsites/myfirstsite/config.toml
Environment: "development"
Serving pages from memory
Running in Fast Render Mode. For full rebuilds on change: hugo server --disableFastRender
Web Server is available at http://192.168.64.2:1313/ (bind address 192.168.64.2)
Press Ctrl+C to stop
Why is this happening ?
Even, look at the configuration file :
$ head -n 2 config.toml
baseURL = "192.168.32.2"
languageCode = "en-us"
The setting “baseURL” isn’t important?
What role does it have?
However, I understand the baseURL setting when I run the Hugo server, but I can not understand the “bind” setting. What do we mean by this setting?
I thank the user @bep for his help :
As a second part of this topic, I would like to tell me your advice on my Dockerfile
and docker-compose.yml
files.
I use wrong the logic of the containers?
Is it wrong to run an application through the container (as I do) ?
Would it be better to have the following command at the end of the Dockerfile
file :
CMD ["hugo", "server", "--buildDrafts", "--watch", "--bind=192.168.48.2", "--baseURL=http://192.168.48.2"]
But in this case, when I want to connect with container and run hugo commands, what will I do?
I did the docker-compose.yml
file for better management and to put (mout) volumes so I can manage the (hugo) static web pages files and from my host machine .