From 7e242d5aa22c5a84a6d9f5d4309fd02ed70e5196 Mon Sep 17 00:00:00 2001 From: Kamil Niemczycki Date: Tue, 7 Mar 2023 23:45:45 +0100 Subject: [PATCH] Docker reorganisation (#6) * - new file location for docker * update of docker containers * update readme --- .env.example | 14 +++- .gitignore | 6 +- README.md | 34 ++++++--- config/docker/dev/laravel/Dockerfile | 27 ------- config/docker/dev/laravel/install.sh | 28 ------- docker-compose.yml | 75 ++++++++++++++----- .../dev/nginx/default.conf | 2 +- environment/dev/npm/Dockerfile | 3 + environment/dev/php/Dockerfile | 34 +++++++++ environment/dev/php/php.ini | 9 +++ 10 files changed, 140 insertions(+), 92 deletions(-) delete mode 100644 config/docker/dev/laravel/Dockerfile delete mode 100755 config/docker/dev/laravel/install.sh rename {config/docker => environment}/dev/nginx/default.conf (91%) create mode 100644 environment/dev/npm/Dockerfile create mode 100644 environment/dev/php/Dockerfile create mode 100644 environment/dev/php/php.ini diff --git a/.env.example b/.env.example index 2b702d8..c7a9d97 100644 --- a/.env.example +++ b/.env.example @@ -1,10 +1,16 @@ -USER_UID=1000 -USER_NAME=laravel - APP_NAME=KamilCraftAPI APP_ENV=local APP_KEY= APP_DEBUG=true APP_URL=http://localhost -DB_CONNECTION=sqlite +DB_CONNECTION=mysql +DB_HOST=kamilcraft-api_db +DB_PORT=3306 +DB_DATABASE=kamilcraft-api +DB_USERNAME=kamilcraft-api +DB_PASSWORD=password + +EXTERNAL_WEBSERVER_PORT=80 +CURRENT_UID=1000 +XDG_CONFIG_HOME=/tmp diff --git a/.gitignore b/.gitignore index eb003b0..ffb5961 100644 --- a/.gitignore +++ b/.gitignore @@ -1,8 +1,10 @@ +/.idea +/.vscode /node_modules -/public/hot /public/storage /storage/*.key /vendor +/.composer .env .env.backup .phpunit.result.cache @@ -11,5 +13,3 @@ Homestead.json Homestead.yaml npm-debug.log yarn-error.log -/.idea -/.vscode diff --git a/README.md b/README.md index fcd60a2..0e3fb1c 100644 --- a/README.md +++ b/README.md @@ -10,9 +10,9 @@ API for kamilcraft.com projects ### Optional -* PHP 8.0 or later -* Composer 2.3.x or later -* Nodejs 16.14.x or later +* PHP 8.1.x or later +* Composer 2.4.x or later +* Nodejs 18.14.x or later ## Preparation and installation @@ -21,9 +21,9 @@ API for kamilcraft.com projects cp .env.example .env ``` -2) Build the image needed for Laravel +2) Build the image needed for Laravel and Node.js ```shell - docker-compose build + docker-compose build --no-cache --pull ``` 3) Run the images prepared in ``docker-compose.yml`` @@ -31,11 +31,23 @@ API for kamilcraft.com projects docker-compose up -d ``` -4) Install the dependencies needed for Laravel and Nodejs. \ - **The installer for Laravel generates the key and migrates the database.** \ - **In the case of Nodejs, it generates page styles.** +4) Install the dependencies needed for Laravel and Nodejs ```shell - docker-compose exec laravel install + docker-compose exec -u "$(id -u):$(id -g)" php composer install ``` - -5) Go to ``http://localhost/dashboard`` in your browser. + ```shell + docker-compose run --rm -u "$(id -u):$(id -g)" npm install + ``` + +5) Key and data generation + ```shell + docker-compose exec -u "$(id -u):$(id -g)" php php artisan key:generate + ``` + ```shell + docker-compose exec -u "$(id -u):$(id -g)" php php artisan migrate:fresh --seed + ``` + ```shell + docker-compose run --rm -u "$(id -u):$(id -g)" npm run dev + ``` + +6) Go to ``http://localhost/dashboard`` in your browser. diff --git a/config/docker/dev/laravel/Dockerfile b/config/docker/dev/laravel/Dockerfile deleted file mode 100644 index 8493fdc..0000000 --- a/config/docker/dev/laravel/Dockerfile +++ /dev/null @@ -1,27 +0,0 @@ -FROM php:8.0-fpm - -ARG USER_UID -ARG USER_NAME -ENV COMPOSER_HOME=/home/$USER_NAME/.composer - -RUN useradd -G www-data,root -u $USER_UID -d /home/$USER_NAME $USER_NAME -RUN mkdir -p /home/$USER_NAME/.composer && \ - chown $USER_NAME:$USER_NAME -R /home/$USER_NAME -RUN set -eux \ - && apt-get update \ - && apt-get upgrade -y \ - && apt-get install git zip unzip dos2unix -y -RUN curl -sS https://getcomposer.org/installer \ - | php -- --version=2.3.5 --install-dir=/usr/local/bin --filename=composer -RUN curl -sL https://deb.nodesource.com/setup_16.x | bash - -RUN set -eux \ - && apt-get install nodejs -y -RUN npm install --global npm@latest - -COPY ./install.sh /usr/local/bin/install -RUN dos2unix /usr/local/bin/install \ - && chmod +x /usr/local/bin/install - -USER $USER_UID - -EXPOSE 9000 diff --git a/config/docker/dev/laravel/install.sh b/config/docker/dev/laravel/install.sh deleted file mode 100755 index acac9c7..0000000 --- a/config/docker/dev/laravel/install.sh +++ /dev/null @@ -1,28 +0,0 @@ -#!/bin/sh - -if [ ! -d "vendor" ] && [ -f "composer.json" ]; then - echo "" - echo "########################################" - echo "# vendor directory not found... #" - echo "########################################" - composer install \ - && php artisan key:generate - - if [ ! -f "database/database.sqlite" ]; then - touch database/database.sqlite - fi - - php artisan migrate:fresh --seed -fi - -if [ ! -d "node_modules" ] && [ -f "package.json" ]; then - echo "" - echo "########################################" - echo "# node_modules directory not found... #" - echo "########################################" - npm install - npm run dev -fi - -echo "$@" -exec "$@" \ No newline at end of file diff --git a/docker-compose.yml b/docker-compose.yml index 3c23032..7b352ec 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -6,28 +6,67 @@ services: container_name: kamilcraft-api_www working_dir: /application ports: - - "80:80" + - ${EXTERNAL_WEBSERVER_PORT:-80}:80 volumes: + - ./environment/dev/nginx/default.conf:/etc/nginx/conf.d/default.conf - .:/application - - ./config/docker/dev/nginx/default.conf:/etc/nginx/conf.d/default.conf - links: - - laravel networks: - - localnet + - kamilcraft + depends_on: + - php + - db - laravel: - build: - args: - USER_UID: ${USER_UID} - USER_NAME: ${USER_NAME} - context: ./config/docker/dev/laravel - container_name: kamilcraft-api_laravel - working_dir: /application - volumes: - - .:/application - networks: - - localnet + php: + build: + context: environment/dev/php + container_name: kamilcraft-api_php + working_dir: /application + user: ${CURRENT_UID:-1000} + volumes: + - .:/application + - ./environment/dev/php/php.ini:/usr/local/etc/php/conf.d/php.ini + networks: + - kamilcraft + extra_hosts: + - host.docker.internal:host-gateway + restart: unless-stopped + + npm: + build: + context: environment/dev/npm + container_name: kamilcraft-api_node + working_dir: /application + entrypoint: [ 'npm' ] + ports: + - '3000:3000' + - '3001:3001' + volumes: + - .:/application + networks: + - kamilcraft + + db: + image: mysql:8.0 + container_name: kamilcraft-api_db + ports: + - '${DB_PORT}:3306' + environment: + MYSQL_ROOT_PASSWORD: '${DB_PASSWORD}' + MYSQL_DATABASE: '${DB_DATABASE}' + MYSQL_USER: '${DB_USERNAME}' + MYSQL_PASSWORD: '${DB_PASSWORD}' + MYSQL_ALLOW_EMPTY_PASSWORD: 'yes' + volumes: + - 'mysql-db-data:/var/lib/mysql' + networks: + - kamilcraft + restart: unless-stopped networks: - localnet: + kamilcraft: driver: bridge + +volumes: + mysql-db-data: + name: ib-mysql-data + driver: local diff --git a/config/docker/dev/nginx/default.conf b/environment/dev/nginx/default.conf similarity index 91% rename from config/docker/dev/nginx/default.conf rename to environment/dev/nginx/default.conf index a2d383f..8960db1 100644 --- a/config/docker/dev/nginx/default.conf +++ b/environment/dev/nginx/default.conf @@ -9,7 +9,7 @@ server { } location ~ \.php$ { - fastcgi_pass laravel:9000; + fastcgi_pass php:9000; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name; include fastcgi_params; diff --git a/environment/dev/npm/Dockerfile b/environment/dev/npm/Dockerfile new file mode 100644 index 0000000..8661170 --- /dev/null +++ b/environment/dev/npm/Dockerfile @@ -0,0 +1,3 @@ +FROM node:18-alpine + +RUN npm install -g npm@latest diff --git a/environment/dev/php/Dockerfile b/environment/dev/php/Dockerfile new file mode 100644 index 0000000..2dee213 --- /dev/null +++ b/environment/dev/php/Dockerfile @@ -0,0 +1,34 @@ +FROM php:8.1-fpm-alpine + +ARG XDEBUG_VERSION=3.1.6 +ARG INSTALL_XDEBUG=false + +ARG COMPOSER_VERSION=2.4.4 +ENV COMPOSER_HOME=/application/.composer +ENV COMPOSER_MEMORY_LIMIT=-1 + +RUN if [ ${INSTALL_XDEBUG} = true ]; then \ + apk --no-cache add $PHPIZE_DEPS \ + && pecl install xdebug-${XDEBUG_VERSION} \ + && docker-php-ext-enable xdebug \ +;fi + +RUN apk update && apk upgrade \ + && apk add --no-cache pcre-dev $PHPIZE_DEPS \ + icu-dev \ + zip \ + libzip-dev \ + libpng-dev \ + && curl -sS https://getcomposer.org/installer | php -- --version="${COMPOSER_VERSION}" --install-dir=/usr/local/bin --filename=composer \ + && pecl install redis \ + && docker-php-ext-install \ + mysqli \ + pdo \ + pdo_mysql \ + zip \ + gd \ + bcmath \ + && docker-php-ext-configure \ + zip \ + && docker-php-ext-enable \ + redis diff --git a/environment/dev/php/php.ini b/environment/dev/php/php.ini new file mode 100644 index 0000000..301cfc1 --- /dev/null +++ b/environment/dev/php/php.ini @@ -0,0 +1,9 @@ +[PHP] +memory_limit = 1G + +[xdebug] +xdebug.client_host=host.docker.internal +xdebug.client_port=9003 +xdebug.mode=debug +xdebug.start_with_request=yes +xdebug.log_level=0