From 3aee5e8d78e3582c8326b4f3c8f8dd32b40e5b58 Mon Sep 17 00:00:00 2001 From: Michael Mayer Date: Fri, 15 Jan 2021 15:02:06 +0100 Subject: [PATCH] Docker: Use variables to set UID & GID in development environments --- Dockerfile | 2 +- Makefile | 3 ++- docker-compose.yml | 4 ++-- docker/development/Dockerfile | 8 ++++++-- docker/development/entrypoint.sh | 26 ++++++++++++++++++++++++++ docker/photoprism/Dockerfile | 2 +- 6 files changed, 38 insertions(+), 7 deletions(-) create mode 100755 docker/development/entrypoint.sh diff --git a/Dockerfile b/Dockerfile index 57b293497..67ffd27be 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,4 +1,4 @@ -FROM photoprism/development:20201215 +FROM photoprism/development:20210115 # Set up project directory WORKDIR "/go/src/github.com/photoprism/photoprism" diff --git a/Makefile b/Makefile index bb2e8f823..37f3a6ed5 100644 --- a/Makefile +++ b/Makefile @@ -9,6 +9,7 @@ export GO111MODULE=on GOIMPORTS=goimports BINARY_NAME=photoprism DOCKER_TAG=`date -u +%Y%m%d` +UID=`(id -u)` HASRICHGO := $(shell which richgo) @@ -51,7 +52,7 @@ start: stop: go run cmd/photoprism/photoprism.go stop terminal: - docker-compose exec photoprism bash + docker-compose exec -u $(UID) photoprism bash root-terminal: docker-compose exec -u root photoprism bash migrate: diff --git a/docker-compose.yml b/docker-compose.yml index bab183828..5f6af667b 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -20,8 +20,6 @@ services: security_opt: - seccomp:unconfined - apparmor:unconfined - # Uncomment and edit the following line to set a specific user / group id: - # user: "1000:1000" depends_on: - photoprism-db ports: @@ -32,6 +30,8 @@ services: - "go-mod:/go/pkg/mod" shm_size: "2gb" environment: + UID: ${UID:-1000} + GID: ${GID:-1000} TF_CPP_MIN_LOG_LEVEL: 0 # Show TensorFlow log messages for development PHOTOPRISM_SITE_URL: "http://localhost:2342/" PHOTOPRISM_SITE_TITLE: "PhotoPrism" diff --git a/docker/development/Dockerfile b/docker/development/Dockerfile index 298a65493..c6f427696 100644 --- a/docker/development/Dockerfile +++ b/docker/development/Dockerfile @@ -154,5 +154,9 @@ WORKDIR "/go/src/github.com/photoprism/photoprism" EXPOSE 2342 2343 9515 VOLUME /var/lib/photoprism -# Keep container running (services can be started manually using a terminal) -CMD tail -f /dev/null +# Configure entrypoint +COPY --chown=root:root /docker/development/entrypoint.sh /entrypoint.sh +ENTRYPOINT ["/entrypoint.sh"] + +# Run server +CMD ["tail", "-f", "/dev/null"] \ No newline at end of file diff --git a/docker/development/entrypoint.sh b/docker/development/entrypoint.sh new file mode 100755 index 000000000..50b4c8a3d --- /dev/null +++ b/docker/development/entrypoint.sh @@ -0,0 +1,26 @@ +#!/usr/bin/env bash + +if [[ ${UMASK} ]]; then + umask "${UMASK}" +fi + +find /go -type d -print0 | xargs -0 chmod 777 +chmod -R a+rw /var/lib/photoprism /go + +if [[ ${UID} ]] && [[ ${GID} ]] && [[ ${UID} != "0" ]] && [[ $(id -u) = "0" ]]; then + groupadd -f -g "${GID}" "${GID}" + usermod -o -u "${UID}" -g "${GID}" photoprism + chown -R photoprism:photoprism /photoprism /var/lib/photoprism /go + gosu "${UID}:${GID}" "$@" & +elif [[ ${UID} ]] && [[ ${UID} != "0" ]] && [[ $(id -u) = "0" ]]; then + usermod -o -u "${UID}" photoprism + chown -R photoprism /photoprism /var/lib/photoprism /go + gosu "${UID}" "$@" & +else + "$@" & +fi + +PID=$! + +trap "kill $PID" INT TERM +wait \ No newline at end of file diff --git a/docker/photoprism/Dockerfile b/docker/photoprism/Dockerfile index 385e469a3..a394ebabe 100644 --- a/docker/photoprism/Dockerfile +++ b/docker/photoprism/Dockerfile @@ -1,4 +1,4 @@ -FROM photoprism/development:20201215 as build +FROM photoprism/development:20210115 as build # Set up project directory WORKDIR "/go/src/github.com/photoprism/photoprism"