-
Notifications
You must be signed in to change notification settings - Fork 3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Implement run-hooks as a separate script #1979
Implement run-hooks as a separate script #1979
Conversation
@benz0li take a look, please. |
@mathbunnyru Looks good! Tested (on macOS using Docker Desktop) with a local build including your modifications: Create home directorydocker run --rm \
-v "${PWD}/jupyterlab-benz0li":/dummy \
alpine chown 1000:100 /dummy Run containerdocker run -it --rm \
-p 8888:8888 \
-u root \
-v "${PWD}/jupyterlab-benz0li":/home/benz0li \
-e NB_USER=benz0li \
-e NB_UID=$(id -u) \
-e NB_GID=$(id -g) \
-e CHOWN_HOME=yes \
-e CHOWN_HOME_OPTS='-R' \
jupyterlab/qgis/base:3.32.2
|
@benz0li thanks 👍 I think it would be nice if your images somehow were able to update scripts like this, so we have fewer differences and these differences are easier to find. I don't know if it's easy for you or not though. |
Very few additional modifications to yours: diff --git a/base/scripts/usr/local/bin/start.sh.orig b/base/scripts/usr/local/bin/start.sh
index d8b97bd..eb62fc7 100755
--- a/base/scripts/usr/local/bin/start.sh.orig
+++ b/base/scripts/usr/local/bin/start.sh
@@ -53,6 +53,8 @@ if [ "$(id -u)" == 0 ] ; then
# - CHOWN_HOME: a boolean ("1" or "yes") to chown the user's home folder
# - CHOWN_EXTRA: a comma separated list of paths to chown
# - CHOWN_HOME_OPTS / CHOWN_EXTRA_OPTS: arguments to the chown commands
+ # - CHMOD_HOME: a boolean ("1" or "yes") to chmod the user's home folder
+ # - CHMOD_HOME_MODE: mode argument to the chmod command
# Refit the jovyan user to the desired the user (NB_USER)
if id jovyan &> /dev/null ; then
@@ -75,7 +77,7 @@ if [ "$(id -u)" == 0 ] ; then
fi
# Recreate the desired user as we want it
userdel "${NB_USER}"
- useradd --no-log-init --home "/home/${NB_USER}" --uid "${NB_UID}" --gid "${NB_GID}" --groups 100 "${NB_USER}"
+ useradd --no-log-init --home "/home/${NB_USER}" --shell "$(which zsh)" --uid "${NB_UID}" --gid "${NB_GID}" --groups 100 "${NB_USER}"
fi
# Move or symlink the jovyan home directory to the desired users home
@@ -97,12 +99,22 @@ if [ "$(id -u)" == 0 ] ; then
exit 1
fi
fi
+ # The home directory could be bind mounted. Populate it if it is empty
+ elif [[ "$(ls -A "/home/${NB_USER}" 2> /dev/null)" == "" ]]; then
+ _log "Populating home dir /home/${NB_USER}..."
+ if cp -a /home/jovyan/. "/home/${NB_USER}/"; then
+ _log "Success!"
+ else
+ _log "Failed to copy data from /home/jovyan to /home/${NB_USER}!"
+ exit 1
+ fi
fi
# Ensure the current working directory is updated to the new path
if [[ "${PWD}/" == "/home/jovyan/"* ]]; then
new_wd="/home/${NB_USER}/${PWD:13}"
_log "Changing working directory to ${new_wd}"
cd "${new_wd}"
+ export CODE_WORKDIR=/home/${NB_USER}/projects
fi
fi
@@ -120,6 +132,10 @@ if [ "$(id -u)" == 0 ] ; then
chown ${CHOWN_EXTRA_OPTS} "${NB_UID}:${NB_GID}" "${extra_dir}"
done
fi
+ # Optionally change the mode of the user's home folder
+ if [[ "${CHMOD_HOME}" == "1" || "${CHMOD_HOME}" == "yes" ]]; then
+ chmod "${CHMOD_HOME_MODE:-755}" "/home/${NB_USER}"
+ fi
# Update potentially outdated environment variables since image build
export XDG_CACHE_HOME="/home/${NB_USER}/.cache"
@@ -140,6 +156,7 @@ if [ "$(id -u)" == 0 ] ; then
unset_explicit_env_vars
_log "Running as ${NB_USER}:" "${cmd[@]}"
exec sudo --preserve-env --set-home --user "${NB_USER}" \
+ LD_LIBRARY_PATH="${LD_LIBRARY_PATH:-}" \
PATH="${PATH}" \
PYTHONPATH="${PYTHONPATH:-}" \
"${cmd[@]}"
@@ -196,7 +213,7 @@ else
# We cannot use "sed --in-place" since sed tries to create a temp file in
# /etc/ and we may not have write access. Apply sed on our own temp file:
sed --expression="s/^jovyan:/nayvoj:/" /etc/passwd > /tmp/passwd
- echo "${NB_USER}:x:$(id -u):$(id -g):,,,:/home/jovyan:/bin/bash" >> /tmp/passwd
+ echo "${NB_USER}:x:$(id -u):$(id -g):,,,:/home/jovyan:$(which zsh)" >> /tmp/passwd
cat /tmp/passwd > /etc/passwd
rm /tmp/passwd
|
I think the only part1 that is relevant comes after I have no intention of syncing any files or folders with a pre-populated directory between the host and container. Footnotes
|
And the |
P.S.: I use a similar approach for my Data Science Dev Containers1: https://github.com/b-data/data-science-devcontainers/blob/main/.devcontainer/scripts/usr/local/bin/onCreateCommand.sh Footnotes |
I think this PR is ready to go, so I will merge it. @benz0li I have a few questions though if you don't mind:
And thank you for :
|
This is more of a personal choice. My point of view: Only the original content [from the image] ensures that the container works as intended.
ℹ️ This way
– Volumes | Docker Docs > [...] > Populate a volume using a container 👉 This happens only at the first run and only if the volume is empty. |
@mathbunnyru |
Very simple, entirely sane and the outcome easily predictable. I might extend to # The home directory could be bind mounted. Populate it if it is empty
elif [[ "$(ls -A "/home/${NB_USER}" 2> /dev/null)" == "" ]]; then
_log "Populating home dir /home/${NB_USER}..."
if cp -a /home/jovyan/. "/home/${NB_USER}/"; then
_log "Success!"
else
_log "ERROR: Failed to copy data from /home/jovyan to /home/${NB_USER}!"
_log " The home directory must be empty at the first run!"
exit 1
fi
fi i.e. improving the error message on failure. Also for |
Cross reference: b-data/jupyterlab-python-docker-stack#1 |
Describe your changes
Issue ticket if applicable
Work on: #1478
Checklist (especially for first-time contributors)