Skip to content

Instantly share code, notes, and snippets.

@pOmelchenko
Created June 4, 2022 19:54
Show Gist options
  • Save pOmelchenko/38a9e346fd1bec6531a884cbd8353d20 to your computer and use it in GitHub Desktop.
Save pOmelchenko/38a9e346fd1bec6531a884cbd8353d20 to your computer and use it in GitHub Desktop.
# base image for install common dependencies
FROM php:8.1-fpm as base
RUN apt-get update && apt-get install --no-install-recommends -y \
&& rm -rf /var/lib/apt/lists/*
RUN mv "${PHP_INI_DIR}/php.ini-production" "${PHP_INI_DIR}/php.ini" \
&& touch "${PHP_INI_DIR}/conf.d/docker.ini" && { \
echo '[docker]'; \
} >> "${PHP_INI_DIR}/conf.d/docker.ini"
# build composer dependency
FROM base as build
ARG COMPOSER_TOKEN
RUN apt-get update && apt-get install --no-install-recommends -y \
git \
unzip \
zlib1g-dev \
&& rm -rf /var/lib/apt/lists/*
COPY --from=composer /usr/bin/composer /usr/bin/composer
COPY composer.* .
RUN if [ -z ${COMPOSER_TOKEN} ]; then \
composer config gitlab-token.gitlab.com "${COMPOSER_TOKEN}"; \
fi \
&& composer install --no-dev --no-scripts
# setup permission for rootles
FROM base as app
ARG UID=1000
ARG GID=1000
ARG ROOTLES_USER=whuser
ENV UID=${UID} \
UID=${UID} \
ROOTLES_USER=${ROOTLES_USER}
# run `delgroup dialout` if GID equals 20 for macOS users
RUN if [ ${GID} = 20 ]; then \
delgroup dialout; \
fi \
&& addgroup --gid ${GID} --system ${ROOTLES_USER} \
&& adduser --gid ${GID} --uid ${UID} --shell /bin/sh --system ${ROOTLES_USER}
COPY --chown=${UID}:${GID} . .
# image for dev environment
# usage: docker build --target=production --tag adapter:dev
FROM app as dev
ARG COMPOSER_TOKEN
RUN apt-get update && apt-get install --no-install-recommends -y \
git \
unzip \
zlib1g-dev \
rsync \
ssh \
&& rm -rf /var/lib/apt/lists/* \
RUN docker-php-source extract \
&& pecl install xdebug \
&& docker-php-ext-enable xdebug \
&& docker-php-source delete && rm -r /tmp/pear/*
COPY --from=composer /usr/bin/composer /usr/bin/composer
RUN if [ -z ${COMPOSER_TOKEN} ]; then \
composer config gitlab-token.gitlab.com "${COMPOSER_TOKEN}"; \
fi \
&& composer install
USER ${ROOTLES_USER}
CMD php -S 0.0.0.0:80 -t ./public
# image for production environment
# usage: docker build --target=production --tag adapter:latest --tag adapter:version_tag
FROM app as production
COPY --chown=${UID}:${GID} --from=build /var/www/html/vendor/ ./vendor/
USER ${ROOTLES_USER}
RUN ./bin/console cache:clear \
&& ./bin/console assets:install public
CMD ["php-fpm"]
# fallback for build without `target` option
# usage: docker build --tag adapter:latest --tag adapter:version_tag
FROM production
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment