Skip to content

Commit 61cfed5

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 61cfed5

File tree

4 files changed

+41
-12
lines changed

4 files changed

+41
-12
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: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -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: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
#!/bin/bash
2+
# Remap container user to host UID/GID (from /home/user/hostcwd) so that
3+
# buildozer can write to bind-mounted project dirs without permission errors.
4+
# Then exec buildozer as that user.
5+
6+
# Get the host user's UID and GID from the mounted volume
7+
HOST_UID=$(stat --format %u /home/user/hostcwd)
8+
HOST_GID=$(stat --format %g /home/user/hostcwd)
9+
10+
# Create group with host GID if it doesn't exist
11+
if ! getent group $HOST_GID > /dev/null 2>&1; then
12+
groupadd --gid $HOST_GID hostgroup
13+
fi
14+
15+
# Check if UID already exists
16+
if getent passwd $HOST_UID > /dev/null 2>&1; then
17+
# UID exists, get the existing username
18+
EXISTING_USER=$(getent passwd $HOST_UID | cut -d: -f1)
19+
USER_NAME="$EXISTING_USER"
20+
else
21+
# UID doesn't exist, create new user
22+
USER_NAME="user"
23+
useradd --uid $HOST_UID --gid $HOST_GID --home /home/user --shell /bin/bash --no-create-home $USER_NAME
24+
fi
25+
26+
# Ensure home directory and venv ownership
27+
chown --recursive $HOST_UID:$HOST_GID /home/user
28+
29+
# Switch to the user and execute buildozer
30+
exec $USER_NAME buildozer "$@"

0 commit comments

Comments
 (0)