Skip to content

Commit b3dd42a

Browse files
committed
fixes for xdebug
1 parent 2d8621a commit b3dd42a

File tree

8 files changed

+85
-26
lines changed

8 files changed

+85
-26
lines changed

development/nginx-fpm/.env.example

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,4 +25,14 @@ POSTGRES_USERNAME=laravel
2525
POSTGRES_PASSWORD=secret
2626

2727
# The port on which the PostgreSQL database will be exposed.
28-
POSTGRES_PORT=5432
28+
POSTGRES_PORT=5432
29+
30+
# Xdebug configuration
31+
XDEBUG_ENABLED=true
32+
XDEBUG_MODE=develop,coverage,debug,profile
33+
XDEBUG_HOST=host.docker.internal
34+
XDEBUG_PORT=9003
35+
XDEBUG_IDE_KEY=DOCKER
36+
XDEBUG_START_WITH_REQUEST=trigger
37+
XDEBUG_LOG=/dev/stdout
38+
XDEBUG_LOG_LEVEL=0

development/nginx-fpm/compose.yaml

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,14 +25,19 @@ services:
2525
args:
2626
UID: ${UID}
2727
GID: ${GID}
28+
XDEBUG_ENABLED: ${XDEBUG_ENABLED}
29+
XDEBUG_MODE: ${XDEBUG_MODE}
30+
XDEBUG_HOST: ${XDEBUG_HOST}
31+
XDEBUG_IDE_KEY: ${XDEBUG_IDE_KEY}
32+
XDEBUG_LOG: ${XDEBUG_LOG}
33+
XDEBUG_LOG_LEVEL: ${XDEBUG_LOG_LEVEL}
2834
env_file:
2935
# Load the environment variables from the Laravel application
3036
- ${LARAVEL_APP_PATH}/.env
37+
user: "${UID}:${GID}"
3138
volumes:
3239
# Mount the application code for live updates
3340
- ${LARAVEL_APP_PATH}:/var/www
34-
ports:
35-
- "9004:9003" # Xdebug port
3641
networks:
3742
- laravel
3843
depends_on:

development/nginx-fpm/php-fpm/Dockerfile

