Skip to content

Commit 1a2eeff

Browse files
committed
Dockerize app
1 parent fad1b5b commit 1a2eeff

9 files changed

+119
-71
lines changed

Dockerfile

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# pull official base image
2+
FROM python:3.8.0-alpine
3+
# set work directory
4+
WORKDIR /usr/src/app
5+
# set environment variables
6+
ENV PYTHONDONTWRITEBYTECODE 1
7+
ENV PYTHONUNBUFFERED 1
8+
ENV FLASK_RUN_HOST=0.0.0.0
9+
RUN apk update && apk add build-base postgresql-dev gcc python3-dev musl-dev
10+
# install dependencies
11+
RUN pip install --upgrade pip
12+
COPY ./requirements.txt /usr/src/app/requirements.txt
13+
#RUN export LDFLAGS="-L/usr/local/opt/openssl/lib"
14+
RUN pip install -r requirements.txt
15+
# copy project
16+
COPY . /usr/src/app/
17+
EXPOSE 5000
18+
19+
COPY ./docker-entrypoint.sh /docker-entrypoint.sh
20+
RUN chmod +x /docker-entrypoint.sh
21+
ENTRYPOINT ["/docker-entrypoint.sh"]

app.py

+1-2
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,7 @@
1111

1212
app = Flask(__name__)
1313
api = Api(app)
14-
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
15-
app.config['SQLALCHEMY_DATABASE_URI'] = 'postgresql://postgres:postgres@localhost:5432/user_api'
14+
app.config.from_object('config.Config')
1615
db = SQLAlchemy(app)
1716
migrate = Migrate(app, db)
1817

config.py

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import os
2+
3+
4+
class Config:
5+
6+
# General Config
7+
TESTING = True
8+
DEBUG = True
9+
10+
# Database
11+
SQLALCHEMY_DATABASE_URI = os.environ.get('SQLALCHEMY_DATABASE_URI',
12+
'postgresql+psycopg2://postgres:postgres@localhost:5432/user_api')
13+
SQLALCHEMY_USERNAME = 'postgres'
14+
SQLALCHEMY_PASSWORD = 'postgres'
15+
SQLALCHEMY_DATABASE_NAME = 'user_api'
16+
SQLALCHEMY_TABLE = 'migrations'
17+
SQLALCHEMY_DB_SCHEMA = 'public'
18+
SQLALCHEMY_TRACK_MODIFICATIONS = False

docker-compose.yml

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
version: '3.7'
2+
networks:
3+
internal:
4+
driver: bridge
5+
6+
services:
7+
api:
8+
build: .
9+
depends_on:
10+
- db
11+
environment:
12+
STAGE: test
13+
SQLALCHEMY_DATABASE_URI: postgresql://postgres:postgres@postgres_python_api:5432/user_api
14+
networks:
15+
- internal
16+
ports:
17+
- 5000:5000
18+
volumes:
19+
- ./app:/usr/src/app/app
20+
- ./migrations:/usr/src/app/migrations
21+
restart: always
22+
23+
db:
24+
container_name: postgres_python_api
25+
environment:
26+
POSTGRES_USER: postgres
27+
POSTGRES_PASSWORD: postgres
28+
POSTGRES_DB: user_api
29+
image: postgres:latest
30+
networks:
31+
- internal
32+
ports:
33+
- 5405:5432
34+
restart: always
35+
volumes:
36+
- ./postgres-data:/var/lib/postgresql/data

docker-entrypoint.sh

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
#!/bin/sh
2+
set -e
3+
flask db upgrade
4+
flask run

migrations/versions/32930e0f95c8_.py

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
"""empty message
2+
3+
Revision ID: 32930e0f95c8
4+
Revises:
5+
Create Date: 2021-05-24 22:05:44.443631
6+
7+
"""
8+
from alembic import op
9+
import sqlalchemy as sa
10+
11+
12+
# revision identifiers, used by Alembic.
13+
revision = '32930e0f95c8'
14+
down_revision = None
15+
branch_labels = None
16+
depends_on = None
17+
18+
19+
def upgrade():
20+
# ### commands auto generated by Alembic - please adjust! ###
21+
op.create_table('user_model',
22+
sa.Column('id', sa.Integer(), nullable=False),
23+
sa.Column('name', sa.String(length=150), nullable=False),
24+
sa.Column('login', sa.String(length=50), nullable=False),
25+
sa.Column('email', sa.String(length=100), nullable=False),
26+
sa.PrimaryKeyConstraint('id')
27+
)
28+
# ### end Alembic commands ###
29+
30+
31+
def downgrade():
32+
# ### commands auto generated by Alembic - please adjust! ###
33+
op.drop_table('user_model')
34+
# ### end Alembic commands ###

migrations/versions/c34557db7f12_.py

-64
This file was deleted.

requirements.txt

+5-4
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
1-
Flask_Migrate==3.0.0
2-
SQLAlchemy==1.4.15
3-
Flask_SQLAlchemy==2.5.1
41
Flask_RESTful==0.3.8
5-
Flask==2.0.0
62
alembic==1.6.2
3+
Flask_SQLAlchemy==2.5.1
4+
SQLAlchemy==1.4.15
5+
Flask_Migrate==3.0.0
6+
Flask_Caching==1.10.1
7+
Flask==2.0.0
78
gunicorn==20.1.0
89
psycopg2-binary==2.8.6

setup.sh

-1
This file was deleted.

0 commit comments

Comments
 (0)