Skip to content

Commit

Permalink
Docker setup
Browse files Browse the repository at this point in the history
  • Loading branch information
NickMous committed Aug 22, 2024
1 parent 6632b85 commit 7eed231
Show file tree
Hide file tree
Showing 16 changed files with 334 additions and 56 deletions.
42 changes: 42 additions & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
# Include any files or directories that you don't want to be copied to your
# container here (e.g., local build artifacts, temporary files, etc.).
#
# For more help, visit the .dockerignore file reference guide at
# https://docs.docker.com/go/build-context-dockerignore/

**/.classpath
**/.ddev
**/.dockerignore
**/.git
**/.gitignore
**/.project
**/.settings
**/.toolstarget
**/.vs
**/.vscode
**/.next
**/.cache
**/*.*proj.user
**/*.dbmdl
**/*.jfm
**/charts
**/docker-compose*
**/compose.y*ml
!**/composer.json
!**/composer.lock
**/Dockerfile*
**/node_modules
**/npm-debug.log
**/obj
**/secrets.dev.yaml
**/values.dev.yaml
**/vendor
.editorconfig
.gitattributes
.prettierignore
.prettierrc
eslint.config.js
phpstan.neon
LICENSE
README.md
README.Docker.md
2 changes: 1 addition & 1 deletion .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,5 @@ trim_trailing_whitespace = false
[*.{yml,yaml}]
indent_size = 2

[docker-compose.yml]
[{docker-compose.yml, docker-compose.*.yml}]
indent_size = 4
12 changes: 6 additions & 6 deletions .env.example
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
APP_NAME=Laravel
APP_NAME=CycleQuest
APP_ENV=local
APP_KEY=
APP_DEBUG=true
APP_TIMEZONE=UTC
APP_URL=http://localhost
APP_URL=http://localhost:1501

APP_LOCALE=en
APP_FALLBACK_LOCALE=en
APP_FAKER_LOCALE=en_US
APP_LOCALE=nl
APP_FALLBACK_LOCALE=nl
APP_FAKER_LOCALE=nl_NL

APP_MAINTENANCE_DRIVER=file
APP_MAINTENANCE_STORE=database
Expand All @@ -22,7 +22,7 @@ LOG_LEVEL=debug
DB_CONNECTION=mariadb
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=cyclequest_lvl11
DB_DATABASE=db
DB_USERNAME=root
DB_PASSWORD=

Expand Down
60 changes: 22 additions & 38 deletions .github/workflows/laravel.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,53 +2,37 @@ name: Laravel Pest Tests

on:
pull_request:
branches: ["main"]
branches: [ "main" ]

jobs:
laravel-tests:
runs-on: ubuntu-latest

services:
mariadb:
image: mariadb:10.11
env:
MYSQL_ROOT_PASSWORD: root_password
MYSQL_DATABASE: test
MYSQL_USER: test_user
MYSQL_PASSWORD: test_password
docker:
image: docker
options: --privileged
ports:
- 3306:3306
options: >-
--health-cmd="mysqladmin ping --silent"
--health-interval=10s
--health-timeout=5s
--health-retries=3

steps:
- uses: shivammathur/setup-php@15c43e89cdef867065b0213be354c2841860869e
- name: Checkout code
uses: actions/checkout@v3
- name: Login to Docker Hub
uses: docker/login-action@v3
with:
php-version: "8.3"
- uses: actions/checkout@v3
- name: Copy .env
run: php -r "file_exists('.env') || copy('.env.example', '.env');"
- name: Install Composer Dependencies
run: composer install -q --no-ansi --no-interaction --no-scripts --no-progress --prefer-dist
- name: Install NPM Dependencies
run: npm install
- name: Run build
run: npm run build
- name: Generate key
run: php artisan key:generate
- name: Directory Permissions
run: chmod -R 777 storage bootstrap/cache
- name: Create Database
username: ${{ vars.DOCKER_USERNAME }}
password: ${{ secrets.DOCKERHUB_TOKEN }}
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3
- name: Copy password file
run: cp database/password.txt.example database/password.txt
- name: Set up Docker Compose
run: |
mkdir -p database
touch database/database.sqlite
- name: Execute tests (Unit and Feature tests) via Pest
env:
DB_CONNECTION: mariadb
DB_DATABASE: test
DB_USERNAME: test_user
DB_PASSWORD: test_password
run: vendor/bin/pest
docker-compose -f compose.testing.yaml up -d
- name: Run tests
run: |
docker-compose -f compose.testing.yaml --env-file .env exec server-testing ./vendor/bin/pest tests
- name: Tear down Docker Compose
run: |
docker-compose -f compose.testing.yaml down
76 changes: 76 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
# syntax=docker/dockerfile:1
FROM composer:lts as deps-composer-base
WORKDIR /app
COPY artisan /app/
COPY app /app/app/
COPY bootstrap /app/bootstrap/
COPY config /app/config/
COPY database /app/database/
COPY lang /app/lang/
COPY public /app/public/
COPY resources /app/resources/
COPY routes /app/routes/
COPY storage /app/storage/

FROM deps-composer-base as deps-composer-prod
RUN --mount=type=bind,source=composer.json,target=composer.json \
--mount=type=bind,source=composer.lock,target=composer.lock \
--mount=type=cache,target=/tmp/cache \
composer install --no-interaction