Lines changed: 38 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -24,35 +24,57 @@ RUN apt-get update && apt-get install -y --no-install-recommends \
2424
bcmath \
2525
soap \
2626
&& pecl install redis xdebug \
27-
&& docker-php-ext-enable redis xdebug\
27+
&& docker-php-ext-enable redis \
2828
&& apt-get autoremove -y && apt-get clean && rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/*
2929

30-
# Configure Xdebug by appending settings to the existing INI file
31-
RUN echo "xdebug.mode=develop,coverage,debug,profile" >> /usr/local/etc/php/conf.d/docker-php-ext-xdebug.ini \
32-
&& echo "xdebug.idekey=docker" >> /usr/local/etc/php/conf.d/docker-php-ext-xdebug.ini \
33-
&& echo "xdebug.start_with_request=yes" >> /usr/local/etc/php/conf.d/docker-php-ext-xdebug.ini \
34-
&& echo "xdebug.log=/dev/stdout" >> /usr/local/etc/php/conf.d/docker-php-ext-xdebug.ini \
35-
&& echo "xdebug.log_level=0" >> /usr/local/etc/php/conf.d/docker-php-ext-xdebug.ini \
36-
&& echo "xdebug.client_port=9003" >> /usr/local/etc/php/conf.d/docker-php-ext-xdebug.ini \
37-
&& echo "xdebug.client_host=host.docker.internal" >> /usr/local/etc/php/conf.d/docker-php-ext-xdebug.ini
30+
# Use ARG to define environment variables passed from the Docker build command or Docker Compose.
31+
ARG XDEBUG_ENABLED
32+
ARG XDEBUG_MODE
33+
ARG XDEBUG_HOST
34+
ARG XDEBUG_IDE_KEY
35+
ARG XDEBUG_LOG
36+
ARG XDEBUG_LOG_LEVEL
37+
38+
# Configure Xdebug if enabled
39+
RUN if [ "${XDEBUG_ENABLED}" = "true" ]; then \
40+
docker-php-ext-enable xdebug && \
41+
echo "xdebug.mode=${XDEBUG_MODE}" >> /usr/local/etc/php/conf.d/docker-php-ext-xdebug.ini && \
42+
echo "xdebug.idekey=${XDEBUG_IDE_KEY}" >> /usr/local/etc/php/conf.d/docker-php-ext-xdebug.ini && \
43+
echo "xdebug.log=${XDEBUG_LOG}" >> /usr/local/etc/php/conf.d/docker-php-ext-xdebug.ini && \
44+
echo "xdebug.log_level=${XDEBUG_LOG_LEVEL}" >> /usr/local/etc/php/conf.d/docker-php-ext-xdebug.ini && \
45+
echo "xdebug.client_host=${XDEBUG_HOST}" >> /usr/local/etc/php/conf.d/docker-php-ext-xdebug.ini ; \
46+
echo "xdebug.start_with_request=yes" >> /usr/local/etc/php/conf.d/docker-php-ext-xdebug.ini ; \
47+
fi
3848

3949
# Copy the initialization script
40-
COPY ./entrypoint.sh /usr/local/bin/entrypoint.sh
50+
COPY ./entrypoint.dev.sh /usr/local/bin/entrypoint.sh
4151
RUN chmod +x /usr/local/bin/entrypoint.sh
4252

4353
# Set environment variables for user and group ID
4454
ARG UID=1000
4555
ARG GID=1000
4656

47-
# Create a user with the same UID and GID as the host user
48-
RUN groupadd -g ${GID} www && \
49-
useradd -u ${UID} -g www -m www
57+
# Create a new user with the specified UID and GID, reusing an existing group if GID exists
58+
RUN if getent group ${GID}; then \
59+
group_name=$(getent group ${GID} | cut -d: -f1); \
60+
useradd -m -u ${UID} -g ${GID} -s /bin/bash www; \
61+
else \
62+
groupadd -g ${GID} www && \
63+
useradd -m -u ${UID} -g www -s /bin/bash www; \
64+
group_name=www; \
65+
fi
66+
67+
# Dynamically update php-fpm to use the new user and group
68+
RUN sed -i "s/user = www-data/user = www/g" /usr/local/etc/php-fpm.d/www.conf && \
69+
sed -i "s/group = www-data/group = $group_name/g" /usr/local/etc/php-fpm.d/www.conf
5070

51-
# Set working directory
71+
72+
# Set the working directory
5273
WORKDIR /var/www
5374

54-
# Change ownership of the working directory to the new user
55-
RUN chown -R www:www /var/www
75+
# Copy the entrypoint script
76+
COPY ./entrypoint.dev.sh /usr/local/bin/entrypoint.sh
77+
RUN chmod +x /usr/local/bin/entrypoint.sh
5678

5779
# Change the default command to run the entrypoint script
5880
ENTRYPOINT ["/usr/local/bin/entrypoint.sh"]

development/nginx-fpm/workspace/Dockerfile

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,12 +31,15 @@ RUN apt-get update && apt-get install -y --no-install-recommends \
3131
&& curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer \
3232
&& apt-get autoremove -y && apt-get clean && rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/*
3333

34-
# Create a user with the same UID and GID as the host user
35-
RUN groupadd -g ${GID} www && \
36-
useradd -u ${UID} -g www -m www && \
34+
# If the group already exists, use it; otherwise, create the 'www' group
35+
RUN if getent group ${GID}; then \
36+
useradd -m -u ${UID} -g ${GID} -s /bin/bash www; \
37+
else \
38+
groupadd -g ${GID} www && \
39+
useradd -m -u ${UID} -g www -s /bin/bash www; \
40+
fi && \
3741
usermod -aG sudo www && \
38-
echo 'www ALL=(ALL) NOPASSWD:ALL' >> /etc/sudoers && \
39-
chown -R www:www /home/www
42+
echo 'www ALL=(ALL) NOPASSWD:ALL' >> /etc/sudoers
4043

4144
# Switch to the non-root user to install NVM and Node.js
4245
USER www

example-app/entrypoint.dev.sh

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
#!/bin/sh
2+
set -e
3+
4+
# Check if $UID and $GID are set, else fallback to default (1000:1000)
5+
USER_ID=${UID:-1000}
6+
GROUP_ID=${GID:-1000}
7+
8+
# Fix file ownership and permissions using the passed UID and GID
9+
echo "Fixing file permissions with UID=${USER_ID} and GID=${GROUP_ID}..."
10+
chown -R ${USER_ID}:${GROUP_ID} /var/www
11+
12+
# Clear configurations to avoid caching issues in development
13+
echo "Clearing configurations..."
14+
php artisan config:clear
15+
php artisan route:clear
16+
php artisan view:clear
17+
18+
# Run the default command (e.g., php-fpm or bash)
19+
exec "$@"
File renamed without changes.

example-app/routes/web.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,4 +62,4 @@
6262
$httpStatus = $isHealthy ? 200 : 503;
6363

6464
return response()->json($status, $httpStatus);
65-
});
65+
});

production/nginx-fpm/php-fpm/Dockerfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ RUN curl -o /usr/local/bin/php-fpm-healthcheck \
7070
&& chmod +x /usr/local/bin/php-fpm-healthcheck
7171

7272
# Copy the initialization script
73-
COPY ./entrypoint.sh /usr/local/bin/entrypoint.sh
73+
COPY ./entrypoint.prod.sh /usr/local/bin/entrypoint.sh
7474
RUN chmod +x /usr/local/bin/entrypoint.sh
7575

7676
# Copy the initial storage structure

0 commit comments

Comments
 (0)