Skip to content
This repository was archived by the owner on Mar 7, 2025. It is now read-only.

Commit a1898a4

Browse files
author
Acosta Nicolás Gabriel
committed
dockerization and separation of redis and postress fron the app itself, and creation of docker-compose file for te deploy of development environment
1 parent 040b1a5 commit a1898a4

File tree

8 files changed

+116
-36
lines changed

8 files changed

+116
-36
lines changed

Dockerfile

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,24 @@
1-
FROM ubuntu:16.04
1+
FROM python:3.8-alpine
22

3-
#MAINTANER Your Name "[email protected]"
43

4+
# Packages required for psycopg2
5+
RUN apk update && apk add postgresql-dev gcc python3-dev musl-dev
6+
7+
#MAINTANER Your Name "[email protected]"
58
69
ENV MAIL_PASSWORD=testpass
710
ENV SECRET_KEY=SuperRandomStringToBeUsedForEncryption
8-
9-
RUN apt-get update -y && \
10-
apt-get install -y python3-pip python3-dev
11-
RUN apt-get install -y ruby-dev
12-
RUN gem install foreman
1311
# We copy just the requirements.txt first to leverage Docker cache
1412
COPY ./requirements.txt /app/requirements.txt
1513

16-
17-
RUN apt-get install -y redis-server
1814
WORKDIR /app
19-
RUN apt-get install -y build-essential libpq-dev
20-
RUN pip3 install -r requirements.txt
15+
RUN pip3 install -r requirements.txt
2116
ENV PYTHONIOENCODING=UTF-8
2217
RUN pip3 install sqlalchemy_utils flask_dance flask_caching python-gitlab
18+
2319
COPY . /app
24-
RUN python3 manage.py recreate_db && python3 manage.py setup_dev && python3 manage.py add_fake_data
2520

26-
CMD ["foreman", "start" ,"-f", "Local"]
21+
#RUN python3 manage.py recreate_db && python3 manage.py setup_dev && python3 manage.py add_fake_data
22+
23+
ENTRYPOINT ["python3", "-u" ,"manage.py", "runserver"]
2724

Dockerfile.worker

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
FROM python:3.8-alpine
2+
3+
4+
# Packages required for psycopg2
5+
RUN apk update && apk add postgresql-dev gcc python3-dev musl-dev
6+
7+
#MAINTANER Your Name "[email protected]"
8+
9+
ENV MAIL_PASSWORD=testpass
10+
ENV SECRET_KEY=SuperRandomStringToBeUsedForEncryption
11+
# We copy just the requirements.txt first to leverage Docker cache
12+
COPY ./requirements.txt /app/requirements.txt
13+
14+
WORKDIR /app
15+
RUN pip3 install -r requirements.txt
16+
ENV PYTHONIOENCODING=UTF-8
17+
RUN pip3 install sqlalchemy_utils flask_dance flask_caching python-gitlab
18+
19+
COPY . /app
20+
21+
ENTRYPOINT ["python3", "-u" ,"manage.py", "run_worker"]
22+

Local

Lines changed: 0 additions & 3 deletions
This file was deleted.

README.md

Lines changed: 27 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,33 @@ Admin Editing Users:
4141

4242
![edit user](readme_media/edituser.gif "edituser")
4343

