Skip to content

Instantly share code, notes, and snippets.

@AlexanderKomkov
Last active March 25, 2024 16:27
Show Gist options
  • Save AlexanderKomkov/774a314d8716151ee76591fe84d15d3d to your computer and use it in GitHub Desktop.
Save AlexanderKomkov/774a314d8716151ee76591fe84d15d3d to your computer and use it in GitHub Desktop.
#!/bin/sh
set -e
INIT_SEM=/tmp/initialized.sem
fresh_container() {
[ ! -f $INIT_SEM ]
}
app_present() {
[ -f /app/config/app.php ]
}
vendor_present() {
[ -f /app/vendor ]
}
install_laravel() {
rm -rfv /tmp/laravel # just in case there is some files left
su-exec alpine:alpine composer create-project --prefer-dist laravel/laravel /tmp/laravel
echo "moving framework files to our working directory"
for x in /tmp/laravel/* /tmp/laravel/.[!.]* /tmp/laravel/..?*; do
if [ -e "$x" ]; then mv -- "$x" /app/; fi
done
rm -rfv /tmp/laravel
}
create_user() {
if [[ -z "${UID}" ]]; then
GID=1000
UID=1000
echo "Using default GID - $GID"
echo "Using default UID - $UID"
fi
#user_exists=grep -c "^$UID:$GID:" /etc/passwd
if ! grep "$UID:$GID" /etc/passwd &>/dev/null; then
echo "user don't exist"
echo "Creating group with id $GID"
echo "Creating user with id $UID"
addgroup -g $GID -S alpine && \
adduser -u $UID -S alpine -G alpine --shell /bin/ash
# fixing rights if user was changed, ignoring docker folder or we'll mess up our database
find /app/ -type d ! -path '/app/docker*' -exec chown $UID:$GID {} +
find /app/ -type f ! -path '/app/docker*' -exec chown $UID:$GID {} +
fi
}
if [ "${1}" == "php" -a "$2" == "artisan" -a "$3" == "serve" ]; then
create_user
# if app doesn't exist - install fresh copy of laravel framework
if ! app_present ; then
echo "Creating laravel application"
install_laravel
fi
echo "Installing/Updating Laravel dependencies (composer)"
if ! vendor_present ; then
su-exec alpine:alpine composer install
echo "Dependencies installed"
else
su-exec alpine:alpine composer update
echo "Dependencies updated"
fi
#wait_for_db
# this thing is actually a workaround for a problem with incompatibility of mysql and mariadb
# https://github.com/laravel/framework/issues/17337
if ! fresh_container; then
echo "#########################################################################"
echo " "
echo " App initialization skipped: "
echo " Delete the file $INIT_SEM and restart the container to reinitialize "
echo " You can alternatively run specific commands using docker-compose exec "
echo " e.g docker-compose exec myapp php artisan make:console FooCommand "
echo " "
echo "#########################################################################"
else
echo "Initialization finished"
touch $INIT_SEM
fi
fi
exec su-exec alpine:alpine tini -- "$@"
FROM php:7.2-fpm-alpine
# add our user and group first to make sure their IDs get assigned consistently, regardless of whatever dependencies get added
#RUN addgroup -S alpine && adduser -S -G alpine alpine
#ARG uid=1000
#ARG gid=1000
#RUN addgroup -g $gid -S alpine
#RUN adduser -u $uid -S alpine -G alpine
RUN apk update
# Basic packages - php and all the nessessary stuff
RUN set -ex && \
apk add --no-cache freetype libpng libjpeg-turbo freetype-dev libpng-dev libjpeg-turbo-dev \
libwebp-dev \
libxpm-dev \
zlib-dev \
openssl \
zip \
unzip \
git \
&& docker-php-ext-configure gd \
--with-freetype-dir=/usr/include/ \
--with-png-dir=/usr/include/ \
--with-jpeg-dir=/usr/include/ \
&& docker-php-ext-configure zip \
--with-zlib-dir=/usr/ \
&& docker-php-ext-install gd zip pdo pdo_mysql mbstring bcmath
# && apk del --no-cache freetype-dev libpng-dev libjpeg-turbo-dev
#
#RUN apk add --no-cache tini # tiny init for containers
# RUN apk add --no-cache busybox-suid # fixing su in busybox - well, we don't really need it, right?
RUN apk add --no-cache \
# grab su-exec for easy step-down from root
'su-exec>=0.2' \
# tiny init for containers
tini
# project dependencies
RUN apk add --no-cache aspell aspell-en
## INSTALL COMPOSER
RUN curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer
RUN apk --no-cache add \
--repository https://dl-3.alpinelinux.org/alpine/edge/testing/ \
musl-locales
ENV LANG=en_US.UTF-8 \
LANGUAGE=en_US.UTF-8
# Configs
COPY ./config/php/php.ini /usr/local/etc/php/php.ini
COPY app-entrypoint.sh /app-entrypoint.sh
#RUN addgroup -g 1000 -S alpine
#RUN adduser -u 1000 -S alpine -G alpine
RUN mkdir /app
#RUN chown alpine:alpine /app
WORKDIR /app
#USER alpine
#COPY . /app
#RUN composer install
ENTRYPOINT [ "/app-entrypoint.sh" ]
CMD [ "php", "-S", "0.0.0.0:8000", "/app/public/index.php" ]
EXPOSE 8000
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment