Skip to content

Commit

Permalink
Merge pull request airbnb#685 from airbnb/jg--update_sql_image
Browse files Browse the repository at this point in the history
[kp2] add docker image for db
  • Loading branch information
JJJ000 authored Nov 28, 2022
2 parents d5141eb + fff7a02 commit 76432d0
Show file tree
Hide file tree
Showing 10 changed files with 108 additions and 4 deletions.
Empty file added knowledge_repo_v2/__init__
Empty file.
27 changes: 26 additions & 1 deletion knowledge_repo_v2/docker/docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,30 @@ services:
ports:
- '5050:5050'
volumes:
- /app/node_modules
- /app/venv
- ./../service:/app
environment:
NODE_ENV: production
DB_HOST: db
DB_PORT: 5432
DB_USER: postgres
DB_PASSWORD: postgres
DB_NAME: knowledge_repo_dev
depends_on:
- db
db:
image: postgres:15.1-alpine
restart: always
environment:
- POSTGRES_USER=postgres
- POSTGRES_PASSWORD=postgres
- POSTGRES_DB=knowledge_repo_dev
logging:
options:
max-size: 10m
max-file: "3"
ports:
- '5432:5432'

volumes:
postgres_data:
6 changes: 6 additions & 0 deletions knowledge_repo_v2/service/.env.dev
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
FLASK_APP=__init__.py
FLASK_DEBUG=1
DATABASE_URL=postgresql://postgres:postgres@db:5432/knowledge_repo_dev
SQL_HOST=db
SQL_PORT=5432
DATABASE=postgres
15 changes: 14 additions & 1 deletion knowledge_repo_v2/service/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,25 @@ ARG PIP=pip3

WORKDIR /app

# Set environment variables
ENV PYTHONDONTWRITEBYTECODE 1
ENV PYTHONUNBUFFERED 1

# Install system dependencies
RUN apt-get update && apt-get install -y netcat

# Setup Expose port
EXPOSE 5050

# Install dependencies
RUN pip install --upgrade pip
COPY requirements.txt ./

RUN ${PIP} install -r requirements.txt

# Copy project
COPY . ./

# Run entrypoint.sh
ENTRYPOINT ["./entrypoint.sh"]

CMD ["python", "app.py"]
Empty file.
12 changes: 12 additions & 0 deletions knowledge_repo_v2/service/app.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,23 @@
from flask import Flask
from flask_cors import CORS
from flask_sqlalchemy import SQLAlchemy


app = Flask(__name__)
CORS(app)
app.config.from_object("config.Config")
db = SQLAlchemy(app)


class User(db.Model):
__tablename__ = "knowledge_post_author"

id = db.Column(db.Integer, primary_key=True)
email = db.Column(db.String(128), unique=True, nullable=False)

def __init__(self, email):
self.email = email

@app.route("/")
def hello():
return "Hello, we are building knowledge repo v2!"
Expand Down
9 changes: 9 additions & 0 deletions knowledge_repo_v2/service/config.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import os


basedir = os.path.abspath(os.path.dirname(__file__))


class Config(object):
SQLALCHEMY_DATABASE_URI = os.getenv("DATABASE_URL", "sqlite://")
SQLALCHEMY_TRACK_MODIFICATIONS = False
16 changes: 16 additions & 0 deletions knowledge_repo_v2/service/entrypoint.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#!/bin/sh

if [ "$DATABASE" = "postgres" ]
then
echo "Waiting for postgres..."

while ! nc -z $SQL_HOST $SQL_PORT; do
sleep 0.1
done

echo "PostgreSQL started"
fi

python manage.py create_db

exec "$@"
21 changes: 21 additions & 0 deletions knowledge_repo_v2/service/manage.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
from flask.cli import FlaskGroup
from app import app, db, User

cli = FlaskGroup(app)

# docker-compose exec service python manage.py create_db
@cli.command("create_db")
def create_db():
db.drop_all()
db.create_all()
db.session.commit()


@cli.command("seed_db")
def seed_db():
db.session.add(User(email="[email protected]"))
db.session.commit()


if __name__ == "__main__":
cli()
6 changes: 4 additions & 2 deletions knowledge_repo_v2/service/requirements.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
black==22.10.0
flask==2.2.2
flask-cors==3.0.10
black==22.10.0
flask-sqlalchemy==3.0.2
pylint==2.15.6
pycodestyle==2.9.1
pycodestyle==2.9.1
psycopg2-binary==2.9.4

0 comments on commit 76432d0

Please sign in to comment.