Skip to content

Commit

Permalink
Add docker compose setup (JhumanJ#73)
Browse files Browse the repository at this point in the history
* Add docker compose setup

* Shorten healthcheck interval for faster startup

* Add .env file for docker setup. Take changes in composer/package-lock into account.

* Add readme entry

* JWT force command + readme minor changes

* Udpated readme

---------

Co-authored-by: Julien Nahum <[email protected]>
  • Loading branch information
Philip Gatzka and JhumanJ authored Feb 22, 2023
1 parent 5a1bd0e commit 49d1a23
Show file tree
Hide file tree
Showing 6 changed files with 194 additions and 5 deletions.
8 changes: 8 additions & 0 deletions .env.docker
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
POSTGRES_USER="postgres"
POSTGRES_PASSWORD="postgres"
POSTGRES_DB="postgres"

DB_HOST="database"
DB_DATABASE="postgres"
DB_USERNAME="postgres"
DB_PASSWORD="postgres"
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,5 @@ public/.DS_Store
.env.production
.env.staging
_ide_helper.php

/.make.*
43 changes: 43 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@

up: down .env .make.composer-install .make.npm-install .make.npm-build .make.artisan-key-generate .make.jwt-secret .make.migrate server

down:
docker compose down

clean:
rm ./.make.*

server:
docker compose up -d server

logs:
docker compose logs -f server

.make.composer-install: composer.json composer.lock
docker compose run --rm composer-install
touch $@

.make.npm-install: package.json package-lock.json
docker compose run --rm npm-install
touch $@

.make.npm-build:
docker compose run --rm npm-build
touch $@

.make.artisan-key-generate:
docker compose run --rm php-cli artisan key:generate
touch $@

.make.jwt-secret:
docker compose run --rm php-cli artisan jwt:secret --f
touch $@

.make.migrate:
docker compose run --rm migrate
touch $@

.env:
cp .env.example .env

.PHONY: up down clean server
41 changes: 36 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,18 +33,49 @@ The easiest way to get started with OpnForm is with the [official managed servic

It takes 1 minute to try out the builder for free. You'll have high availability, backups, security, and maintenance all managed for you.

## Self-hosting

🚧 This section is under construction!

### Requirements

- PHP >= 8.0
- MySQL/MariaDB or PostgreSQL
- Node.js and NPM/Yarn/... to compile assets

### Local installation
## Installation


### Docker installation 🐳

There's a `docker compose` setup automating most of the manual steps:

```bash
make up
```

The application is now running on [http://localhost:4000](http://localhost:4000).
Alternatively, you may use the compose setup on its own

```bash
# Start the application
docker compose up -d server

# Run php commands, for example
docker compose run php-cli artisan about

# ...or update npm dependencies
docker compose run node-cli npm ci
```

`make` keeps track of all executed targets using `.make.*` files.
In case you'd like to start from scratch (re-install dependencies, reset jwt
token, run migrations, ...), run

```bash
make clean
```

After that, `make` will re-execute all targets upon the next execution.

### Using Laravel Valet
This section explains how to get started locally with the project. It's most likely relevant if you're trying to work on the project.
First, let's work with the codebase and its dependencies.

```bash
Expand Down
75 changes: 75 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
---

services:
php-cli:
build:
dockerfile: './php-cli.Dockerfile'
entrypoint: [ 'php' ]
volumes:
- '.:/project'
working_dir: '/project'
user: '1000'

node-cli:
image: 'node:16'
volumes:
- '.:/project'
working_dir: '/project'
user: '1000'

composer-cli:
build:
dockerfile: './php-cli.Dockerfile'
entrypoint: [ 'composer' ]
volumes:
- '.:/project'
working_dir: '/project'
user: '1000'

composer-install:
extends:
service: composer-cli
command: [ 'install' ]

npm-install:
extends:
service: node-cli
entrypoint: [ 'npm' ]
command: [ 'clean-install' ]

npm-build:
extends:
service: node-cli
entrypoint: [ 'npm', 'run' ]
command: [ 'build' ]

database:
image: 'postgres:15'
restart: 'unless-stopped'
env_file: '.env.docker'
healthcheck:
test: [ 'CMD-SHELL', 'pg_isready', '-d', 'postgres' ]
start_period: 5s
interval: 5s
timeout: 10s
retries: 3

migrate:
extends:
service: php-cli
depends_on:
database:
condition: service_healthy
env_file: '.env.docker'
command: [ 'artisan', 'migrate' ]

server:
extends:
service: php-cli
depends_on:
migrate:
condition: service_completed_successfully
env_file: '.env.docker'
command: [ '-S', '0.0.0.0:4000', '-t', './public']
ports:
- '4000:4000'
30 changes: 30 additions & 0 deletions php-cli.Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# syntax=docker/dockerfile:1.3-labs
FROM php:8.1-cli

COPY --from=mlocati/php-extension-installer /usr/bin/install-php-extensions /usr/bin/
COPY --from=composer:2 /usr/bin/composer /usr/bin/composer

RUN <<EOF
install-php-extensions \
apcu \
bcmath \
bz2 \
calendar \
ffi \
gd \
gmp \
imagick \
intl \
mysqli \
pcntl \
pcov \
pdo_mysql \
pdo_pgsql \
redis \
soap \
sockets \
sodium \
xsl \
zip \
exif
EOF

0 comments on commit 49d1a23

Please sign in to comment.