44+
## Gettin up and running with Docker and docker-compose:
45+
46+
##### Clone the repository
47+
```
48+
$ git clone https://github.com/YOUR_USERNAME/REPO_NAME.git
49+
$ cd REPO_NAME
50+
```
51+
##### Create and run the images:
52+
53+
```
54+
$ docker-compose up
55+
```
56+
57+
##### Create database and initial data for development:
58+
59+
```
60+
$ docker-compose exec server ./init_database.sh
61+
```
62+
63+
It will deploy 5 docker images:
64+
65+
- server: Flask app running in [http://localhost:5000](http://localhost:5000).
66+
- worker: Worker ready to get tasks.
67+
- postgres: Postgres SQL isolated from the app.
68+
- adminer: Web client for database management, running in [http://localhost:8080](http://localhost:8080).
69+
- redis: Redis SQL isolated from the app
70+
4471

4572
## Setting up
4673

@@ -191,23 +218,6 @@ $ honcho start -e config.env -f Local
191218

192219
For Windows users having issues with binding to a redis port locally, refer to [this issue](https://github.com/hack4impact/flask-base/issues/132).
193220

194-
195-
## Gettin up and running with Docker
196-
197-
Currently we have a `Dockerfile` intended for testing purposes and it automates the whole cycle of running the application, setting up the database and redis.
198-
199-
200-
##### How to use the docker file
201-
In only three simple steps :
202-
- change the variables `MAIL_USERNAME` , `MAIL_PASSWORD` and `SECRET_KEY`
203-
- `docker build -t <image_name> .
204-
- `docker run -it -d -p 5000:5000 --name <container name> <image_name> /bin/bash`
205-
- To run in foreground mode `docker run -it -p 5000:5000 --name <container name> <image_name> /bin/bash`
206-
207-
##### Note
208-
209-
A more robust version with docker-compose is being developed to separate redis in separate container and allow the deployment of production-level applications automatically without the need of manual provisioning
210-
211221
## Formatting code
212222

213223
Before you submit changes to flask-base, you may want to autoformat your code with `python manage.py format`.

config.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121

2222
class Config:
2323
APP_NAME = os.environ.get('APP_NAME', 'Flask-Base')
24-
2524
if os.environ.get('SECRET_KEY'):
2625
SECRET_KEY = os.environ.get('SECRET_KEY')
2726
else:

docker-compose.yml

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
version: '3.4'
2+
services:
3+
server:
4+
build:
5+
context: .
6+
ports:
7+
- '5000:5000'
8+
volumes:
9+
- './:/app'
10+
environment:
11+
# set environment variables
12+
REDISTOGO_URL: http://redis:6379
13+
DEV_DATABASE_URL: postgres://${POSTGRES_USER}:${POSTGRES_PASSWORD}@postgres:5432/${POSTGRES_DB}
14+
TEST_DATABASE_URL: postgres://${POSTGRES_USER}:${POSTGRES_PASSWORD}@postgres:5432/${POSTGRES_DB}
15+
DATABASE_URL: postgres://${POSTGRES_USER}:${POSTGRES_PASSWORD}@postgres:5432/${POSTGRES_DB}
16+
17+
worker:
18+
build:
19+
dockerfile: Dockerfile.worker
20+
context: .
21+
volumes:
22+
- './:/app'
23+
environment:
24+
# set environment variables
25+
REDISTOGO_URL: http://redis:6379
26+
DEV_DATABASE_URL: postgres://${POSTGRES_USER}:${POSTGRES_PASSWORD}@postgres:5432/${POSTGRES_DB}
27+
TEST_DATABASE_URL: postgres://${POSTGRES_USER}:${POSTGRES_PASSWORD}@postgres:5432/${POSTGRES_DB}
28+
DATABASE_URL: postgres://${POSTGRES_USER}:${POSTGRES_PASSWORD}@postgres:5432/${POSTGRES_DB}
29+
30+
postgres:
31+
image: postgres
32+
environment:
33+
POSTGRES_USER: admin
34+
POSTGRES_PASSWORD: example
35+
POSTGRES_DB: mydatabase
36+
volumes:
37+
- db-data:/var/lib/postgresql/data
38+
adminer:
39+
image: adminer
40+
restart: always
41+
ports:
42+
- 8080:8080
43+
44+
redis:
45+
image:
46+
redis:6-alpine
47+
48+
volumes:
49+
db-data:

init_database.sh

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
#! /usr/bin/env sh
2+
3+
python3 manage.py recreate_db
4+
python3 manage.py setup_dev
5+
python3 manage.py add_fake_data

manage.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,14 @@
33
import subprocess
44

55
from flask_migrate import Migrate, MigrateCommand
6-
from flask_script import Manager, Shell
6+
from flask_script import Manager, Shell, Server
77
from redis import Redis
88
from rq import Connection, Queue, Worker
99

1010
from app import create_app, db
1111
from app.models import Role, User
1212
from config import Config
13-
13+
import os
1414
app = create_app(os.getenv('FLASK_CONFIG') or 'default')
1515
manager = Manager(app)
1616
migrate = Migrate(app, db)
@@ -22,6 +22,7 @@ def make_shell_context():
2222

2323
manager.add_command('shell', Shell(make_context=make_shell_context))
2424
manager.add_command('db', MigrateCommand)
25+
manager.add_command('runserver', Server(host="0.0.0.0"))
2526

2627

2728
@manager.command

0 commit comments

Comments
 (0)