From cbd06482f47c7789d575ea38c787156b333c3296 Mon Sep 17 00:00:00 2001 From: vista Date: Fri, 23 Aug 2024 15:54:47 +0200 Subject: [PATCH] switch base container from ubuntu to php-alpine (#9) Co-authored-by: Ruben Momoa Reviewed-on: https://git.narvas.tech/vista/simple-laravel/pulls/9 --- .env.example | 19 ---------- .woodpecker/build.yml | 46 ------------------------- .woodpecker/build_dev.yml | 23 +++++++++++++ .woodpecker/build_release.yml | 40 +++++++++++++++++++++ Dockerfile | 65 +++++++++++++++-------------------- build/start-container.sh | 29 +++++++++++----- docker-compose.yml | 35 +++++++++++-------- 7 files changed, 131 insertions(+), 126 deletions(-) delete mode 100644 .env.example delete mode 100644 .woodpecker/build.yml create mode 100644 .woodpecker/build_dev.yml create mode 100644 .woodpecker/build_release.yml diff --git a/.env.example b/.env.example deleted file mode 100644 index 8b1213a..0000000 --- a/.env.example +++ /dev/null @@ -1,19 +0,0 @@ -# the laravel version to install (empty for latest) -LARAVEL_VERSION= - -# weather to run "npm dev run" at boot -AUTO_START_NPM_DEV=true - -# external ports -FORWARD_LARAVEL_PORT=80 -FORWARD_VITE_PORT=5173 -FORWARD_DB_PORT=3306 -FORWARD_REDIS_PORT=6379 -FORWARD_MAILPIT_PORT=1025 -FORWARD_MAILPIT_DASHBOARD_PORT=8025 - -# DB settings (this will overwrite the defaults in the laravel project) -DB_PORT=3306 -DB_DATABASE=laravel -DB_USERNAME=laravel -DB_PASSWORD=password diff --git a/.woodpecker/build.yml b/.woodpecker/build.yml deleted file mode 100644 index 3e89361..0000000 --- a/.woodpecker/build.yml +++ /dev/null @@ -1,46 +0,0 @@ -when: - branch: master - -steps: - release_build_and_publish: - image: woodpeckerci/plugin-docker-buildx - settings: - username: vistanarvas - password: - from_secret: DOCKERHUB_TOKEN - context: ./build - repo: vistanarvas/${CI_REPO_NAME} - auto_tag: true - when: - event: release - - cron_build_and_publish: - image: woodpeckerci/plugin-docker-buildx - settings: - username: vistanarvas - password: - from_secret: DOCKERHUB_TOKEN - context: ./build - repo: vistanarvas/${CI_REPO_NAME} - tag: nightly - when: - path: - include: [ '.woodpecker/build.yml', 'build/*', 'Dockerfile' ] - cron: nightly - event: [ cron ] - - dev_build_and_publish: - image: woodpeckerci/plugin-docker-buildx - settings: - username: vistanarvas - password: - from_secret: DOCKERHUB_TOKEN - context: ./build - repo: vistanarvas/${CI_REPO_NAME} - tag: ${CI_COMMIT_BRANCH} - when: - branch: - exclude: [ main, master ] - path: - include: [ '.woodpecker/build.yml', 'build/*', 'Dockerfile' ] - event: [ push, manual ] \ No newline at end of file diff --git a/.woodpecker/build_dev.yml b/.woodpecker/build_dev.yml new file mode 100644 index 0000000..794e833 --- /dev/null +++ b/.woodpecker/build_dev.yml @@ -0,0 +1,23 @@ +matrix: + PHP_VERSION: + - 8 + +when: + branch: + exclude: [ main, master ] + path: + include: [ '.woodpecker/build_dev.yml', 'build/*', 'Dockerfile' ] + event: [ push, manual ] + +steps: + dev_alpine_build_and_publish: + image: woodpeckerci/plugin-docker-buildx + settings: + username: vistanarvas + password: + from_secret: DOCKERHUB_TOKEN + context: ./build + repo: vistanarvas/${CI_REPO_NAME} + tag: test-${CI_COMMIT_BRANCH}-php-${PHP_VERSION} + build_args: + - BASE_VERSION=${PHP_VERSION} \ No newline at end of file diff --git a/.woodpecker/build_release.yml b/.woodpecker/build_release.yml new file mode 100644 index 0000000..ec568cf --- /dev/null +++ b/.woodpecker/build_release.yml @@ -0,0 +1,40 @@ +matrix: + PHP_VERSION: + - 8 + - 8.3 + - 8.2 + - 8.1 + +when: + branch: + include: [ master ] + cron: nightly + event: [ cron, release, manual ] + +steps: + cron_pre_check: + image: docker + commands: + - docker pull php:${PHP_VERSION}-fpm-alpine + - docker pull vistanarvas/simple-laravel:${PHP_VERSION}-fpm-alpine + - BASE_IMG=$(docker inspect php:${PHP_VERSION}-fpm-alpine --format='{{.Created}}' | cut -d '.' -f 1) + - PREV_IMG=$(docker inspect vistanarvas/simple-laravel:php-${PHP_VERSION} --format='{{.Created}}' | cut -d '.' -f 1) + - BASE_DATE=$(date -D "%Y-%m-%dT%H:%M:%S" -d "$BASE_IMG" +%s) + - PREV_DATE=$(date -D "%Y-%m-%dT%H:%M:%S" -d "$PREV_IMG" +%s) + - if [ "$PREV_DATE" -gt "$BASE_DATE" ]; then echo "no rebuild needed"; exit 1; fi + volumes: + - /var/run/docker.sock:/var/run/docker.sock + when: + cron: nightly + event: [ cron ] + + release_alpine_build_and_publish: + image: woodpeckerci/plugin-docker-buildx + settings: + username: vistanarvas + password: + from_secret: DOCKERHUB_TOKEN + context: ./build + repo: vistanarvas/${CI_REPO_NAME} + auto_tag: true + auto_tag_suffix: -php-${PHP_VERSION} \ No newline at end of file diff --git a/Dockerfile b/Dockerfile index 032079c..d60ea31 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,48 +1,37 @@ -FROM ubuntu:latest +ARG BASE_VERSION -ARG USERNAME=laravel -ARG USER_UID=1080 +FROM php:${BASE_VERSION}-fpm-alpine -# node version number like "12.22.1" or empty for the latest version -ARG NODE_VERSION='' - -# composer version number like "2.1" or empty for the latest version -ARG COMPOSER_VERSION='' - -# disables asking for input -ARG DEBIAN_FRONTEND="noninteractive" - -ENV NVM_DIR="/home/$USERNAME/.nvm" +# port to for the webserver +ENV WEBSERVER_PORT=8080 COPY start-container.sh /usr/local/bin/start-container -RUN apt-get -y update -RUN apt-get install -y curl git 7zip php php-xml php-curl php-xml php-mbstring php-mysql php-gd -RUN apt-get clean -RUN useradd --create-home --uid=${USER_UID} ${USERNAME} -RUN chmod +x /usr/local/bin/start-container -RUN echo "install composer" -RUN curl -o composer-setup.php https://getcomposer.org/installer -RUN curl -o composer-setup.sig https://composer.github.io/installer.sig -RUN echo " composer-setup.php" >> composer-setup.sig -RUN sha384sum -c composer-setup.sig -RUN if [ -n "$COMPOSER_VERSION" ]; then COMPOSER_VERSION="--${COMPOSER_VERSION}"; fi -RUN php composer-setup.php --install-dir=/usr/local/bin --filename=composer $COMPOSER_VERSION -RUN rm composer-setup.php composer-setup.sig /var/www/html/* -RUN chown $USERNAME:$USERNAME /var/www/html +RUN apk add --no-cache \ + curl-dev \ + libxml2-dev \ + oniguruma-dev \ + libpng-dev \ + icu-dev \ + php-ctype \ + php-curl \ + php-dom \ + php-fileinfo \ + php-mbstring \ + php-session \ + php-xml \ + php-intl \ + php-bcmath \ + php-exif \ + php-xmlwriter \ + php-tokenizer \ + composer \ + npm \ + && docker-php-ext-install filter pdo_mysql \ + && chmod +x /usr/local/bin/start-container -EXPOSE 8080 +EXPOSE ${WEBSERVER_PORT} WORKDIR /var/www/html -USER $USERNAME - -RUN echo "install nvm and node" \ - && curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.3/install.sh | bash \ - && if [ -z "$NODE_VERSION" ]; then NODE_VERSION="node"; fi \ - && echo "source $NVM_DIR/nvm.sh" >> /home/$USERNAME/.bashrc \ - && [ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh" \ - && nvm install $NODE_VERSION \ - && nvm alias default $NODE_VERSION - ENTRYPOINT ["start-container"] \ No newline at end of file diff --git a/build/start-container.sh b/build/start-container.sh index 5b2a357..38c8251 100644 --- a/build/start-container.sh +++ b/build/start-container.sh @@ -1,15 +1,28 @@ -#!/usr/bin/env bash +#!/usr/bin/env ash # exit when any command fails set -e -# init nvm -\. $NVM_DIR/nvm.sh +# install extra packages and extensions on first boot +if [[ ! -f /etc/first_boot ]]; then + if [[ -n "${EXTRA_APK_PACKAGES// /}" ]]; then + # shellcheck disable=SC2086 + apk add --no-cache $EXTRA_APK_PACKAGES + fi + if [[ -n "${EXTRA_PHP_EXTENSIONS// /}" ]]; then + # shellcheck disable=SC2086 + docker-php-ext-install $EXTRA_PHP_EXTENSIONS + fi + touch /etc/first_boot +fi - -if [[ "$INSTALL_BEFORE_START" = true ]]; then - echo "composer and npm install" +if [[ "$BOOT_COMPOSER_INSTALL" = true ]]; then + echo "composer install" composer install +fi + +if [[ "$BOOT_NPM_INSTALL" = true ]]; then + echo "npm install" npm install fi @@ -18,10 +31,10 @@ if [ ! -f /var/www/html/artisan ]; then exit fi -if [[ "$AUTO_START_NPM_DEV" = true ]]; then +if [[ "$BOOT_NPM_DEV" = true ]]; then echo "Staring npm dev" npm run dev --prefix /var/www/html & fi echo "Staring Laravel" -php /var/www/html/artisan serve +php /var/www/html/artisan serve --host=0.0.0.0 --port="$WEBSERVER_PORT" diff --git a/docker-compose.yml b/docker-compose.yml index d610b78..9406d14 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -1,20 +1,25 @@ version: '3' services: app: - build: - context: build - dockerfile: Dockerfile - image: laravel + image: vistanarvas/simple-laravel ports: - - '${FORWARD_LARAVEL_PORT:-80}:8080' - - '${FORWARD_VITE_PORT:-5173}:5173' + - '8080:80' + - '5173:5173' environment: - LARAVEL_VERSION: '${LARAVEL_VERSION}' - AUTO_START_NPM_DEV: '${AUTO_START_NPM_DEV:-false}' - DB_HOST: mysql + WEBSERVER_PORT: 80 DB_CONNECTION: mysql - env_file: - .env + DB_HOST: mysql + DB_PORT: 3306 + DB_DATABASE: laravel + DB_USERNAME: laravel + DB_PASSWORD: password + BOOT_NPM_DEV: true # also starts `npm dev` + BOOT_NPM_INSTALL: true # runs `npm install` on boot + BOOT_COMPOSER_INSTALL: true # runs `composer install` on boot + + # Extra packages and php extensions to install on first boot + # EXTRA_APK_PACKAGES: # example: libpng-dev icu-dev + # EXTRA_PHP_EXTENSIONS: # example: exif intl bcmath gd pdo_mysql depends_on: - mysql - redis @@ -23,7 +28,7 @@ services: mysql: image: 'mysql/mysql-server:8.0' ports: - - '${FORWARD_DB_PORT:-3306}:3306' + - '3306:3306' environment: MYSQL_ROOT_PASSWORD: '${DB_PASSWORD}' MYSQL_ROOT_HOST: '%' @@ -37,15 +42,15 @@ services: redis: image: 'redis:alpine' ports: - - '${FORWARD_REDIS_PORT:-6379}:6379' + - '6379:6379' volumes: - 'redis:/data' mailpit: image: 'axllent/mailpit:latest' ports: - - '${FORWARD_MAILPIT_PORT:-1025}:1025' - - '${FORWARD_MAILPIT_DASHBOARD_PORT:-8025}:8025' + - '1025:1025' + - '8025:8025' volumes: mysql: