Skip to content

Commit

Permalink
Merge pull request #13 from karmek-k/dev
Browse files Browse the repository at this point in the history
Deployment preparation with docker-compose
  • Loading branch information
karmek-k authored Jun 22, 2021
2 parents 466e204 + 3fd82d5 commit ca5eea5
Show file tree
Hide file tree
Showing 14 changed files with 153 additions and 173 deletions.
4 changes: 2 additions & 2 deletions .env
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ APP_SECRET=80021d294317eab0437313a51a05d31b
# Format described at https://www.doctrine-project.org/projects/doctrine-dbal/en/latest/reference/configuration.html#connecting-using-a-url
# IMPORTANT: You MUST configure your server version, either here or in config/packages/doctrine.yaml
#
DATABASE_URL="sqlite:///%kernel.project_dir%/var/data.db"
# DATABASE_URL="sqlite:///%kernel.project_dir%/var/data.db"
# DATABASE_URL="mysql://db_user:[email protected]:3306/db_name?serverVersion=5.7"
# DATABASE_URL="postgresql://db_user:[email protected]:5432/db_name?serverVersion=13&charset=utf8"
DATABASE_URL="postgresql://db_user:[email protected]:5432/db_name?serverVersion=13&charset=utf8"
###< doctrine/doctrine-bundle ###
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,5 @@
npm-debug.log
yarn-error.log
###< symfony/webpack-encore-bundle ###

db.env
2 changes: 2 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,7 @@ FROM php:8.0-fpm

WORKDIR /app
COPY --from=composer /app .
RUN apt-get update && apt-get install -y libpq-dev
RUN docker-php-ext-install pgsql pdo pdo_pgsql
ENV APP_ENV=prod
EXPOSE 9000
44 changes: 38 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,43 @@

## Installation

### Docker (PHP-FPM)
### Docker Compose

Create the `db.env` from a template and edit it:

```
cp db.example.env db.env
```

Build and launch docker-compose:

```
docker-compose up
```

You are not able to use the database right now, as it has not been migrated.
Migrate the database using the `init_db.sh` script **while `docker-compose` is running**:

```
chmod +x init_db.sh
./init_db.sh
```

Create a user:

```bash
chmod +x create_user.sh

# normal user
./create_user.sh

# admin user
./create_user.sh -a
```

Now you should be able to see the login page at `http://localhost:8000`.

