Skip to content

Commit

Permalink
Add Docker container (osmbe#770)
Browse files Browse the repository at this point in the history
* Create Dockerfile

* Create .dockerignore

* Create composer.sh

* Create apache.conf

* Update INSTALL.md

* Create docker.yml

* Update docker.yml

* Rename apache.conf to apache/app.conf

* Update INSTALL.md

* Update Dockerfile

* Update INSTALL.md

* Create welcome-update.sh

* Update Dockerfile
  • Loading branch information
jbelien authored Oct 16, 2022
1 parent e8c67ff commit 74a400d
Show file tree
Hide file tree
Showing 7 changed files with 206 additions and 1 deletion.
34 changes: 34 additions & 0 deletions .docker/apache/app.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<VirtualHost *:80>
# The ServerName directive sets the request scheme, hostname and port that
# the server uses to identify itself. This is used when creating
# redirection URLs. In the context of virtual hosts, the ServerName
# specifies what hostname must appear in the request's Host: header to
# match this virtual host. For the default virtual host (this file) this
# value is not decisive as it is used as a last resort host regardless.
# However, you must set it for any further virtual host explicitly.
#ServerName www.example.com

ServerAdmin webmaster@localhost
DocumentRoot /var/www/app/public

<Directory "/var/www/app/public">
Require all granted
AllowOverride All
</Directory>

# Available loglevels: trace8, ..., trace1, debug, info, notice, warn,
# error, crit, alert, emerg.
# It is also possible to configure the loglevel for particular
# modules, e.g.
#LogLevel info ssl:warn

ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined

# For most configuration files from conf-available/, which are
# enabled or disabled at a global level, it is possible to
# include a line for only one particular virtual host. For example the
# following line enables the CGI configuration for this host only
# after it has been globally disabled with "a2disconf".
#Include conf-available/serve-cgi-bin.conf
</VirtualHost>
17 changes: 17 additions & 0 deletions .docker/composer.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#!/bin/sh

EXPECTED_CHECKSUM="$(php -r 'copy("https://composer.github.io/installer.sig", "php://stdout");')"
php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');"
ACTUAL_CHECKSUM="$(php -r "echo hash_file('sha384', 'composer-setup.php');")"

if [ "$EXPECTED_CHECKSUM" != "$ACTUAL_CHECKSUM" ]
then
>&2 echo 'ERROR: Invalid installer checksum'
rm composer-setup.php
exit 1
fi

php composer-setup.php --quiet
RESULT=$?
rm composer-setup.php
exit $RESULT
4 changes: 4 additions & 0 deletions .docker/cron.daily/welcome-update.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#!/bin/sh

cd /var/www/osm-welcome-tool/
php bin/console welcome:update
7 changes: 7 additions & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
.github/
docs/
node_modules/
public/build/
var/
vendor/
.env.*
44 changes: 44 additions & 0 deletions .github/workflows/docker.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
name: Docker Image CI

on:
push:
branches: [ 2.x ]
pull_request:
branches: [ 2.x ]

env:
REGISTRY: ghcr.io
IMAGE_NAME: ${{ github.repository }}

jobs:

build:

runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v3

- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v2

- name: Log in to the Container registry
uses: docker/login-action@v2
if: github.event_name != 'pull_request'
with:
registry: ${{ env.REGISTRY }}
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}

- name: Extract metadata (tags, labels) for Docker
id: meta
uses: docker/metadata-action@v4
with:
images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}

- name: Build and push Docker image
uses: docker/build-push-action@v3
with:
context: .
push: ${{ github.event_name != 'pull_request' }}
tags: ${{ steps.meta.outputs.tags }}
85 changes: 85 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
# Node.js (Build assets)

FROM node:16-alpine as node

WORKDIR /assets

COPY . .

RUN npm install
RUN npm run build

# Composer

FROM composer:2 as composer

# Application

FROM php:8.1-apache as app

## Install PHP dependencies

RUN apt-get update -y && apt-get upgrade -y
RUN apt-get install -y libicu-dev libpq-dev libsodium-dev libzip-dev

RUN docker-php-ext-configure zip;
RUN docker-php-ext-install -j$(nproc) intl pdo_pgsql sodium zip

## Install Symfony CLI

# RUN apk add --no-cache bash
# RUN curl -1sLf 'https://dl.cloudsmith.io/public/symfony/stable/setup.alpine.sh' | bash
# RUN apk add symfony-cli

## Configure Apache

COPY ".docker/apache/app.conf" "/etc/apache2/sites-available/"

RUN a2enmod rewrite alias
RUN a2dissite 000-default
RUN a2ensite app
RUN apache2ctl restart

## Copy/Clean files

ENV APP_ENV=prod
# ENV APP_ENV=dev

WORKDIR "/var/www/app"

COPY --chown=www-data . .
COPY .docker/cron.daily/welcome-update.sh /etc/cron.daily/welcome-update

RUN rm -Rf .docker/
RUN rm -Rf assets/

COPY --from=node --chown=www-data "/assets/public/build" "./public/build"

## Install Composer & Dependencies

COPY --from=composer "/usr/bin/composer" "/usr/bin/composer"

ENV COMPOSER_ALLOW_SUPERUSER=1

RUN composer validate
RUN composer install --no-ansi --no-interaction --no-progress --prefer-dist --optimize-autoloader --no-scripts --no-dev
# RUN composer install --no-ansi --no-interaction --no-progress --prefer-dist --optimize-autoloader --no-scripts
RUN composer clear-cache
RUN composer dump-env prod
RUN composer run-script post-install-cmd --no-dev
# RUN composer run-script post-install-cmd
RUN chmod +x bin/console; sync;

###> recipes ###
###< recipes ###

## Check Symfony requirements

# RUN symfony check:requirements

## Finalize

RUN mkdir -p var/cache/${APP_ENV} var/log/
RUN chown -R www-data:www-data var/

EXPOSE 80
16 changes: 15 additions & 1 deletion INSTALL.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,23 +8,37 @@
composer install
```

- Create database:
- Create database (if needed):

```cmd
php bin/console doctrine:database:create
```

- Initialize schema:

```cmd
php bin/console doctrine:schema:create
```

- Create `OSMCHA_API_KEY` in your environment with your [OSMCha](https://osmcha.org/) API key

## Run locally

### Symfony Local Web Server

- Install [Symfony CLI](https://symfony.com/download)
- Run `symfony server:start`
- Browse the given URL

Check [Symfony local server documentation](https://symfony.com/doc/current/setup/symfony_server.html) for more information.

### Docker

```cmd
docker build . --tag osm-welcome-tool
docker run --detach --publish 80:80 --env-file .env.local osm-welcome-tool
```

## Deploy

Check [Symfony deployment documentation](https://symfony.com/doc/current/deployment.html).

0 comments on commit 74a400d

Please sign in to comment.