From f88495f4a2e2f7525881dfcd1c6cbc239c3ed7a1 Mon Sep 17 00:00:00 2001
From: Christoph Reiter <reiter.christoph@gmail.com>
Date: Thu, 29 Oct 2020 15:55:51 +0100
Subject: [PATCH] docker-compose setup

---
 .env                                          |  2 +-
 docker-compose/README.md                      | 31 ++++++++++++
 docker-compose/docker-compose.yml             | 43 ++++++++++++++++
 docker-compose/php-fpm/Dockerfile             | 49 +++++++++++++++++++
 docker-compose/php-fpm/php-fpm-overrides.conf | 32 ++++++++++++
 docker-compose/php-fpm/php-ini-overrides.ini  |  5 ++
 docker-compose/run                            |  3 ++
 docker-compose/webserver/nginx.conf           | 26 ++++++++++
 8 files changed, 190 insertions(+), 1 deletion(-)
 create mode 100644 docker-compose/README.md
 create mode 100644 docker-compose/docker-compose.yml
 create mode 100644 docker-compose/php-fpm/Dockerfile
 create mode 100644 docker-compose/php-fpm/php-fpm-overrides.conf
 create mode 100644 docker-compose/php-fpm/php-ini-overrides.ini
 create mode 100755 docker-compose/run
 create mode 100644 docker-compose/webserver/nginx.conf

diff --git a/.env b/.env
index cef47e8..b20d503 100644
--- a/.env
+++ b/.env
@@ -39,4 +39,4 @@ APP_BUILDINFO=unknown  # a git hash or something identifying the build
 APP_BUILDINFO_URL='#'
 
 # Disable the webserver bundle (avoids deprecation warnings). Use docker instead.
