Skip to content

Commit 6509428

Browse files
authored
Merge pull request #45 from realpython/docker
added docker
2 parents 6789427 + 7c9dc58 commit 6509428

File tree

15 files changed

+308
-73
lines changed

15 files changed

+308
-73
lines changed

.travis.yml

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,30 @@ python:
66
- "3.4"
77
- "2.7"
88

9-
before_install:
9+
sudo: required
10+
11+
services:
12+
- docker
13+
14+
env:
15+
global:
16+
- DOCKER_COMPOSE_VERSION=1.18.0
17+
18+
before_install:
19+
- if [[ $TRAVIS_PYTHON_VERSION == 3.6 ]]; then bash docker-compose.sh; fi
1020
- "cd {{cookiecutter.app_slug}}"
1121

1222
install:
1323
- "pip install pipenv"
1424
- "pipenv install --dev"
1525

26+
before_script:
27+
- if [[ $TRAVIS_PYTHON_VERSION == 3.6 ]]; then docker-compose up --build -d; fi
28+
1629
script:
1730
- flake8 .
1831
- python manage.py cov
32+
- if [[ $TRAVIS_PYTHON_VERSION == 3.6 ]]; then docker-compose run web python manage.py cov; fi
33+
34+
after_script:
35+
- if [[ $TRAVIS_PYTHON_VERSION == 3.6 ]]; then docker-compose down; fi

README.md

