Skip to content
This repository was archived by the owner on Jun 24, 2022. It is now read-only.

Commit 91f8eca

Browse files
Create symlinker function from conf.d/ to user_conf.d/
In issue [#33][1] there was a request for having the container add the configuration files at runtime, instead of having to build from your own Dockerfile. I was first sceptical of this, since this would deviate quite a bit from the original setup of this image, but I think I have found a method that is sort of backwards compatible while still allowing people to just mount a local folder and have it work. This will also allow us to update the config files currently in nginx_conf.d, and have them propagate to users mounting folders into the container. [1]: JonasAlfredsson/docker-nginx-certbot#33
1 parent 68b2dcf commit 91f8eca

File tree

3 files changed

+41
-0
lines changed

3 files changed

+41
-0
lines changed

src/Dockerfile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ RUN set -ex && \
4747
# Create new directories and set correct permissions for them.
4848
mkdir -p /var/www/letsencrypt && \
4949
chown www-data:www-data -R /var/www && \
50+
mkdir -p /etc/nginx/user_conf.d && \
5051
mkdir -p /etc/letsencrypt/dhparams && \
5152
chmod 700 /etc/letsencrypt/dhparams \
5253
&& \

src/scripts/start_nginx_certbot.sh

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,9 @@ fi
5151
(
5252
set -e
5353
while [ true ]; do
54+
# Create symlinks from conf.d/ to user_conf.d/ if necessary.
55+
symlink_user_configs
56+
5457
# Check that all dhparam files exists.
5558
"$(cd "$(dirname "$0")"; pwd)/create_dhparams.sh"
5659

src/scripts/util.sh

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,43 @@ allfiles_exist() {
117117
return ${all_exist}
118118
}
119119

120+
# Creates symlinks from /etc/nginx/conf.d/ to all the files found inside
121+
# /etc/nginx/user_conf.d/. This will also remove broken links.
122+
symlink_user_configs() {
123+
debug "Creating symlinks to any files found in /etc/nginx/user_conf.d/"
124+
125+
# Remove any broken symlinks that point back to the user_conf.d/ folder.
126+
while IFS= read -r -d $'\0' symlink; do
127+
info "Removing broken symlink '${symlink}' to '$(realpath "${symlink}")'"
128+
rm "${symlink}"
129+
done < <(find /etc/nginx/conf.d/ -maxdepth 1 -xtype l -lname '/etc/nginx/user_conf.d/*' -print0)
130+
131+
# Go through all files and directories in the user_conf.d/ folder and create
132+
# a symlink to them inside the conf.d/ folder.
133+
while IFS= read -r -d $'\0' source_file; do
134+
local symlinks_found=0
135+
136+
# See if there already exist a symlink to this source file.
137+
while IFS= read -r -d $'\0' symlink; do
138+
debug "The file '${source_file}' is already symlinked by '${symlink}'"
139+
symlinks_found=$((${symlinks_found} + 1))
140+
done < <(find -L /etc/nginx/conf.d/ -maxdepth 1 -samefile "${source_file}" -print0)
141+
142+
if [ "${symlinks_found}" -eq "1" ]; then
143+
# One symlink found, then we have nothing more to do.
144+
continue
145+
elif [ "${symlinks_found}" -gt "1" ]; then
146+
warning "Found more than one symlink to the file '${source_file}' inside '/etc/nginx/conf.d/'"
147+
continue
148+
fi
149+
150+
# No symlinks to this file found, lets create one.
151+
local link="/etc/nginx/conf.d/$(basename -- ${source_file})"
152+
info "Creating symlink '${link}' to '${source_file}'"
153+
ln -s "${source_file}" "${link}"
154+
done < <(find /etc/nginx/user_conf.d/ -maxdepth 1 -type f -print0)
155+
}
156+
120157
# Helper function that sifts through /etc/nginx/conf.d/, looking for configs
121158
# that don't have their necessary files yet, and disables them until everything
122159
# has been set up correctly. This also activates them afterwards.

0 commit comments

Comments
 (0)