Skip to content

Commit 5a4c30a

Browse files
committed
Add app user in docker php image
1 parent 69a5267 commit 5a4c30a

File tree

3 files changed

+33
-17
lines changed

3 files changed

+33
-17
lines changed

Makefile

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,11 @@ UNAME := $(shell uname)
33
DOCKER_COMPOSE := docker compose
44
PHP_CLI := $(DOCKER_COMPOSE) run --rm app
55

6+
include .env
7+
68
.PHONY: help
79
help:
8-
@grep -E '(^[a-zA-Z0-9_-]+:.*?##)|(^##)' $(MAKEFILE_LIST) | awk 'BEGIN {FS = ":.*?## "; printf "Usage: make \033[32m<target>\033[0m\n"}{printf "\033[32m%-15s\033[0m %s\n", $$1, $$2}' | sed -e 's/\[32m## /\n[33m/'
10+
@grep -E '(^[a-zA-Z0-9_-]+:.*?##)|(^##)' $(firstword $(MAKEFILE_LIST)) | awk 'BEGIN {FS = ":.*?## "; printf "Usage: make \033[32m<target>\033[0m\n"}{printf "\033[32m%-20s\033[0m %s\n", $$1, $$2}' | sed -e 's/\[32m## /\n[33m/'
911

1012
## Docker
1113
.PHONY: up
@@ -20,14 +22,18 @@ down: ## Stop and remove containers.
2022
ps: ## List active containers.
2123
$(DOCKER_COMPOSE) ps
2224

25+
.PHONY: build
26+
build: ## Build images.
27+
$(DOCKER_COMPOSE) build
28+
2329
## GdprDump
2430
.PHONY: dump
25-
dump: .env vendor ## Run bin/gdpr-dump command. Example: "make dump c=test.yaml"
31+
dump: vendor ## Run bin/gdpr-dump command. Example: "make dump c=test.yaml"
2632
@$(eval c ?=)
2733
$(PHP_CLI) bin/gdpr-dump $(c)
2834

2935
.PHONY: compile
30-
compile: .env ## Run bin/compile command.
36+
compile: ## Run bin/compile command.
3137
$(PHP_CLI) composer install --no-dev
3238
$(PHP_CLI) bin/compile $(c)
3339
$(PHP_CLI) composer install
@@ -39,20 +45,20 @@ composer: ## Run composer. Example: "make composer c=update"
3945

4046
## Code Quality
4147
.PHONY: analyse
42-
analyse: .env vendor ## Run code analysis tools (parallel-lint, phpcs, phpstan).
48+
analyse: vendor ## Run code analysis tools (parallel-lint, phpcs, phpstan).
4349
$(PHP_CLI) composer audit
4450
$(PHP_CLI) vendor/bin/parallel-lint app bin src tests
4551
$(PHP_CLI) vendor/bin/phpcs
4652
$(PHP_CLI) vendor/bin/phpstan analyse
4753

4854
.PHONY: test
49-
test: .env vendor ## Run phpunit.
55+
test: vendor ## Run phpunit.
5056
$(PHP_CLI) vendor/bin/phpunit
5157

52-
vendor:
58+
vendor: composer.json
5359
$(PHP_CLI) composer install
5460

55-
.env:
61+
.env: | .env.dist
5662
@cp .env.dist .env
5763
ifeq ($(UNAME), Linux)
5864
@sed -i -e "s/^UID=.*/UID=$$(id -u)/" -e "s/^GID=.*/GID=$$(id -g)/" .env

compose.yaml

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,18 @@
11
services:
22
app:
3-
build: 'docker/php-cli'
4-
user: '${UID:-}:${GID:-}'
3+
build:
4+
context: 'docker/php-cli'
5+
args:
6+
APP_UID: '${UID}'
7+
APP_GID: '${GID}'
58
environment:
69
COMPOSER_AUTH: '${COMPOSER_AUTH:-}'
710
DB_HOST: 'db'
811
depends_on:
912
- 'db'
1013
volumes:
11-
- '.:/var/www/html'
12-
- 'composer-cache:/tmp/composer'
14+
- '.:/var/www/html:rw,cached'
15+
- 'composer:/home/www/.composer'
1316

1417
db:
1518
image: 'mariadb:10.6'
@@ -25,5 +28,5 @@ services:
2528
- './docker/db-data:/docker-entrypoint-initdb.d'
2629

2730
volumes:
28-
composer-cache:
31+
composer:
2932
db-data:

docker/php-cli/Dockerfile

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,24 @@ FROM php:8.1-cli-alpine
33
# Install required PHP extensions
44
RUN docker-php-ext-install pdo_mysql
55

6-
# Copy PHP configuration
6+
# PHP configuration
77
RUN ln -s $PHP_INI_DIR/php.ini-development $PHP_INI_DIR/php.ini
88
COPY ./config/php.ini $PHP_INI_DIR/conf.d/gdpr-dump.ini
99

1010
# Install composer
1111
ENV COMPOSER_ALLOW_SUPERUSER 1
12-
ENV COMPOSER_HOME /tmp/composer
12+
COPY --from=composer:2 /usr/bin/composer /usr/local/bin/composer
13+
14+
# Add application user (more elegant than changing the uid of www-data, which is a system user)
15+
ARG APP_UID=1000
16+
ARG APP_GID=1000
1317
RUN set -ex; \
14-
mkdir $COMPOSER_HOME; \
15-
chmod 777 $COMPOSER_HOME
16-
COPY --from=composer:2 /usr/bin/composer /usr/local/bin/
18+
addgroup --gid "$APP_GID" www; \
19+
adduser --uid "$APP_UID" --ingroup www --disabled-password --gecos "" www
20+
USER www
21+
22+
# Create directories that may be mounted as volumes (otherwise they would be created with root permissions)
23+
RUN mkdir -p ~/.composer
1724

1825
WORKDIR /var/www/html
1926
CMD ["sh"]

0 commit comments

Comments
 (0)