Skip to content

Commit 16124f9

Browse files
committed
Use single Dockerfile with multi stage for dev and prod
1 parent 7b78e32 commit 16124f9

File tree

4 files changed

+69
-84
lines changed

4 files changed

+69
-84
lines changed

compose.dev.yaml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,8 @@ services:
2121
# For the php-fpm service, we will create a custom image to install the necessary PHP extensions and setup proper permissions.
2222
build:
2323
context: .
24-
dockerfile: ./docker/development/php-fpm/Dockerfile
24+
dockerfile: ./docker/common/php-fpm/Dockerfile
25+
target: development
2526
args:
2627
UID: ${UID:-1000}
2728
GID: ${GID:-1000}

compose.prod.yaml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,8 @@ services:
2929
# For the php-fpm service, we will create a custom image to install the necessary PHP extensions and setup proper permissions.
3030
build:
3131
context: .
32-
dockerfile: ./docker/production/php-fpm/Dockerfile
32+
dockerfile: ./docker/common/php-fpm/Dockerfile
33+
target: production
3334
restart: unless-stopped
3435
volumes:
3536
- laravel-storage-production:/var/www/storage # Mount the storage volume

docker/production/php-fpm/Dockerfile renamed to docker/common/php-fpm/Dockerfile

Lines changed: 65 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ RUN apt-get update && apt-get install -y --no-install-recommends \
2727
&& docker-php-ext-enable redis \
2828
&& apt-get autoremove -y && apt-get clean && rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/*
2929

30+
3031
# Set the working directory inside the container
3132
WORKDIR /var/www
3233

@@ -51,8 +52,9 @@ COPY . /var/www
5152
RUN curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer \
5253
&& composer install --no-dev --optimize-autoloader --no-interaction --no-progress --prefer-dist
5354

55+
5456
# Stage 2: Production environment
55-
FROM php:8.3-fpm
57+
FROM php:8.3-fpm AS production
5658

5759
# Install only runtime libraries needed in production
5860
# libfcgi-bin and procps are required for the php-fpm-healthcheck script
@@ -113,3 +115,65 @@ ENTRYPOINT ["/usr/local/bin/entrypoint.sh"]
113115
# Expose port 9000 and start php-fpm server
114116
EXPOSE 9000
115117
CMD ["php-fpm"]
118+
119+
120+
# Stage 3: Development image
121+
FROM production AS development
122+
123+
# Use ARG to define environment variables passed from the Docker build command or Docker Compose.
124+
ARG XDEBUG_ENABLED=true
125+
ARG XDEBUG_MODE=develop,coverage,debug,profile
126+
ARG XDEBUG_HOST=host.docker.internal
127+
ARG XDEBUG_IDE_KEY=DOCKER
128+
ARG XDEBUG_LOG=/dev/stdout
129+
ARG XDEBUG_LOG_LEVEL=0
130+
131+
USER root
132+
133+
# Configure Xdebug if enabled
134+
RUN if [ "${XDEBUG_ENABLED}" = "true" ]; then \
135+
pecl install xdebug && \
136+
docker-php-ext-enable xdebug && \
137+
echo "xdebug.mode=${XDEBUG_MODE}" >> /usr/local/etc/php/conf.d/docker-php-ext-xdebug.ini && \
138+
echo "xdebug.idekey=${XDEBUG_IDE_KEY}" >> /usr/local/etc/php/conf.d/docker-php-ext-xdebug.ini && \
139+
echo "xdebug.log=${XDEBUG_LOG}" >> /usr/local/etc/php/conf.d/docker-php-ext-xdebug.ini && \
140+
echo "xdebug.log_level=${XDEBUG_LOG_LEVEL}" >> /usr/local/etc/php/conf.d/docker-php-ext-xdebug.ini && \
141+
echo "xdebug.client_host=${XDEBUG_HOST}" >> /usr/local/etc/php/conf.d/docker-php-ext-xdebug.ini ; \
142+
echo "xdebug.start_with_request=yes" >> /usr/local/etc/php/conf.d/docker-php-ext-xdebug.ini ; \
143+
fi
144+
145+
# Add ARGs for syncing permissions
146+
ARG UID=1000
147+
ARG GID=1000
148+
149+
# Create a new user with the specified UID and GID, reusing an existing group if GID exists
150+
RUN if getent group ${GID}; then \
151+
group_name=$(getent group ${GID} | cut -d: -f1); \
152+
useradd -m -u ${UID} -g ${GID} -s /bin/bash www; \
153+
else \
154+
groupadd -g ${GID} www && \
155+
useradd -m -u ${UID} -g www -s /bin/bash www; \
156+
group_name=www; \
157+
fi
158+
159+
# Dynamically update php-fpm to use the new user and group
160+
RUN sed -i "s/user = www-data/user = www/g" /usr/local/etc/php-fpm.d/www.conf && \
161+
sed -i "s/group = www-data/group = $group_name/g" /usr/local/etc/php-fpm.d/www.conf
162+
163+
164+
# Set the working directory
165+
WORKDIR /var/www
166+
167+
# Copy the entrypoint script
168+
COPY ./docker/development/php-fpm/entrypoint.sh /usr/local/bin/entrypoint.sh
169+
RUN chmod +x /usr/local/bin/entrypoint.sh
170+
171+
# Switch back to the non-privileged user to run the application
172+
USER www-data
173+
174+
# Change the default command to run the entrypoint script
175+
ENTRYPOINT ["/usr/local/bin/entrypoint.sh"]
176+
177+
# Expose port 9000 and start php-fpm server
178+
EXPOSE 9000
179+
CMD ["php-fpm"]

docker/development/php-fpm/Dockerfile

Lines changed: 0 additions & 81 deletions
This file was deleted.

0 commit comments

Comments
 (0)