From 898f0ac6c4c6cc58720e7fceb2ce6ce1a5263368 Mon Sep 17 00:00:00 2001 From: PatrickChenHZ <2540932806@qq.com> Date: Mon, 20 Jan 2025 16:51:38 -0600 Subject: [PATCH] Add an entrypoint covienience script for an out-of-the-box user experience. --- DockerBuild.sh | 4 +-- DockerEntrypoint.sh | 81 +++++++++++++++++++++++++++++++++++++++++++++ DockerRun.sh | 8 +++-- Dockerfile | 67 +++++++++++++++++++++++++++---------- 4 files changed, 138 insertions(+), 22 deletions(-) create mode 100644 DockerEntrypoint.sh diff --git a/DockerBuild.sh b/DockerBuild.sh index 3546bf2b8..a4b745e9c 100755 --- a/DockerBuild.sh +++ b/DockerBuild.sh @@ -8,8 +8,8 @@ set -x # would speed up recompile times significantly. For end users, # the simplicity of a single Docker image and a one-time compilation # seems better. -docker build -t bambustudio \ - --build-arg USER=$USER \ +docker build -t bambu_studio_yes_args \ + --build-arg USER=${USER:-root} \ --build-arg UID=$(id -u) \ --build-arg GID=$(id -g) \ $PROJECT_ROOT diff --git a/DockerEntrypoint.sh b/DockerEntrypoint.sh new file mode 100644 index 000000000..2212ce6b2 --- /dev/null +++ b/DockerEntrypoint.sh @@ -0,0 +1,81 @@ +#!/bin/bash + +# Entrypoint script to create an out-of-the-box experience for BambuStudio. +# Perform some initial setup if none was done previously. +# It is not necessary if you know what you are doing. Feel free to go +# to the Dockerfile and switch the entrypoint to the BambuStudio binary. + +# Check if the current effective user is root + +if [ "$EUID" -eq 0 ]; then + echo "No User specified at build time." + if [ -z "$RUN_USER" ] || [ -z "$RUN_UID" ] || [ -z "$RUN_GID" ] || [ "$RUN_UID" -eq 0 ]; then + echo "At least one of RUN_USER, RUN_UID, or RUN_GID is unset. Or 'root' was requested." + echo "Running as root" + + if [ "$HOME" != "/root" ]; then + if [ ! -d "/root" ]; then + mkdir /root + chown root:root /root + chmod 700 /root + fi + fi + + export HOME="/root" + EXEC_USER="root" + else + echo "Setting up a new user" + + # Check if there is a already a valid user entry for the passed UID, if not create one + if [ -z "$(getent passwd "$RUN_UID" | cut -d: -f1)" ]; then + #GID=$(id -g) + echo "User specified at runtime. Performing setup." + groupadd -g "$RUN_GID" "$RUN_USER" + useradd -u "$RUN_UID" -g "$RUN_GID" -d "/home/$RUN_USER" "$RUN_USER" + usermod -aG sudo "$RUN_USER" + passwd -d "$RUN_USER" + + #This will take forever to run, so we will just chown the build folder which contains the binaries + #chown -R "$RUN_UID":"$RUN_GID" /BambuStudio + chown "$RUN_UID":"$RUN_GID" /BambuStudio + chown -R "$RUN_UID":"$RUN_GID" /BambuStudio/build + + + export HOME="/home/$RUN_USER" + EXEC_USER="$RUN_USER" + fi + fi +else + echo "User specified at build time." + CURRENT_USER=$(id -un) + if [ -n "$RUN_USER" ] && [ -n "$RUN_UID" ] && [ -n "$RUN_GID" ] && [ "$RUN_UID" -ne "$EUID" ]; then + echo "New User config passed at Runtime. Setting up." + if [ -z "$(getent passwd "$RUN_UID" | cut -d: -f1)" ]; then + sudo groupadd -g "$RUN_UID" "$RUN_USER" + sudo useradd -u "$RUN_UID" -g "$RUN_GID" -d "/home/$RUN_USER" "$RUN_USER" + sudo usermod -aG sudo "$RUN_USER" + passwd -d "$RUN_USER" + + #sudo chown -R "$RUN_UID":"$RUN_GID" /BambuStudio + chown "$RUN_UID":"$RUN_GID" /BambuStudio + chown -R "$RUN_UID":"$RUN_GID" /BambuStudio/build + + export HOME="/home/$RUN_USER" + EXEC_USER="$RUN_USER" + fi + else + echo "Using Build time user." + EXEC_USER="$CURRENT_USER" + #It should've been set in Dockerfile, but just in case, uncomment this it there is problem + #export HOME="/home/$USER" + fi +fi + +# make sure ~/.config folder exists so Bambu Studio will start +if [ ! -d "$HOME/.config" ]; then + mkdir -p "$HOME/.config" +fi + +# Using su $USER -c will retain all the important ENV args when Bamboo Studio starts in a different shell +# Continue with Bambu Studio using correct user, passing all arguments +exec su "$EXEC_USER" -c "/BambuStudio/build/package/bin/bambu-studio $*" diff --git a/DockerRun.sh b/DockerRun.sh index 212d9d9e6..5a6225925 100755 --- a/DockerRun.sh +++ b/DockerRun.sh @@ -6,7 +6,7 @@ set -x # If there's problems with the X display, try this # -v /tmp/.X11-unix:/tmp/.X11-unix \ # or -# -v $HOME/.Xauthority:/root/.Xauthority +# -v $HOME/.Xauthority:/root/.Xauthority \ # You also need to run "xhost +" on your host system # Bambu Studio also require the parent directory for the configuration directory to be present to start # which means it is important to make sure user is passed to container correctly @@ -23,7 +23,11 @@ docker run \ `# Run as your workstations username to keep permissions the same` \ -u $USER \ `# Bind mount your home directory into the container for loading/saving files` \ - -v $HOME:/home/$USER \ + -v $HOME:$HOME \ + -v $HOME/.Xauthority:/tmp/.Xauthority \ + -v /tmp/.X11-unix:/tmp/.X11-unix \ + -e XAUTHORITY=/tmp/.Xauthority \ + -u $(id -u ${USER}):$(id -g ${USER}) \ `# Pass the X display number to the container` \ -e DISPLAY=$DISPLAY \ `# It seems that libGL and dbus things need privileged mode` \ diff --git a/Dockerfile b/Dockerfile index b4118ebc8..878d213f2 100644 --- a/Dockerfile +++ b/Dockerfile @@ -60,12 +60,53 @@ ENV SSL_CERT_FILE=/etc/ssl/certs/ca-certificates.crt COPY ./ /BambuStudio +RUN chmod +x /BambuStudio/DockerEntrypoint.sh + WORKDIR /BambuStudio -# These can run together, but we run them seperate for podman caching -# Update System dependencies +# Ubuntu 24 Docker Image now come with default standard user "ubuntu" +# It might conflict with your mapped user, remove if user ubuntu exist +RUN if id "ubuntu" >/dev/null 2>&1; then userdel -r ubuntu; fi + +# It's easier to run Bambu Studio as the same username, +# UID and GID as your workstation. Since we bind mount +# your home directory into the container, it's handy +# to keep permissions the same. Just in case, defaults +# are root. + +# Set ARG values +# If user was passed from build it will create a user same +# as your workstation. Else it will use /root + +# Setting ARG at build time is convienient for testing purposes +# otherwise the same commands will be executed at runtime + +ARG USER=root +ARG UID=0 +ARG GID=0 +RUN if [ "$UID" != "0" ]; then \ + groupadd -g $GID $USER && \ + useradd -u $UID -g $GID -m -d /home/$USER $USER && \ + mkdir -p /home/$USER && \ + chown -R $UID:$GID /BambuStudio && \ + usermod -aG sudo $USER && \ + passwd -d "$USER"; \ + else \ + mkdir -p /root/.config; \ + fi + +# Allow password-less sudo for ALL users +RUN echo "ALL ALL=(ALL) NOPASSWD:ALL" > /etc/sudoers.d/999-passwordless +RUN chmod 440 /etc/sudoers.d/999-passwordless + +# Update System dependencies(Run before user switch) RUN ./BuildLinux.sh -u +# Run as the mapped user (or root by default) +USER $USER + + +# These can run together, but we run them seperate for podman caching # Build dependencies in ./deps RUN ./BuildLinux.sh -d @@ -76,27 +117,17 @@ RUN ./BuildLinux.sh -s ENV container=podman RUN ./BuildLinux.sh -i -# It's easier to run Bambu Studio as the same username, -# UID and GID as your workstation. Since we bind mount -# your home directory into the container, it's handy -# to keep permissions the same. Just in case, defaults -# are root. + # Use bash as the shell SHELL ["/bin/bash", "-l", "-c"] -# Set ARG values -ARG USER=root -ARG UID=0 -ARG GID=0 - -RUN if [ "$UID" != "0" ]; then \ - groupadd -f -g $GID $USER && \ - useradd --non-unique -u $UID -g $GID $USER; \ - fi - # Point FFMPEG Library search to the binary built upon BambuStudio build time ENV LD_LIBRARY_PATH=/BambuStudio/build/package/bin # Using an entrypoint instead of CMD because the binary # accepts several command line arguments. -ENTRYPOINT ["/BambuStudio/build/package/bin/bambu-studio"] +# entrypoint script will pass all arguments to bambu-studio +# after the script finishes + +#ENTRYPOINT ["/BambuStudio/build/package/bin/bambu-studio"] +ENTRYPOINT ["/BambuStudio/DockerEntrypoint.sh"]