-DISABLE_DEV_SERVER=false
\ No newline at end of file
+DISABLE_DEV_SERVER=true
\ No newline at end of file
diff --git a/docker-compose/README.md b/docker-compose/README.md
new file mode 100644
index 0000000..cbc07d2
--- /dev/null
+++ b/docker-compose/README.md
@@ -0,0 +1,31 @@
+# Running with Docker
+
+This is simple docker-compose setup using docker with php-fpm and nginx to serve
+the symfony app.
+
+## Setup
+
+* `sudo apt install docker-compose` to install docker-compose on Debian/Ubuntu
+
+## Running the Server
+
+* `sudo docker-compose up -d` to start the server in the background
+* `sudo docker-compose up` to start the server in the foreground with logs
+* `sudo docker-compose stop` to stop the server
+* `sudo docker-compose logs -f` to see the logs
+* `sudo docker-compose restart messenger-worker` restart messenger worker for updating message handling code
+
+## Executing Symfony commands
+
+* `./run` to start a shell in the php-fpm container.
+* After this you can run symfony/composer commands like:
+  * `./bin/console debug:config`
+  * `composer install`
+  * `composer test`
+* You can also call `bin/console` commands directly from outside the container with `./console debug:config`
+  if you have all dependencies available on the host.
+
+## Links
+
+* Open <http://127.0.0.1:8000/> for the API Platform webpage
+* Open <http://localhost:8101/> for redis commander
diff --git a/docker-compose/docker-compose.yml b/docker-compose/docker-compose.yml
new file mode 100644
index 0000000..ce62bc6
--- /dev/null
+++ b/docker-compose/docker-compose.yml
@@ -0,0 +1,43 @@
+version: "3.1"
+
+services:
+  webserver:
+    image: nginx:alpine
+    working_dir: /application
+    volumes:
+      - ..:/application
+      - ./webserver/nginx.conf:/etc/nginx/conf.d/default.conf
+    ports:
+      - "8000:80"
+
+  php-fpm:
+    environment:
+      - TERM_PROGRAM=Hyper  # force colors for the symfony dumper
+    build:
+      context: php-fpm
+    working_dir: /application
+    volumes:
+      - ..:/application
+      - ./php-fpm/php-ini-overrides.ini:/etc/php/7.3/fpm/conf.d/99-overrides.ini
+      - ./php-fpm/php-fpm-overrides.conf:/etc/php/7.3/fpm/pool.d/z-overrides.conf
+
+  messenger-worker:
+    environment:
+      - TERM_PROGRAM=Hyper  # force colors for the symfony dumper
+    build:
+      context: php-fpm
+    working_dir: /application
+    entrypoint: ./bin/console messenger:consume async
+    volumes:
+      - ..:/application
+      - ./php-fpm/php-ini-overrides.ini:/etc/php/7.3/cli/conf.d/99-overrides.ini
+
+  redis:
+    image: "redis"
+
+  redis-commander:
+    image: "rediscommander/redis-commander"
+    environment:
+      - REDIS_HOST=redis
+    ports:
+      - "8101:8081"
\ No newline at end of file
diff --git a/docker-compose/php-fpm/Dockerfile b/docker-compose/php-fpm/Dockerfile
new file mode 100644
index 0000000..b2e792c
--- /dev/null
+++ b/docker-compose/php-fpm/Dockerfile
@@ -0,0 +1,49 @@
+FROM debian:buster
+
+ENV DEBIAN_FRONTEND=noninteractive
+
+# Basics
+RUN apt-get update && apt-get install -y wget lsb-release
+
+# Install PHP and the rest
+# Debian Buster only has php-redis 4.2, we need 4.3
+RUN wget -O /etc/apt/trusted.gpg.d/php.gpg https://packages.sury.org/php/apt.gpg \
+    && echo "deb https://packages.sury.org/php/ $(lsb_release -sc) main" > /etc/apt/sources.list.d/php.list \
+    && apt-get update \
+    && apt-get -y --no-install-recommends install \
+        ca-certificates \
+        curl \
+        unzip \
+        sudo \
+        git \
+        php-apcu \
+        php-apcu-bc \
+        php7.3-cli \
+        php7.3-curl \
+        php7.3-soap \
+        php7.3-json \
+        php7.3-mbstring \
+        php7.3-opcache \
+        php7.3-readline \
+        php7.3-xml \
+        php7.3-zip \
+        php7.3-redis \
+        php7.3-fpm \
+        php7.3-ldap \
+        php7.3-gmp \
+        php7.3-xdebug \
+        composer \
+    && apt-get clean \
+    && rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/*
+
+STOPSIGNAL SIGQUIT
+
+RUN useradd -u 1000 -ms /bin/bash user
+RUN echo 'user ALL=(ALL) NOPASSWD:ALL' >> /etc/sudoers
+USER user
+
+RUN git config --global url."https://gitlab.tugraz.at/".insteadOf "git@gitlab.tugraz.at:"
+
+CMD ["/usr/sbin/php-fpm7.3", "-O" ]
+EXPOSE 9000
+WORKDIR "/application"
\ No newline at end of file
diff --git a/docker-compose/php-fpm/php-fpm-overrides.conf b/docker-compose/php-fpm/php-fpm-overrides.conf
new file mode 100644
index 0000000..8c9ddcc
--- /dev/null
+++ b/docker-compose/php-fpm/php-fpm-overrides.conf
@@ -0,0 +1,32 @@
+[global]
+; Override default pid file
+pid = /home/user/.php-fpm.pid
+
+; Avoid logs being sent to syslog
+error_log = /proc/self/fd/2
+
+; Set this to php default's max_execution_time to allow children to stop gracefully when fpm is commanded to stop
+; This helps avoiding 502's
+process_control_timeout = 30
+
+; Do not daemonize (eg send process to the background)
+daemonize = no
+
+; Don't print anything when started
+log_level = warning
+
+[www]
+; Access from webserver container is via network, not socket file
+listen = [::]:9000
+
+; Redirect logs to stdout - FPM closes /dev/std* on startup
+access.log = /proc/self/fd/2
+access.format = "[%{%Y-%m-%d %H:%M:%S}t] %m %{REQUEST_SCHEME}e://%{HTTP_HOST}e%{REQUEST_URI}e CODE=%s"
+
+catch_workers_output = yes
+
+; Remove "pool www" decoration from log output
+decorate_workers_output = no
+
+; This option sets the limit on the number of simultaneous requests that will be served
+pm.max_children = 10
\ No newline at end of file
diff --git a/docker-compose/php-fpm/php-ini-overrides.ini b/docker-compose/php-fpm/php-ini-overrides.ini
new file mode 100644
index 0000000..6c3555f
--- /dev/null
+++ b/docker-compose/php-fpm/php-ini-overrides.ini
@@ -0,0 +1,5 @@
+upload_max_filesize = 200M
+post_max_size = 208M
+memory_limit = 450M
+zend.assertions = 1
+date.timezone = "Europe/Vienna"
diff --git a/docker-compose/run b/docker-compose/run
new file mode 100755
index 0000000..79363b1
--- /dev/null
+++ b/docker-compose/run
@@ -0,0 +1,3 @@
+#!/bin/sh
+
+docker-compose run --rm --entrypoint bash php-fpm
diff --git a/docker-compose/webserver/nginx.conf b/docker-compose/webserver/nginx.conf
new file mode 100644
index 0000000..e620861
--- /dev/null
+++ b/docker-compose/webserver/nginx.conf
@@ -0,0 +1,26 @@
+server {
+    listen 80 default;
+
+    client_max_body_size 108M;
+
+    access_log /var/log/nginx/application.access.log;
+
+
+    root /application/public;
+    index index.php;
+
+    if (!-e $request_filename) {
+        rewrite ^.*$ /index.php last;
+    }
+
+    location ~ (\.php|/)$ {
+        fastcgi_pass php-fpm:9000;
+        fastcgi_index index.php;
+        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
+        fastcgi_param PHP_VALUE "error_log=/var/log/nginx/application_php_errors.log";
+        fastcgi_buffers 16 16k;
+        fastcgi_buffer_size 32k;
+        include fastcgi_params;
+    }
+    
+}
-- 
GitLab