Lines changed: 3 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,6 @@ Flask starter project for [Cookiecutter](https://github.com/audreyr/cookiecutter
66

77
## Quick Start
88

9-
### Basics
10-
119
Install Cookiecutter globally:
1210

1311
```sh
@@ -20,67 +18,7 @@ Generate the boilerplate:
2018
$ cookiecutter https://github.com/realpython/cookiecutter-flask-skeleton.git
2119
```
2220

23-
Create and activate a virtual environment, and then install the requirements.
24-
25-
### Set Environment Variables
26-
27-
Update *project/server/config.py*, and then run:
28-
29-
```sh
30-
$ export APP_NAME="Flask Skeleton"
31-
$ export APP_SETTINGS="project.server.config.DevelopmentConfig"
32-
$ export FLASK_DEBUG=1
33-
```
34-
35-
Using [Pipenv](https://docs.pipenv.org/) or [python-dotenv](https://github.com/theskumar/python-dotenv)? Use the *.env* file to set environment variables:
36-
37-
```sh
38-
APP_NAME="Flask Skeleton"
39-
APP_SETTINGS="project.server.config.DevelopmentConfig"
40-
FLASK_DEBUG=1
41-
```
42-
43-
### Create DB
44-
45-
```sh
46-
$ python manage.py create_db
47-
$ python manage.py db init
48-
$ python manage.py db migrate
49-
$ python manage.py create_admin
50-
$ python manage.py create_data
51-
```
52-
53-
### Run the Application
54-
55-
56-
```sh
57-
$ python manage.py run
58-
```
59-
60-
Access the application at the address [http://localhost:5000/](http://localhost:5000/)
61-
62-
### Testing
63-
64-
Without coverage:
65-
66-
```sh
67-
$ python manage.py test
68-
```
69-
70-
With coverage:
71-
72-
```sh
73-
$ python manage.py cov
74-
```
75-
76-
Run flake8 on the app:
21+
Once generated, review the setup guides, within the newly created project directory, to configure the app:
7722

78-
```sh
79-
$ python manage.py flake
80-
```
81-
82-
or
83-
84-
```sh
85-
$ flake8 project
86-
```
23+
1. [setup-with-docker.md](%7B%7Bcookiecutter.app_slug%7D%7D/setup-with-docker.md)
24+
1. [setup-without-docker.md](%7B%7Bcookiecutter.app_slug%7D%7D/setup-without-docker.md)

docker-compose.sh

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
#!/bin/bash
2+
3+
sudo rm /usr/local/bin/docker-compose
4+
curl -L https://github.com/docker/compose/releases/download/${DOCKER_COMPOSE_VERSION}/docker-compose-`uname -s`-`uname -m` > docker-compose
5+
chmod +x docker-compose
6+
sudo mv docker-compose /usr/local/bin

{{cookiecutter.app_slug}}/.python-version

Lines changed: 0 additions & 1 deletion
This file was deleted.
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
FROM python:3.6.5
2+
3+
# install environment dependencies
4+
RUN apt-get update -yqq \
5+
&& apt-get install -yqq --no-install-recommends \
6+
netcat \
7+
&& apt-get -q clean
8+
9+
# set working directory
10+
RUN mkdir -p /usr/src/app
11+
WORKDIR /usr/src/app
12+
13+
# add requirements
14+
COPY ./requirements.txt /usr/src/app/requirements.txt
15+
16+
# install requirements
17+
RUN pip install -r requirements.txt
18+
19+
# add entrypoint.sh
20+
COPY ./entrypoint.sh /usr/src/app/entrypoint.sh
21+
22+
# add app
23+
COPY . /usr/src/app
24+
25+
# run server
26+
CMD ["./entrypoint.sh"]
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# Flask Skeleton
2+
3+
## Quick Start
4+
5+
Install Cookiecutter globally:
6+
7+
```sh
8+
$ pip install cookiecutter
9+
```
10+
11+
Generate the boilerplate:
12+
13+
```sh
14+
$ cookiecutter https://github.com/realpython/cookiecutter-flask-skeleton.git
15+
```
16+
17+
Review the set up guides to configure the app:
18+
19+
1. [setup-with-docker.md](setup-with-docker.md)
20+
1. [setup-without-docker](setup-without-docker.md)
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
version: '3.5'
2+
3+
services:
4+
5+
web:
6+
image: web
7+
build:
8+
context: ./
9+
dockerfile: Dockerfile
10+
volumes:
11+
- '.:/usr/src/app'
12+
ports:
13+
- 5002:5000
14+
environment:
15+
- APP_NAME={{cookiecutter.app_name}}
16+
- FLASK_DEBUG=1
17+
- PYTHONUNBUFFERED=0
18+
- APP_SETTINGS=project.server.config.DevelopmentConfig
19+
- DATABASE_URL=postgres://postgres:postgres@web-db:5432/users_dev
20+
- DATABASE_TEST_URL=postgres://postgres:postgres@web-db:5432/users_test
21+
- SECRET_KEY=change_me_in_prod
22+
depends_on:
23+
- web-db
24+
25+
web-db:
26+
container_name: web-db
27+
build:
28+
context: ./project/server/db
29+
dockerfile: Dockerfile
30+
ports:
31+
- 5435:5432
32+
environment:
33+
- POSTGRES_USER=postgres
34+
- POSTGRES_PASSWORD=postgres
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
# Docker Setup
2+
3+
Use this guide if you want to use Docker in your project.
4+
5+
> Built with Docker v18.03.0-ce.
6+
7+
## Quick Start
8+
9+
### Basics
10+
11+
Install Cookiecutter globally:
12+
13+
```sh
14+
$ pip install cookiecutter
15+
```
16+
17+
Generate the boilerplate:
18+
19+
```sh
20+
$ cookiecutter https://github.com/realpython/cookiecutter-flask-skeleton.git
21+
```
22+
23+
Update the environment variables in *docker-compose.yml*, and then build the images and spin up the containers:
24+
25+
```sh
26+
$ docker-compose up -d --build
27+
```
28+
29+
Create the database:
30+
31+
```sh
32+
$ docker-compose run web python manage.py create_db
33+
$ docker-compose run web python manage.py db init
34+
$ docker-compose run web python manage.py db migrate
35+
$ docker-compose run web python manage.py create_admin
36+
$ docker-compose run web python manage.py create_data
37+
```
38+
39+
Access the application at the address [http://localhost:5002/](http://localhost:5002/)
40+
41+
### Testing
42+
43+
Test without coverage:
44+
45+
```sh
46+
$ docker-compose run web python manage.py test
47+
```
48+
49+
Test with coverage:
50+
51+
```sh
52+
$ docker-compose run web python manage.py cov
53+
```
54+
55+
Lint:
56+
57+
```sh
58+
$ docker-compose run web flake8 project
59+
```
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
#!/bin/sh
2+
3+
echo "Waiting for postgres..."
4+
5+
while ! nc -z web-db 5432; do
6+
sleep 0.1
7+
done
8+
9+
echo "PostgreSQL started"
10+
11+
python manage.py run -h 0.0.0.0

{{cookiecutter.app_slug}}/project/server/config.py

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,10 @@
77

88
class BaseConfig(object):
99
"""Base configuration."""
10-
APP_NAME = os.getenv('APP_NAME', default='Flask Skeleton')
10+
APP_NAME = os.getenv('APP_NAME', 'Flask Skeleton')
1111
BCRYPT_LOG_ROUNDS = 4
1212
DEBUG_TB_ENABLED = False
13-
SECRET_KEY = os.getenv('SECRET_KEY', default='my_precious')
13+
SECRET_KEY = os.getenv('SECRET_KEY', 'my_precious')
1414
SQLALCHEMY_TRACK_MODIFICATIONS = False
1515
WTF_CSRF_ENABLED = False
1616

@@ -19,19 +19,21 @@ class DevelopmentConfig(BaseConfig):
1919
"""Development configuration."""
2020
DEBUG_TB_ENABLED = True
2121
DEBUG_TB_INTERCEPT_REDIRECTS = False
22-
SQLALCHEMY_DATABASE_URI = 'sqlite:///{0}'.format(
23-
os.path.join(basedir, 'dev.db'))
22+
SQLALCHEMY_DATABASE_URI = os.environ.get(
23+
'DATABASE_URL',
24+
'sqlite:///{0}'.format(os.path.join(basedir, 'dev.db')))
2425

2526

2627
class TestingConfig(BaseConfig):
2728
"""Testing configuration."""
2829
PRESERVE_CONTEXT_ON_EXCEPTION = False
2930
SQLALCHEMY_DATABASE_URI = 'sqlite:///'
31+
SQLALCHEMY_DATABASE_URI = os.environ.get('DATABASE_TEST_URL', 'sqlite:///')
3032
TESTING = True
3133

3234

3335
class ProductionConfig(BaseConfig):
3436
"""Production configuration."""
3537
BCRYPT_LOG_ROUNDS = 13
36-
SQLALCHEMY_DATABASE_URI = 'postgresql://localhost/example'
38+
SQLALCHEMY_DATABASE_URI = os.environ.get('DATABASE_URL')
3739
WTF_CSRF_ENABLED = True

0 commit comments

Comments
 (0)