Skip to content

Commit

Permalink
Fix to permissions in docker container
Browse files Browse the repository at this point in the history
  • Loading branch information
semohr committed Nov 7, 2024
1 parent f62b53e commit 84e85dd
Show file tree
Hide file tree
Showing 8 changed files with 46 additions and 37 deletions.
2 changes: 1 addition & 1 deletion .dockerignore
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,4 @@

!README.md
!LICENSE
!entrypoint.sh
!entrypoint*.sh
4 changes: 3 additions & 1 deletion .github/workflows/docker_hub.yml
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
name: ci
name: docker-hub

on:
push:
tags:
- "v*.*.*"
- "test-*"
workflow_dispatch:

jobs:
docker:
runs-on: ubuntu-latest
Expand Down
19 changes: 9 additions & 10 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,8 @@ FROM python:3.11-alpine AS base

FROM base AS deps

ARG USER_ID
ARG GROUP_ID
ENV USER_ID=$USER_ID
ENV GROUP_ID=$GROUP_ID
RUN addgroup -g $GROUP_ID beetle && adduser -D -u $USER_ID -G beetle beetle
RUN addgroup -g 1000 beetle && \
adduser -D -u 1000 -G beetle beetle

# map beets directory and our configs to /config
RUN mkdir -p /config/beets
Expand All @@ -31,7 +28,8 @@ RUN --mount=type=cache,target=/var/cache/apk \
bash \
keyfinder-cli \
npm \
tmux
tmux \
shadow

# Install our package (backend)
COPY ./backend /repo/backend
Expand All @@ -53,7 +51,7 @@ ENV IB_SERVER_CONFIG="dev_docker"

# relies on mounting this volume
WORKDIR /repo
USER beetle
USER root
ENTRYPOINT ["./entrypoint_dev.sh"]

# ------------------------------------------------------------------------------------ #
Expand All @@ -66,7 +64,7 @@ WORKDIR /repo
COPY --from=deps --chown=beetle:beetle /repo /repo
COPY entrypoint_test.sh .
ENV IB_SERVER_CONFIG="test"
USER beetle
USER root
ENTRYPOINT ["./entrypoint_test.sh"]

# ------------------------------------------------------------------------------------ #
Expand Down Expand Up @@ -97,7 +95,8 @@ WORKDIR /repo
COPY --from=deps /repo /repo
COPY --from=build /repo/frontend/dist /repo/frontend/dist
COPY entrypoint.sh .
COPY entrypoint_fix_permissions.sh .
RUN chown -R beetle:beetle /repo

USER beetle
ENTRYPOINT ["./entrypoint.sh"]
USER root
ENTRYPOINT ["/bin/sh", "-c", "./entrypoint_fix_permissions.sh && su beetle -c ./entrypoint.sh"]
7 changes: 3 additions & 4 deletions docker-compose-dev.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,15 @@ services:
context: .
dockerfile: Dockerfile
target: dev
args:
# 502 is default on macos, 1000 on linux
USER_ID: 1000
GROUP_ID: 1000
image: beets-flask
restart: unless-stopped
ports:
- "5001:5001" # production and backend
- "5173:5173" # for vite dev server
environment:
# 502 is default on macos, 1000 on linux
USER_ID: 1000
GROUP_ID: 1000
LOG_LEVEL_BEETSFLASK: DEBUG # this is used for our own logs. (set beets level via the config)
LOG_LEVEL_OTHERS: WARNING # this is passed python logging basic config (all other modules)
volumes:
Expand Down
34 changes: 17 additions & 17 deletions docker-compose-tests.yaml
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
services:
beets-flask-tests:
container_name: beets-flask-tests
hostname: beets-container
build:
context: .
dockerfile: Dockerfile
target: test
args:
# 502 is default on macos, 1000 on linux
USER_ID: 502
GROUP_ID: 502
image: beets-flask-tests
ports:
- "5001:5001"
- "5173:5173"
volumes:
- ./:/repo/
beets-flask-tests:
container_name: beets-flask-tests
hostname: beets-container
build:
context: .
dockerfile: Dockerfile
target: test
image: beets-flask-tests
ports:
- "5001:5001"
- "5173:5173"
environment:
# 502 is default on macos, 1000 on linux
USER_ID: 502
GROUP_ID: 502
volumes:
- ./:/repo/
7 changes: 3 additions & 4 deletions docker-compose.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,14 @@ services:
context: .
dockerfile: Dockerfile
target: prod
args:
# 502 is default on macos, 1000 on linux
USER_ID: 1000
GROUP_ID: 1000
image: beets-flask
restart: unless-stopped
ports:
- "5001:5001" # production and backend
environment:
# 502 is default on macos, 1000 on linux
USER_ID: 1000
GROUP_ID: 1000
LOG_LEVEL_BEETSFLASK: INFO # this is used for our own logs. (set beets level via the config)
LOG_LEVEL_OTHERS: WARNING # this is passed python logging basic config (all other modules)
volumes:
Expand Down
1 change: 1 addition & 0 deletions entrypoint.sh
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#!/bin/sh

echo "Running as"
id

Expand Down
9 changes: 9 additions & 0 deletions entrypoint_fix_permissions.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#!/bin/sh

if [ ! -z "$USER_ID" ] && [ ! -z "$GROUP_ID" ]; then
echo "Updating UID to $USER_ID and GID to $GROUP_ID"
groupmod -g $GROUP_ID beetle
usermod -u $USER_ID -g $GROUP_ID beetle
chown -R beetle:beetle /home/beetle
chown -R beetle:beetle /repo
fi

0 comments on commit 84e85dd

Please sign in to comment.