Skip to content

Commit 412e39a

Browse files
committed
Fixes #27: Rewrite Flask Docker example so it works with latest SQLAlchemy and Ansible 2.8.
1 parent 23d84d0 commit 412e39a

File tree

9 files changed

+32
-27
lines changed

9 files changed

+32
-27
lines changed

docker/README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,16 @@
22

33
## Background
44

5-
[Docker](https://www.docker.com/) is used to build and manage linux containers. [Ansible](http://www.ansible.com/) is useful in managing the Docker lifecycle.
5+
[Docker](https://www.docker.com/) is used to build and manage linux containers. [Ansible](http://www.ansible.com/) is useful in managing the Docker container lifecycle.
66

77
This Vagrant profile uses Ansible to configure a local VM with Docker, then it uses Ansible to build and run three containers for a simple Flask + MySQL web app stack, Docker-style:
88

99
- `www`: Flask app on an Ubuntu container.
1010
- `db`: MySQL database on an Ubuntu container.
1111
- `data`: MySQL data volume on a Busybox container (for persistence).
1212

13+
> Note: These examples are intended to highlight how Ansible intracts with the Docker container lifecycle. There are a number of other ways to manage container builds and orchestration, like Docker Compose, Kubernetes, etc. This example is not intended to necessarily be the 'best practice' for every situation.
14+
1315
## Getting Started
1416

1517
This README file is inside a folder that contains a `Vagrantfile` (hereafter this folder shall be called the [vagrant_root]), which tells Vagrant how to set up your virtual machine in VirtualBox.

docker/Vagrantfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
VAGRANTFILE_API_VERSION = "2"
55

66
Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|
7-
config.vm.box = "geerlingguy/ubuntu1604"
7+
config.vm.box = "geerlingguy/ubuntu1804"
88
config.vm.network :private_network, ip: "192.168.33.39"
99
config.ssh.insert_key = false
1010

docker/provisioning/docker.yml

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,31 +3,34 @@
33
docker_image:
44
name: "{{ item.name }}"
55
tag: "{{ item.tag }}"
6-
path: "/vagrant/provisioning/{{ item.directory }}"
7-
state: build
6+
source: build
7+
build:
8+
path: "/vagrant/provisioning/{{ item.directory }}"
9+
pull: false
10+
state: present
811
with_items:
9-
- { name: data, tag: "data", directory: data }
10-
- { name: www, tag: "flask", directory: www }
11-
- { name: db, tag: mysql, directory: db }
12+
- { name: data, tag: latest, directory: data }
13+
- { name: flask, tag: latest, directory: www }
14+
- { name: db, tag: latest, directory: db }
1215

1316
# Data containers don't need to be running to be utilized.
1417
- name: Run a Data container.
1518
docker_container:
16-
image: data:data
19+
image: data:latest
1720
name: data
1821
state: present
1922

2023
- name: Run a Flask container.
2124
docker_container:
22-
image: www:flask
25+
image: flask:latest
2326
name: www
2427
state: started
2528
command: python /opt/www/index.py
2629
ports: "80:80"
2730

2831
- name: Run a MySQL container.
2932
docker_container:
30-
image: db:mysql
33+
image: db:latest
3134
name: db
3235
state: started
3336
volumes_from: data

docker/provisioning/main.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
---
22
- hosts: all
3-
become: yes
3+
become: true
44

55
pre_tasks:
66
- name: Update apt cache if needed.
77
apt: update_cache=yes cache_valid_time=3600
88

99
roles:
10-
- role: angstwad.docker_ubuntu
10+
- role: geerlingguy.docker
1111

1212
tasks:
1313
- import_tasks: setup.yml

docker/provisioning/setup.yml

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,10 @@
88
user:
99
name: vagrant
1010
groups: docker
11-
append: yes
12-
become: yes
11+
append: true
1312

1413
- name: Install Pip.
15-
apt: name=python-pip state=installed
16-
become: yes
14+
apt: name=python-pip state=present
1715

1816
- name: Install Docker Python library.
19-
pip: name=docker-py state=present
20-
become: yes
17+
pip: name=docker state=present

docker/provisioning/www/Dockerfile

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
# A simple Flask app container.
2-
FROM geerlingguy/docker-ubuntu1604-ansible
2+
FROM geerlingguy/docker-ubuntu1804-ansible
33
MAINTAINER Jeff Geerling <[email protected]>
44

55
# Install Flask app dependencies.
66
RUN apt-get update
7-
RUN apt-get install -y libmysqlclient-dev python-dev python-pip
7+
RUN apt-get install -y libmysqlclient-dev build-essential \
8+
python-dev python-pip
89
RUN pip install flask flask-sqlalchemy mysql-python
910

1011
# Install playbook and run it.

docker/provisioning/www/index.py.j2

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,23 @@
22
from flask import Flask
33
from flask import Markup
44
from flask import render_template
5-
from flask.ext.sqlalchemy import SQLAlchemy
5+
from flask_sqlalchemy import SQLAlchemy
6+
from sqlalchemy import text
67

78
app = Flask(__name__)
89

910
# Configure MySQL connection.
10-
db = SQLAlchemy()
1111
db_uri = 'mysql://flask:flask@{{ host_ip_address }}/flask'
1212
app.config['SQLALCHEMY_DATABASE_URI'] = db_uri
13-
db.init_app(app)
13+
db = SQLAlchemy(app)
1414

1515
@app.route("/")
1616
def test():
1717
mysql_result = False
1818
try:
19-
if db.session.query("1").from_statement("SELECT 1").all():
19+
query = text('SELECT 1')
20+
result = db.engine.execute(query)
21+
if [row[0] for row in result][0] == 1:
2022
mysql_result = True
2123
except:
2224
pass

docker/provisioning/www/playbook.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
---
22
- hosts: localhost
3-
become: yes
3+
become: true
44

55
tasks:
66
- name: Get host IP address.
7-
shell: "/sbin/ip route|awk '/default/ { print $3 }'"
7+
shell: "/sbin/ip route | awk '/default/ { print $3 }'"
88
register: host_ip
99
changed_when: false
1010

docker/requirements.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
---
2-
- src: angstwad.docker_ubuntu
2+
- src: geerlingguy.docker

0 commit comments

Comments
 (0)