FROM deps-composer-base as deps-composer-dev
RUN --mount=type=bind,source=composer.json,target=composer.json \
--mount=type=bind,source=composer.lock,target=composer.lock \
--mount=type=cache,target=/tmp/cache \
composer install --no-interaction

FROM node:latest as deps-node-base
WORKDIR /app
COPY package.json /app/
COPY package-lock.json /app/
COPY vite.config.js /app/
COPY tailwind.config.js /app/
COPY postcss.config.js /app/

FROM deps-node-base as deps-node-prod
COPY --from=deps-composer-prod /app /app
RUN npm install --production
RUN npm run build

FROM deps-node-base as deps-node-dev
COPY --from=deps-composer-dev /app /app
RUN npm install
RUN npm run build

FROM php:8.3-apache as base
RUN docker-php-ext-install pdo_mysql
COPY .env /var/www/html/.env

# This is for tinker
RUN mkdir /var/www/.config && chown -R www-data:www-data /var/www/.config
ENV APACHE_DOCUMENT_ROOT /var/www/html/public
RUN sed -ri -e 's!/var/www/html!${APACHE_DOCUMENT_ROOT}!g' /etc/apache2/sites-available/*.conf
RUN sed -ri -e 's!/var/www/!${APACHE_DOCUMENT_ROOT}!g' /etc/apache2/apache2.conf /etc/apache2/conf-available/*.conf
RUN a2enmod rewrite


FROM base as prod
RUN mv "$PHP_INI_DIR/php.ini-production" "$PHP_INI_DIR/php.ini"
COPY --from=deps-composer-prod /app /var/www/html
COPY --from=deps-node-prod /app /var/www/html
COPY composer.json /var/www/html/composer.json
RUN chown -R www-data:www-data /var/www/html
USER www-data
RUN php /var/www/html/artisan optimize
RUN php /var/www/html/artisan config:cache

FROM base as dev
RUN mv "$PHP_INI_DIR/php.ini-development" "$PHP_INI_DIR/php.ini"
COPY --from=deps-composer-dev /app /var/www/html
COPY --from=deps-node-dev /app /var/www/html
COPY composer.json /var/www/html/composer.json
COPY tests /var/www/html/tests/
COPY phpunit.xml /var/www/html/phpunit.xml
RUN chown -R www-data:www-data /var/www/html

FROM prod as test
22 changes: 22 additions & 0 deletions README.Docker.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
### Building and running your application

When you're ready, start your application by running:
`docker compose up --build`.

Your application will be available at http://localhost:1501.

### PHP extensions
If your application requires specific PHP extensions to run, they will need to be added to the Dockerfile. Follow the instructions and example in the Dockerfile to add them.

### Deploying your application to the cloud

First, build your image, e.g.: `docker build -t myapp .`.
If your cloud uses a different CPU architecture than your development
machine (e.g., you are on a Mac M1 and your cloud provider is amd64),
you'll want to build the image for that platform, e.g.:
`docker build --platform=linux/amd64 -t myapp .`.

Then, push it to your registry, e.g. `docker push myregistry.com/myapp`.

Consult Docker's [getting started](https://docs.docker.com/go/get-started-sharing/)
docs for more detail on building and pushing.
41 changes: 41 additions & 0 deletions compose.development.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
services:
mailpit:
image: axllent/mailpit
container_name: mailpit
restart: unless-stopped
volumes:
- ./data:/data
ports:
- "8025:8025"
- "1025:1025"
depends_on:
server:
condition: service_started
environment:
MP_MAX_MESSAGES: 5000
MP_DATABASE: /data/mailpit.db
MP_SMTP_AUTH_ACCEPT_ANY: 1
MP_SMTP_AUTH_ALLOW_INSECURE: 1

server:
build:
context: .
secrets:
- db-password
target: dev
extends:
file: compose.yaml
service: server

db:
environment:
- MARIADB_DATABASES="db db_test"
extends:
file: compose.yaml
service: db

volumes:
db-data:
secrets:
db-password:
file: database/password.txt
58 changes: 58 additions & 0 deletions compose.testing.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
services:
server-testing:
build:
context: .
secrets:
- db-password
target: dev
ports:
- 1501:80
- 22:22
depends_on:
db-testing:
condition: service_healthy
secrets:
- db-password
environment:
- DB_PASSWORD_FILE=/run/secrets/db-password
- DB_HOST=db-testing
- DB_PORT=3306
- DB_DATABASE=db_test
- DB_USERNAME=root
- APP_ENV=testing
- APP_MAINTENANCE_DRIVER=file
- BCRYPT_ROUNDS=4
- CACHE_STORE=file
- MAIL_MAILER=array
- PULSE_ENABLED=false
- QUEUE_CONNECTION=sync
- SESSION_DRIVER=array
- TELESCOPE_ENABLED=false
env_file:
- .env

db-testing:
image: mariadb
restart: always
user: root
secrets:
- db-password
volumes:
- db-data-testing:/var/lib/mysql
environment:
- MARIADB_ROOT_PASSWORD_FILE=/run/secrets/db-password
- MARIADB_DATABASE=db_testing
expose:
- 3306
healthcheck:
test: [ "CMD", "/usr/local/bin/healthcheck.sh", "--su-mysql", "--connect", "--innodb_initialized" ]
interval: 10s
timeout: 5s
retries: 5

volumes:
db-data-testing:
secrets:
db-password:
file: database/password.txt

Loading

0 comments on commit 7eed231

Please sign in to comment.