From fd17441e91f5a534408ad495c270405392fc8708 Mon Sep 17 00:00:00 2001 From: Alex Thomassen Date: Thu, 2 Apr 2020 15:38:38 +0200 Subject: [PATCH] #Dockerize it --- .docker/.gitignore | 1 + .docker/Dockerfile | 36 +++++++++++ .docker/README.md | 42 +++++++++++++ .docker/docker-compose.example.yml | 49 +++++++++++++++ .docker/nginx/.gitignore | 2 + .docker/nginx/hovedprosjekt.example.conf | 77 ++++++++++++++++++++++++ .env.example | 6 +- 7 files changed, 210 insertions(+), 3 deletions(-) create mode 100644 .docker/.gitignore create mode 100644 .docker/Dockerfile create mode 100644 .docker/README.md create mode 100644 .docker/docker-compose.example.yml create mode 100644 .docker/nginx/.gitignore create mode 100644 .docker/nginx/hovedprosjekt.example.conf diff --git a/.docker/.gitignore b/.docker/.gitignore new file mode 100644 index 0000000..1120be9 --- /dev/null +++ b/.docker/.gitignore @@ -0,0 +1 @@ +docker-compose.yml diff --git a/.docker/Dockerfile b/.docker/Dockerfile new file mode 100644 index 0000000..c8865a7 --- /dev/null +++ b/.docker/Dockerfile @@ -0,0 +1,36 @@ +FROM php:7.4-fpm + +# Thanks to: https://www.digitalocean.com/community/tutorials/how-to-containerize-a-laravel-application-for-development-with-docker-compose-on-ubuntu-18-04 + +# Arguments defined in docker-compose.yml +ARG user +ARG uid + +# Install system dependencies +RUN apt-get update && apt-get install -y \ + git \ + curl \ + libpng-dev \ + libonig-dev \ + libxml2-dev \ + zip \ + unzip + +# Clear cache +RUN apt-get clean && rm -rf /var/lib/apt/lists/* + +# Install PHP extensions +RUN docker-php-ext-install pdo_mysql mbstring exif pcntl bcmath gd + +# Get latest Composer +COPY --from=composer:latest /usr/bin/composer /usr/bin/composer + +# Create system user to run Composer and Artisan Commands +RUN useradd -G www-data,root -u $uid -d /home/$user $user +RUN mkdir -p /home/$user/.composer && \ + chown -R $user:$user /home/$user + +# Set working directory +WORKDIR /var/www + +USER $user diff --git a/.docker/README.md b/.docker/README.md new file mode 100644 index 0000000..bfe4926 --- /dev/null +++ b/.docker/README.md @@ -0,0 +1,42 @@ +# Application download & Docker setup + +## Requirements + +- Docker +- Docker Compose CLI +- Git +- A text editor, like `nano` + +## Project repository setup + +1. Download the project repository via Git: `git clone https://git.alex.lol/FLOT/HovedprosjektWebApp.git /srv/FLOTWebApp` +2. Navigate to the repository directory: `cd /srv/FLOTWebApp` +3. Copy `.env.example` to `.env`. Leave the `.env` file as is for now. +4. Navigate to the `.docker` directory in the repository. +5. Copy `docker-compose.example.yml` to `docker-compose.yml`. +6. Edit `docker-compose.yml` (with your text editor) and modify configuration/volumes to match your needs. +7. Copy `nginx/hovedprosjekt.example.conf` to `nginx/hovedprosjekt.conf`. +8. Edit `nginx/hovedprosjekt.conf` and modify domain/TLS configuration to your needs. + - Make sure that the SSL certificate and key uses the path as the volume mapping in `docker-compose.yml`. +8. Edit `../.env` and modify your database credentials to match the ones you set in `docker-compose.yml` + - `DB_HOST` (db) and `DB_PORT` (5432) can generally be left as is +9. Run `docker-compose build app` +10. If built successfully, run `docker-compose up -d` to run all containers in "detached" mode. + +## Application setup - First-time setup only + +These steps should be run after you have run `docker-compose up -d`, as the containers need to be running. + +1. While still being in the `.docker` directory run: `docker-compose exec app composer install` + - This will attach the `app` container and run the following command to install dependencies for the application: `composer install` + - Dependency installation might take a while, so be patient. +2. Then run: `docker-compose exec app php artisan key:generate` to generate an app key that's saved in `.env` + +## Logs [Optional] + +You do not need to look at the logs, but if things aren't working, the logs might point you in the right direction: +> `docker-compose logs -f` will show the logs and display new entries while open. + +### Special thanks + +Special thanks to the DigitalOcean tutorial "[How To Containerize a Laravel Application for Development with Docker Compose on Ubuntu 18.04](https://www.digitalocean.com/community/tutorials/how-to-containerize-a-laravel-application-for-development-with-docker-compose-on-ubuntu-18-04)", which was used as the initial template. diff --git a/.docker/docker-compose.example.yml b/.docker/docker-compose.example.yml new file mode 100644 index 0000000..e66b26a --- /dev/null +++ b/.docker/docker-compose.example.yml @@ -0,0 +1,49 @@ +version: '3.7' + +services: + app: + build: + args: + user: hovedprosjekt + uid: 1000 + context: ./ + dockerfile: Dockerfile + image: hovedprosjekt + container_name: hovedprosjekt-app + restart: unless-stopped + working_dir: /var/www/ + volumes: + - ../:/var/www + networks: + - hovedprosjekt + + db: + image: postgres:12.2-alpine + container_name: hovedprosjekt-db + restart: unless-stopped + environment: + POSTGRES_PASSWORD: FLOTHP_D8jqH4YjS4ajD4 + POSTGRES_USER: hovedprosjekt + POSTGRES_DB: hovedprosjekt + volumes: + - /srv/flot_pgdata:/var/lib/postgresql/data + networks: + - hovedprosjekt + + nginx: + image: nginx:1.17 + container_name: hovedprosjekt-nginx + restart: unless-stopped + ports: + - 80:80 + - 443:443 + volumes: + - ../:/var/www + - ./nginx/:/etc/nginx/conf.d/ + - /srv/ssl:/srv/ssl + networks: + - hovedprosjekt + +networks: + hovedprosjekt: + driver: bridge diff --git a/.docker/nginx/.gitignore b/.docker/nginx/.gitignore new file mode 100644 index 0000000..1af5474 --- /dev/null +++ b/.docker/nginx/.gitignore @@ -0,0 +1,2 @@ +*.conf +!*.example.conf diff --git a/.docker/nginx/hovedprosjekt.example.conf b/.docker/nginx/hovedprosjekt.example.conf new file mode 100644 index 0000000..3e5c03e --- /dev/null +++ b/.docker/nginx/hovedprosjekt.example.conf @@ -0,0 +1,77 @@ +server { + listen 80 default_server; + listen [::]:80 default_server; + index index.php index.html; + + location / { + # Comment this line if not using HTTPS. + return 301 https://$host$request_uri; + + try_files $uri $uri/ /index.php?$query_string; + gzip_static on; + } + + location ~ \.php$ { + try_files $uri =404; + fastcgi_split_path_info ^(.+\.php)(/.+)$; + fastcgi_pass app:9000; + fastcgi_index index.php; + include fastcgi_params; + fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; + fastcgi_param PATH_INFO $fastcgi_path_info; + } + + # Domain name, use `.example.com` for wildcard of `example.com` + server_name .hovedprosjekt.no; +} + +server { + listen 443 ssl http2; + listen [::]:443 ssl http2; + root /var/www/public; + + # Domain name, use `.example.com` for wildcard of `example.com` + server_name .hovedprosjekt.no; + + # TLS certificate configuration + ssl_certificate /srv/ssl/hovedprosjekt/fullchain.pem; + ssl_certificate_key /srv/ssl/hovedprosjekt/key.pem; + + server_tokens off; + + include ssl_params; + + add_header X-Frame-Options "SAMEORIGIN"; + add_header X-XSS-Protection "1; mode=block"; + add_header X-Content-Type-Options "nosniff"; + + index index.html index.htm; + + charset utf-8; + + error_log /var/log/nginx/error.log; + access_log /var/log/nginx/access.log; + root /var/www/public; + + location / { + try_files $uri $uri/ /index.php?$query_string; + gzip_static on; + } + + location ~ \.php$ { + try_files $uri =404; + fastcgi_split_path_info ^(.+\.php)(/.+)$; + fastcgi_pass app:9000; + fastcgi_index index.php; + include fastcgi_params; + fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; + fastcgi_param PATH_INFO $fastcgi_path_info; + } + + location = /favicon.ico { access_log off; log_not_found off; } + location = /robots.txt { access_log off; log_not_found off; } + + location ~ /\.ht { + deny all; + } +} diff --git a/.env.example b/.env.example index 41b2646..cad6b7f 100644 --- a/.env.example +++ b/.env.example @@ -6,9 +6,9 @@ APP_URL=http://localhost LOG_CHANNEL=stack -DB_CONNECTION=mysql -DB_HOST=127.0.0.1 -DB_PORT=3306 +DB_CONNECTION=pgsql +DB_HOST=db +DB_PORT=5432 DB_DATABASE=laravel DB_USERNAME=root DB_PASSWORD=