Skip to content

Commit e2f0a84

Browse files
committed
🔨 Fix Docker permission errors with bind mounts
Bind-mounted project dirs failed on `buildozer init` with permission denied because container UID/GID didn't match the host. Add a lean entrypoint that maps the container user to the host UID/GID execs buildozer, fixing writes to `/home/user/hostcwd`. See command used and error output: ``` docker run --volume "$(pwd)":/home/user/hostcwd kivy/buildozer init Traceback (most recent call last): File "/home/user/.venv/bin/buildozer", line 8, in <module> sys.exit(main()) ^^^^^^ File "/home/user/.venv/lib/python3.12/site-packages/buildozer/scripts/client.py", line 16, in main Buildozer().run_command(sys.argv[1:]) File "/home/user/.venv/lib/python3.12/site-packages/buildozer/__init__.py", line 672, in run_command getattr(self, cmd)(*args) File "/home/user/.venv/lib/python3.12/site-packages/buildozer/__init__.py", line 711, in cmd_init buildops.file_copy(join(dirname(__file__), 'default.spec'), 'buildozer.spec') File "/home/user/.venv/lib/python3.12/site-packages/buildozer/buildops.py", line 108, in file_copy copyfile(source, target) File "/usr/lib/python3.12/shutil.py", line 262, in copyfile with open(dst, 'wb') as fdst: ^^^^^^^^^^^^^^^ PermissionError: [Errno 13] Permission denied: 'buildozer.spec' ```
1 parent fe16e3e commit e2f0a84

File tree

4 files changed

+41
-13
lines changed

4 files changed

+41
-13
lines changed

.github/workflows/docker.yml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,9 @@ jobs:
5656
tags: ${{ env.DOCKERHUB_IMAGE }}:latest
5757
# Run the locally built image to test it
5858
- name: Docker run
59-
run: docker run ${{ env.DOCKERHUB_IMAGE }} --version
59+
run: |
60+
docker run ${{ env.DOCKERHUB_IMAGE }} --version
61+
docker run --rm --volume "$PWD":/home/user/hostcwd ${{ env.DOCKERHUB_IMAGE }} init
6062
6163
update-readme:
6264
runs-on: ubuntu-24.04

.github/workflows/test_python.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ jobs:
6868
steps:
6969
- uses: actions/checkout@v5
7070
- name: Requirements
71-
run: pip install -U sphinx sphinxawesome_theme
71+
run: pip install --upgrade sphinx sphinxawesome_theme
7272
- name: Check links
7373
run: sphinx-build -b linkcheck docs/source docs/build
7474
- name: Generate documentation

Dockerfile

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
# In order to give the container access to your current working directory
1111
# it must be mounted using the --volume option.
1212
# Run with (e.g. `buildozer --version`):
13-
# docker run \
13+
# docker run --interactive --tty --rm \
1414
# --volume "$HOME/.buildozer":/home/user/.buildozer \
1515
# --volume "$PWD":/home/user/hostcwd \
1616
# kivy/buildozer --version
@@ -70,19 +70,16 @@ RUN apt update -qq > /dev/null \
7070
zip \
7171
zlib1g-dev
7272

73-
# prepares non root env
74-
RUN useradd --create-home --shell /bin/bash ${USER}
75-
# with sudo access and no password
76-
RUN usermod -append --groups sudo ${USER}
77-
RUN echo "%sudo ALL=(ALL) NOPASSWD: ALL" >> /etc/sudoers
73+
# Create home directory and virtual environment
74+
RUN mkdir -p ${HOME_DIR} \
75+
&& python3 -m venv ${HOME_DIR}/.venv
7876

79-
USER ${USER}
8077
WORKDIR ${WORK_DIR}
81-
COPY --chown=user:user . ${SRC_DIR}
78+
COPY . ${SRC_DIR}
79+
COPY --chmod=755 entrypoint.sh /usr/local/bin/entrypoint.sh
8280

8381
# installs buildozer and dependencies from a virtual environment
8482
ENV PATH="${HOME_DIR}/.venv/bin:${PATH}"
85-
RUN python3 -m venv ${HOME_DIR}/.venv && \
86-
pip3 install --upgrade "Cython<3.0" wheel pip ${SRC_DIR}
83+
RUN pip install --upgrade "Cython<3.0" wheel pip ${SRC_DIR}
8784

88-
ENTRYPOINT ["buildozer"]
85+
ENTRYPOINT ["/usr/local/bin/entrypoint.sh"]

entrypoint.sh

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
#!/bin/bash
2+
3+
DEFAULT_USER_NAME="user"
4+
DEFAULT_USER_HOME="/home/$DEFAULT_USER_NAME"
5+
# Get the host user's UID and GID from the mounted volume
6+
HOST_UID=$(stat -c %u $DEFAULT_USER_HOME/hostcwd 2>/dev/null || echo 1000)
7+
HOST_GID=$(stat -c %g $DEFAULT_USER_HOME/hostcwd 2>/dev/null || echo 1000)
8+
9+
# Create group with host GID if it doesn't exist
10+
if ! getent group $HOST_GID > /dev/null 2>&1; then
11+
groupadd --gid $HOST_GID hostgroup
12+
fi
13+
14+
# Check if UID already exists
15+
if getent passwd $HOST_UID > /dev/null 2>&1; then
16+
# UID exists, get the existing username
17+
USER_NAME=$(getent passwd $HOST_UID | cut -d: -f1)
18+
else
19+
# UID doesn't exist, create new user
20+
USER_NAME=$DEFAULT_USER_NAME
21+
useradd --uid $HOST_UID --gid $HOST_GID --home $DEFAULT_USER_HOME --shell /bin/bash --no-create-home $DEFAULT_USER_NAME
22+
fi
23+
24+
# Ensure home directory and venv ownership
25+
chown --recursive $HOST_UID:$HOST_GID $DEFAULT_USER_HOME
26+
27+
# Switch to the user and execute buildozer
28+
BUILDOZER="$DEFAULT_USER_HOME/.venv/bin/buildozer"
29+
exec sudo --preserve-env --user "$USER_NAME" PATH="$DEFAULT_USER_HOME/.venv/bin:$PATH" -- "$BUILDOZER" "$@"

0 commit comments

Comments
 (0)