<!-- ### Docker without Compose (PHP-FPM)
Create a container:
Expand Down Expand Up @@ -37,8 +73,4 @@ docker exec -it tr bash -c "php bin/console tr:user:create -a"
```
After that, you have to use a FastCGI proxy like [nginx](https://www.nginx.com/).
There is an example configuration file in `server/tinyreader.conf`.

### Docker Compose

TODO: add installation guide for compose
There is an example configuration file in `server/tinyreader.conf`. -->
1 change: 1 addition & 0 deletions create_user.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
docker-compose exec app bash -c "php bin/console tr:user:create $1"
5 changes: 5 additions & 0 deletions db.example.env
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
POSTGRES_USER=tinyreader
POSTGRES_PASSWORD="12345"
POSTGRES_DB=tinyreader
DATABASE_URL="postgresql://${POSTGRES_USER}:${POSTGRES_PASSWORD}@database:5432/${POSTGRES_DB}?serverVersion=13&charset=utf8"
APP_SECRET="keyboard cat"
39 changes: 39 additions & 0 deletions docker-compose.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
version: '3.8'

services:
database:
image: postgres:13
volumes:
- db_data:/var/lib/postgres/data
env_file:
- db.env
ports:
- '5432'
restart: always

app:
build: .
depends_on:
- database
env_file:
- db.env
ports:
- 9000:9000
volumes:
- tr_build:/app

server:
image: nginx:1.21
depends_on:
- app
volumes:
- tr_build:/var/www/tinyreader
- ./server:/etc/nginx/conf.d
ports:
- 8000:80
expose:
- '8000'

volumes:
db_data:
tr_build:
1 change: 1 addition & 0 deletions init_db.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
docker-compose exec app bash -c "php bin/console doctrine:migrations:migrate -n"
33 changes: 0 additions & 33 deletions migrations/Version20210610161354.php

This file was deleted.

31 changes: 0 additions & 31 deletions migrations/Version20210611132919.php

This file was deleted.

36 changes: 0 additions & 36 deletions migrations/Version20210611141027.php

This file was deleted.

61 changes: 0 additions & 61 deletions migrations/Version20210615102516.php

This file was deleted.

54 changes: 54 additions & 0 deletions migrations/Version20210621102752.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<?php

declare(strict_types=1);

namespace DoctrineMigrations;

use Doctrine\DBAL\Schema\Schema;
use Doctrine\Migrations\AbstractMigration;

/**
* Auto-generated Migration: Please modify to your needs!
*/
final class Version20210621102752 extends AbstractMigration
{
public function getDescription(): string
{
return 'Move from SQLite to PostgreSQL';
}

public function up(Schema $schema): void
{
// this up() migration is auto-generated, please modify it to your needs
$this->addSql('CREATE SEQUENCE article_id_seq INCREMENT BY 1 MINVALUE 1 START 1');
$this->addSql('CREATE SEQUENCE feed_source_id_seq INCREMENT BY 1 MINVALUE 1 START 1');
$this->addSql('CREATE SEQUENCE "user_id_seq" INCREMENT BY 1 MINVALUE 1 START 1');
$this->addSql('CREATE TABLE article (id INT NOT NULL, source_id INT NOT NULL, title VARCHAR(255) NOT NULL, url TEXT NOT NULL, excerpt TEXT NOT NULL, last_modified TIMESTAMP(0) WITHOUT TIME ZONE DEFAULT NULL, PRIMARY KEY(id))');
$this->addSql('CREATE INDEX IDX_23A0E66953C1C61 ON article (source_id)');
$this->addSql('CREATE TABLE feed_source (id INT NOT NULL, name VARCHAR(255) NOT NULL, url TEXT NOT NULL, PRIMARY KEY(id))');
$this->addSql('CREATE TABLE "user" (id INT NOT NULL, username VARCHAR(180) NOT NULL, roles JSON NOT NULL, password VARCHAR(255) NOT NULL, PRIMARY KEY(id))');
$this->addSql('CREATE UNIQUE INDEX UNIQ_8D93D649F85E0677 ON "user" (username)');
$this->addSql('CREATE TABLE user_feed_source (user_id INT NOT NULL, feed_source_id INT NOT NULL, PRIMARY KEY(user_id, feed_source_id))');
$this->addSql('CREATE INDEX IDX_34B3C612A76ED395 ON user_feed_source (user_id)');
$this->addSql('CREATE INDEX IDX_34B3C612DDAEFFBD ON user_feed_source (feed_source_id)');
$this->addSql('ALTER TABLE article ADD CONSTRAINT FK_23A0E66953C1C61 FOREIGN KEY (source_id) REFERENCES feed_source (id) NOT DEFERRABLE INITIALLY IMMEDIATE');
$this->addSql('ALTER TABLE user_feed_source ADD CONSTRAINT FK_34B3C612A76ED395 FOREIGN KEY (user_id) REFERENCES "user" (id) ON DELETE CASCADE NOT DEFERRABLE INITIALLY IMMEDIATE');
$this->addSql('ALTER TABLE user_feed_source ADD CONSTRAINT FK_34B3C612DDAEFFBD FOREIGN KEY (feed_source_id) REFERENCES feed_source (id) ON DELETE CASCADE NOT DEFERRABLE INITIALLY IMMEDIATE');
}

public function down(Schema $schema): void
{
// this down() migration is auto-generated, please modify it to your needs
$this->addSql('CREATE SCHEMA public');
$this->addSql('ALTER TABLE article DROP CONSTRAINT FK_23A0E66953C1C61');
$this->addSql('ALTER TABLE user_feed_source DROP CONSTRAINT FK_34B3C612DDAEFFBD');
$this->addSql('ALTER TABLE user_feed_source DROP CONSTRAINT FK_34B3C612A76ED395');
$this->addSql('DROP SEQUENCE article_id_seq CASCADE');
$this->addSql('DROP SEQUENCE feed_source_id_seq CASCADE');
$this->addSql('DROP SEQUENCE "user_id_seq" CASCADE');
$this->addSql('DROP TABLE article');
$this->addSql('DROP TABLE feed_source');
$this->addSql('DROP TABLE "user"');
$this->addSql('DROP TABLE user_feed_source');
}
}
13 changes: 9 additions & 4 deletions server/tinyreader.conf
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,17 @@ server {
# optionally disable falling back to PHP script for the asset directories;
# nginx will return a 404 error when files are not found instead of passing the
# request to Symfony (improves performance but Symfony's 404 page is not displayed)
# location /bundles {
# try_files $uri =404;
# }
location /build {
# try_files $uri =404;
root /var/www/tinyreader/public;
}

location /bundles {
root /var/www/tinyreader/public;
}

location ~ ^/index\.php(/|$) {
fastcgi_pass 127.0.0.1:9000;
fastcgi_pass app:9000;
fastcgi_split_path_info ^(.+\.php)(/.*)$;
include fastcgi_params;

Expand Down

0 comments on commit ca5eea5

Please sign in to comment.