Skip to content

Commit 4b6972c

Browse files
refactor: Update environment configuration and initialization script for improved user/group handling and permissions setup. (#113)
1 parent 735a7a4 commit 4b6972c

File tree

5 files changed

+60
-40
lines changed

5 files changed

+60
-40
lines changed

.env

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
# User and Group settings
2-
GROUP_ID=1000
3-
GROUP_NAME=www-data
2+
# For automatic host user detection, run before docker-compose:
3+
# export USER_ID=$(id -u) && export GROUP_ID=$(id -g)
4+
# Otherwise, these defaults will be used:
45
USER_ID=1000
6+
GROUP_ID=1000
57
USER_NAME=www-data
8+
GROUP_NAME=www-data
69

710
# Yii2 environment settings
811
YII_DEBUG=true

docker-compose.frankenphp.yml

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -12,24 +12,23 @@ services:
1212
entrypoint: ["/usr/local/bin/entrypoint.sh"]
1313
env_file:
1414
- .env
15+
environment:
16+
TZ: "UTC"
17+
YII_DEBUG: "${YII_DEBUG:-false}"
18+
YII_ENV: "${YII_ENV:-prod}"
19+
ports:
20+
- '8081:80'
21+
- '8444:443'
22+
- '8444:443/udp'
1523
restart: always
16-
working_dir: /app
1724
volumes:
1825
- ./:/app
19-
- ~/.composer-docker/cache:/root/.composer/cache:delegated
26+
- ${HOME}/.composer-docker/cache:/var/www/.composer/cache:delegated
2027
- caddy_data:/data
2128
- caddy_config:/config
22-
ports:
23-
- '8081:80'
24-
- '8444:443'
25-
- '8444:443/udp'
26-
environment:
27-
TZ: "UTC"
28-
YII_DEBUG: "${YII_DEBUG:-false}"
29-
YII_ENV: "${YII_ENV:-prod}"
29+
working_dir: /app
3030
tty: true
3131

32-
# Volumes needed for Caddy certificates and configuration
3332
volumes:
3433
caddy_data:
3534
caddy_config:

docker/frankenphp/Dockerfile

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,16 +34,23 @@ ENV COMPOSER_ALLOW_SUPERUSER=1
3434
# Change PHP config
3535
COPY docker/php/php.ini /usr/local/etc/php/conf.d/base.ini
3636

37-
# Install supervisord and Node.js (includes npm)
38-
RUN apt-get update && apt-get install -y \
37+
# Install supervisord, gosu, and Node.js (version simple)
38+
RUN apt-get update && apt-get install -y --no-install-recommends \
3939
supervisor \
4040
curl \
41-
--no-install-recommends \
41+
gosu \
4242
&& curl -fsSL https://deb.nodesource.com/setup_lts.x | bash - \
4343
&& apt-get install -y nodejs \
4444
&& apt-get clean \
4545
&& rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/*
4646

47+
# Apply the user/group IDs to www-data
48+
RUN usermod -u ${USER_ID} www-data && groupmod -g ${GROUP_ID} www-data
49+
50+
# Create composer and npm cache directories with proper ownership
51+
RUN mkdir -p /var/www/.composer/cache /var/www/.npm && \
52+
chown -R www-data:www-data /var/www/.composer /var/www/.npm
53+
4754
# Copy supervisord config
4855
COPY docker/frankenphp/supervisord/supervisord.conf /etc/supervisor/supervisord.conf
4956

@@ -64,7 +71,7 @@ RUN chmod +x /usr/local/bin/init.sh /usr/local/bin/entrypoint.sh && \
6471
# Test that scripts have valid syntax
6572
bash -n /usr/local/bin/init.sh && \
6673
bash -n /usr/local/bin/entrypoint.sh && \
67-
echo "Scripts validated successfully"
74+
echo "Scripts validated successfully..."
6875

6976
# Use ENTRYPOINT to guarantee execution
7077
ENTRYPOINT ["/usr/local/bin/entrypoint.sh"]
Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
[program:frankenphp]
22
command=/usr/local/bin/frankenphp run --config /etc/caddy/Caddyfile
3-
autostart=true
43
autorestart=true
5-
priority=10
4+
autostart=true
65
killasgroup=true
7-
stopasgroup=true
8-
stopsignal=QUIT
9-
stdout_logfile=/dev/stdout
10-
stdout_logfile_maxbytes=0
6+
priority=10
117
stderr_logfile=/dev/stderr
128
stderr_logfile_maxbytes=0
9+
stdout_logfile=/dev/stdout
10+
stdout_logfile_maxbytes=0
11+
stopasgroup=true
12+
stopsignal=QUIT
1313
user=www-data

docker/init.sh

Lines changed: 28 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,12 @@ NC='\033[0m'
88

99
echo -e "${GREEN}Starting container setup...${NC}"
1010

11+
# Create necessary Caddy directories with proper permissions
12+
echo -e "${YELLOW}Creating Caddy directories...${NC}"
13+
mkdir -p /data/caddy/locks /config/caddy
14+
chown -R www-data:www-data /data /config
15+
chmod -R 755 /data /config
16+
1117
# Create necessary Yii2 directories if they don't exist
1218
echo -e "${YELLOW}Creating Yii2 directories...${NC}"
1319
mkdir -p /app/runtime/cache
@@ -48,29 +54,34 @@ echo -e "${GREEN}Setup completed.${NC}"
4854
if [ -f "/app/composer.json" ] && [ ! -d "/app/vendor" ]; then
4955
echo -e "${YELLOW}Installing Composer dependencies...${NC}"
5056

51-
# Install dependencies based on environment
57+
# Give www-data write access without exposing the tree to everyone
58+
chown -R www-data:www-data /app && \
59+
chmod -R u+rwX,g+rwX /app
60+
61+
# Create and configure npm cache directory for www-data
62+
mkdir -p /var/www/.npm
63+
chown -R www-data:www-data /var/www/.npm
64+
65+
# Install dependencies with proper environment variables
5266
if [ "$YII_ENV" = "prod" ]; then
5367
# Production: exclude dev dependencies and optimize autoloader
54-
composer install --no-dev --optimize-autoloader --no-interaction
68+
gosu www-data env \
69+
HOME=/var/www \
70+
COMPOSER_HOME=/var/www/.composer \
71+
COMPOSER_CACHE_DIR=/var/www/.composer/cache \
72+
npm_config_cache=/var/www/.npm \
73+
composer install --no-dev --optimize-autoloader --no-interaction
5574
else
5675
# Development: include dev dependencies
57-
composer install --optimize-autoloader --no-interaction
58-
fi
59-
60-
# Set proper ownership for vendor directory if possible
61-
if chown -R www-data:www-data /app/vendor 2>/dev/null; then
62-
echo -e "${GREEN}✓ Vendor directory ownership set${NC}"
76+
gosu www-data env \
77+
HOME=/var/www \
78+
COMPOSER_HOME=/var/www/.composer \
79+
COMPOSER_CACHE_DIR=/var/www/.composer/cache \
80+
npm_config_cache=/var/www/.npm \
81+
composer install --optimize-autoloader --no-interaction
6382
fi
6483

65-
echo -e "${GREEN}✓ Composer dependencies installed successfully.${NC}"
66-
fi
67-
68-
# Set permissions for node_modules directory if it exists
69-
if chown -R www-data:www-data /app/node_modules 2>/dev/null; then
70-
chmod -R 775 /app/node_modules
71-
echo -e "${GREEN}✓ Node modules directory ownership set${NC}"
72-
else
73-
echo -e "${YELLOW}⚠ Node modules directory ownership could not be set (mounted volume?)${NC}"
84+
echo -e "${GREEN}✓ Composer dependencies installed successfully${NC}"
7485
fi
7586

7687
echo -e "${GREEN}Starting supervisord...${NC}"

0 commit comments

Comments
 (0)