Skip to content

Commit

Permalink
Merge pull request #45 from realpython/docker
Browse files Browse the repository at this point in the history
added docker
  • Loading branch information
mjhea0 authored Apr 18, 2018
2 parents 6789427 + 7c9dc58 commit 6509428
Show file tree
Hide file tree
Showing 15 changed files with 308 additions and 73 deletions.
19 changes: 18 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,30 @@ python:
- "3.4"
- "2.7"

before_install:
sudo: required

services:
- docker

env:
global:
- DOCKER_COMPOSE_VERSION=1.18.0

before_install:
- if [[ $TRAVIS_PYTHON_VERSION == 3.6 ]]; then bash docker-compose.sh; fi
- "cd {{cookiecutter.app_slug}}"

install:
- "pip install pipenv"
- "pipenv install --dev"

before_script:
- if [[ $TRAVIS_PYTHON_VERSION == 3.6 ]]; then docker-compose up --build -d; fi

script:
- flake8 .
- python manage.py cov
- if [[ $TRAVIS_PYTHON_VERSION == 3.6 ]]; then docker-compose run web python manage.py cov; fi

after_script:
- if [[ $TRAVIS_PYTHON_VERSION == 3.6 ]]; then docker-compose down; fi
68 changes: 3 additions & 65 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@ Flask starter project for [Cookiecutter](https://github.com/audreyr/cookiecutter

## Quick Start

### Basics

Install Cookiecutter globally:

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

Create and activate a virtual environment, and then install the requirements.

### Set Environment Variables

Update *project/server/config.py*, and then run:

```sh
$ export APP_NAME="Flask Skeleton"
$ export APP_SETTINGS="project.server.config.DevelopmentConfig"
$ export FLASK_DEBUG=1
```

Using [Pipenv](https://docs.pipenv.org/) or [python-dotenv](https://github.com/theskumar/python-dotenv)? Use the *.env* file to set environment variables:

```sh
APP_NAME="Flask Skeleton"
APP_SETTINGS="project.server.config.DevelopmentConfig"
FLASK_DEBUG=1
```

### Create DB

```sh
$ python manage.py create_db
$ python manage.py db init
$ python manage.py db migrate
$ python manage.py create_admin
$ python manage.py create_data
```

### Run the Application


```sh
$ python manage.py run
```

Access the application at the address [http://localhost:5000/](http://localhost:5000/)

### Testing

Without coverage:

```sh
$ python manage.py test
```

With coverage:

```sh
$ python manage.py cov
```

Run flake8 on the app:
Once generated, review the setup guides, within the newly created project directory, to configure the app:

```sh
$ python manage.py flake
```

or

```sh
$ flake8 project
```
1. [setup-with-docker.md](%7B%7Bcookiecutter.app_slug%7D%7D/setup-with-docker.md)
1. [setup-without-docker.md](%7B%7Bcookiecutter.app_slug%7D%7D/setup-without-docker.md)
6 changes: 6 additions & 0 deletions docker-compose.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#!/bin/bash

sudo rm /usr/local/bin/docker-compose
curl -L https://github.com/docker/compose/releases/download/${DOCKER_COMPOSE_VERSION}/docker-compose-`uname -s`-`uname -m` > docker-compose
chmod +x docker-compose
sudo mv docker-compose /usr/local/bin
1 change: 0 additions & 1 deletion {{cookiecutter.app_slug}}/.python-version

This file was deleted.

26 changes: 26 additions & 0 deletions {{cookiecutter.app_slug}}/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
FROM python:3.6.5

# install environment dependencies
RUN apt-get update -yqq \
&& apt-get install -yqq --no-install-recommends \
netcat \
&& apt-get -q clean

# set working directory
RUN mkdir -p /usr/src/app
WORKDIR /usr/src/app

# add requirements
COPY ./requirements.txt /usr/src/app/requirements.txt

# install requirements
RUN pip install -r requirements.txt

# add entrypoint.sh
COPY ./entrypoint.sh /usr/src/app/entrypoint.sh

# add app
COPY . /usr/src/app

# run server
CMD ["./entrypoint.sh"]
20 changes: 20 additions & 0 deletions {{cookiecutter.app_slug}}/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# Flask Skeleton

## Quick Start

Install Cookiecutter globally:

```sh
$ pip install cookiecutter
```

Generate the boilerplate:

```sh
$ cookiecutter https://github.com/realpython/cookiecutter-flask-skeleton.git
```

Review the set up guides to configure the app:

1. [setup-with-docker.md](setup-with-docker.md)
1. [setup-without-docker](setup-without-docker.md)
34 changes: 34 additions & 0 deletions {{cookiecutter.app_slug}}/docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
version: '3.5'

services:

web:
image: web
build:
context: ./
dockerfile: Dockerfile
volumes:
- '.:/usr/src/app'
ports:
- 5002:5000
environment:
- APP_NAME={{cookiecutter.app_name}}
- FLASK_DEBUG=1
- PYTHONUNBUFFERED=0
- APP_SETTINGS=project.server.config.DevelopmentConfig
- DATABASE_URL=postgres://postgres:postgres@web-db:5432/users_dev
- DATABASE_TEST_URL=postgres://postgres:postgres@web-db:5432/users_test
- SECRET_KEY=change_me_in_prod
depends_on:
- web-db

web-db:
container_name: web-db
build:
context: ./project/server/db
dockerfile: Dockerfile
ports:
- 5435:5432
environment:
- POSTGRES_USER=postgres
- POSTGRES_PASSWORD=postgres
59 changes: 59 additions & 0 deletions {{cookiecutter.app_slug}}/docker-setup.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
# Docker Setup

Use this guide if you want to use Docker in your project.

> Built with Docker v18.03.0-ce.
## Quick Start

### Basics

Install Cookiecutter globally:

```sh
$ pip install cookiecutter
```

Generate the boilerplate:

```sh
$ cookiecutter https://github.com/realpython/cookiecutter-flask-skeleton.git
```

Update the environment variables in *docker-compose.yml*, and then build the images and spin up the containers:

```sh
$ docker-compose up -d --build
```

Create the database:

```sh
$ docker-compose run web python manage.py create_db
$ docker-compose run web python manage.py db init
$ docker-compose run web python manage.py db migrate
$ docker-compose run web python manage.py create_admin
$ docker-compose run web python manage.py create_data
```

Access the application at the address [http://localhost:5002/](http://localhost:5002/)

### Testing

Test without coverage:

```sh
$ docker-compose run web python manage.py test
```

Test with coverage:

```sh
$ docker-compose run web python manage.py cov
```

Lint:

```sh
$ docker-compose run web flake8 project
```
11 changes: 11 additions & 0 deletions {{cookiecutter.app_slug}}/entrypoint.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#!/bin/sh

echo "Waiting for postgres..."

while ! nc -z web-db 5432; do
sleep 0.1
done

echo "PostgreSQL started"

python manage.py run -h 0.0.0.0
12 changes: 7 additions & 5 deletions {{cookiecutter.app_slug}}/project/server/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@

class BaseConfig(object):
"""Base configuration."""
APP_NAME = os.getenv('APP_NAME', default='Flask Skeleton')
APP_NAME = os.getenv('APP_NAME', 'Flask Skeleton')
BCRYPT_LOG_ROUNDS = 4
DEBUG_TB_ENABLED = False
SECRET_KEY = os.getenv('SECRET_KEY', default='my_precious')
SECRET_KEY = os.getenv('SECRET_KEY', 'my_precious')
SQLALCHEMY_TRACK_MODIFICATIONS = False
WTF_CSRF_ENABLED = False

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


class TestingConfig(BaseConfig):
"""Testing configuration."""
PRESERVE_CONTEXT_ON_EXCEPTION = False
SQLALCHEMY_DATABASE_URI = 'sqlite:///'
SQLALCHEMY_DATABASE_URI = os.environ.get('DATABASE_TEST_URL', 'sqlite:///')
TESTING = True


class ProductionConfig(BaseConfig):
"""Production configuration."""
BCRYPT_LOG_ROUNDS = 13
SQLALCHEMY_DATABASE_URI = 'postgresql://localhost/example'
SQLALCHEMY_DATABASE_URI = os.environ.get('DATABASE_URL')
WTF_CSRF_ENABLED = True
4 changes: 4 additions & 0 deletions {{cookiecutter.app_slug}}/project/server/db/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
FROM postgres

# run create.sql on init
ADD create.sql /docker-entrypoint-initdb.d
4 changes: 4 additions & 0 deletions {{cookiecutter.app_slug}}/project/server/db/create.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
CREATE DATABASE users_prod;
CREATE DATABASE users_stage;
CREATE DATABASE users_dev;
CREATE DATABASE users_test;
2 changes: 1 addition & 1 deletion {{cookiecutter.app_slug}}/requirements.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
configparser==3.5.0
coverage==4.5.1
flake8==3.5.0
Flask==0.12.2
Expand All @@ -10,3 +9,4 @@ Flask-Migrate==2.1.1
Flask-SQLAlchemy==2.3.2
Flask-Testing==0.7.1
Flask-WTF==0.14.2
psycopg2==2.7.3.2
45 changes: 45 additions & 0 deletions {{cookiecutter.app_slug}}/setup-with-docker.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
# Docker Setup

Use this guide if you want to use Docker in your project.

> Built with Docker v18.03.0-ce.
## Getting Started

Update the environment variables in *docker-compose.yml*, and then build the images and spin up the containers:

```sh
$ docker-compose up -d --build
```

Create the database:

```sh
$ docker-compose run web python manage.py create_db
$ docker-compose run web python manage.py db init
$ docker-compose run web python manage.py db migrate
$ docker-compose run web python manage.py create_admin
$ docker-compose run web python manage.py create_data
```

Access the application at the address [http://localhost:5002/](http://localhost:5002/)

### Testing

Test without coverage:

```sh
$ docker-compose run web python manage.py test
```

Test with coverage:

```sh
$ docker-compose run web python manage.py cov
```

Lint:

```sh
$ docker-compose run web flake8 project
```
Loading

0 comments on commit 6509428

Please sign in to comment.