Skip to content

Commit 80184cc

Browse files
committed
Issue geerlingguy#301: Move Docker Flask example into repository and update for Ubuntu 20.04.
1 parent 5e1b717 commit 80184cc

File tree

15 files changed

+261
-0
lines changed

15 files changed

+261
-0
lines changed

.travis.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,10 @@ env:
2424
distro: ubuntu2004
2525
test_idempotence: false
2626

27+
- playbook: docker-flask.yml
28+
distro: ubuntu2004
29+
test_idempotence: false
30+
2731
- playbook: docker-hubot.yml
2832
distro: ubuntu2004
2933
test_idempotence: false

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@ Here is an outline of all the examples contained in this repository, by chapter:
8383

8484
- [`docker`](docker/): Very simple playbook demonstrating Ansible's ability to manage Docker container images.
8585
- [`docker-hubot`](docker-hubot/): Slightly more involved example of Ansible's ability to manage and run Docker container images.
86+
- [`docker-flask`](docker-flask/): A sample Flask app built with Ansible playbooks running inside the container.
8687

8788
### Chapter 16
8889

docker-flask/README.md

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
# Ansible configuration to build Flask App inside Docker Container
2+
3+
## Background
4+
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.
6+
7+
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:
8+
9+
- `www`: Flask app on an Ubuntu container.
10+
- `db`: MySQL database on an Ubuntu container.
11+
- `data`: MySQL data volume on a Busybox container (for persistence).
12+
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+
15+
## Getting Started
16+
17+
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.
18+
19+
To use the vagrant file, you will need to have done the following:
20+
21+
1. Download and Install [VirtualBox](https://www.virtualbox.org/wiki/Downloads)
22+
2. Download and Install [Vagrant](https://www.vagrantup.com/downloads.html)
23+
3. Install [Ansible](http://docs.ansible.com/ansible/latest/intro_installation.html)
24+
4. Open a shell prompt (Terminal app on a Mac) and cd into the folder containing the `Vagrantfile`
25+
5. Run the following command to install the necessary Ansible roles for this profile: `$ ansible-galaxy install -r requirements.yml`
26+
27+
Once all of that is done, you can simply type in `vagrant up`, and Vagrant will create a new VM, install the base box, and configure it.
28+
29+
Once the new VM is up and running (after `vagrant up` is complete and you're back at the command prompt), you can log into it via SSH if you'd like by typing in `vagrant ssh`. Otherwise, the next steps are below.
30+
31+
### Setting up your hosts file
32+
33+
You need to modify your host machine's hosts file (Mac/Linux: `/etc/hosts`; Windows: `%systemroot%\system32\drivers\etc\hosts`), adding the line below:
34+
35+
192.168.33.39 docker-flask.test
36+
37+
After that is configured, you could visit http://docker-flask.test/ in a browser, and you'll see the test page, confirming the Flask container can communicate with the MySQL container.
38+
39+
If you'd like additional assistance editing your hosts file, please read [How do I modify my hosts file?](http://www.rackspace.com/knowledge_center/article/how-do-i-modify-my-hosts-file) from Rackspace.
40+
41+
## Author Information
42+
43+
Created by by [Jeff Geerling](https://www.jeffgeerling.com/), as an example in [Ansible for DevOps](https://www.ansiblefordevops.com/).

docker-flask/Vagrantfile

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# -*- mode: ruby -*-
2+
# vi: set ft=ruby :
3+
4+
VAGRANTFILE_API_VERSION = "2"
5+
6+
Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|
7+
config.vm.box = "geerlingguy/ubuntu2004"
8+
config.vm.network :private_network, ip: "192.168.33.39"
9+
config.ssh.insert_key = false
10+
11+
config.vm.hostname = "docker-flask.test"
12+
config.vm.provider :virtualbox do |v|
13+
v.name = "docker-flask.test"
14+
v.memory = 1024
15+
v.cpus = 2
16+
v.customize ["modifyvm", :id, "--natdnshostresolver1", "on"]
17+
v.customize ["modifyvm", :id, "--ioapic", "on"]
18+
end
19+
20+
# Enable provisioning with Ansible.
21+
config.vm.provision "ansible" do |ansible|
22+
ansible.playbook = "provisioning/main.yml"
23+
end
24+
25+
end
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# Build a simple MySQL data volume Docker container.
2+
FROM busybox
3+
MAINTAINER Jeff Geerling <[email protected]>
4+
5+
# Create data volume for MySQL.
6+
RUN mkdir -p /var/lib/mysql
7+
VOLUME /var/lib/mysql
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# A simple MySQL container.
2+
FROM mysql:5.7
3+
MAINTAINER Jeff Geerling <[email protected]>
4+
5+
EXPOSE 3306

docker-flask/provisioning/docker.yml

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
---
2+
- name: Build Docker images from Dockerfiles.
3+
docker_image:
4+
name: "{{ item.name }}"
5+
tag: "{{ item.tag }}"
6+
source: build
7+
build:
8+
path: "/vagrant/provisioning/{{ item.directory }}"
9+
pull: false
10+
state: present
11+
with_items:
12+
- { name: data, tag: latest, directory: data }
13+
- { name: flask, tag: latest, directory: www }
14+
- { name: db, tag: latest, directory: db }
15+
16+
# Data containers don't need to be running to be utilized.
17+
- name: Run a Data container.
18+
docker_container:
19+
image: data:latest
20+
name: data
21+
state: present
22+
23+
- name: Run a Flask container.
24+
docker_container:
25+
image: flask:latest
26+
name: www
27+
state: started
28+
command: python3 /opt/www/index.py
29+
ports: "80:80"
30+
31+
- name: Run a MySQL container.
32+
docker_container:
33+
image: db:latest
34+
name: db
35+
state: started
36+
volumes_from: data
37+
ports: "3306:3306"
38+
env:
39+
MYSQL_ROOT_PASSWORD: root
40+
MYSQL_DATABASE: flask
41+
MYSQL_USER: flask
42+
MYSQL_PASSWORD: flask

docker-flask/provisioning/main.yml

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
---
2+
- hosts: all
3+
become: true
4+
5+
pre_tasks:
6+
- name: Update apt cache if needed.
7+
apt: update_cache=yes cache_valid_time=3600
8+
9+
roles:
10+
- role: geerlingguy.docker
11+
12+
tasks:
13+
- import_tasks: setup.yml
14+
- import_tasks: docker.yml

docker-flask/provisioning/setup.yml

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
---
2+
# You can add users to the docker group to allow them to manage docker
3+
# containers. This playbook uses sudo rather than the vagrant user because
4+
# Ansible uses ControlPersist for SSH connections. The first time this playbook
5+
# runs, the docker commands would fail if run by the vagrant user, since the
6+
# persisted connection state isn't aware of the addition of the 'docker' group.
7+
- name: Add vagrant user to docker group.
8+
user:
9+
name: vagrant
10+
groups: docker
11+
append: true
12+
13+
- name: Install Pip.
14+
apt: name=python3-pip state=present
15+
16+
- name: Install Docker Python library.
17+
pip: name=docker state=present
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# A simple Flask app container.
2+
FROM geerlingguy/docker-ubuntu2004-ansible
3+
MAINTAINER Jeff Geerling <[email protected]>
4+
5+
# Install Flask app dependencies.
6+
RUN apt-get update
7+
RUN apt-get install -y libmysqlclient-dev build-essential \
8+
python3-dev python3-pip
9+
RUN pip3 install flask flask-sqlalchemy mysqlclient
10+
11+
# Install playbook and run it.
12+
COPY playbook.yml /etc/ansible/playbook.yml
13+
COPY index.py.j2 /etc/ansible/index.py.j2
14+
COPY templates /etc/ansible/templates
15+
RUN mkdir -m 755 /opt/www
16+
RUN ansible-playbook /etc/ansible/playbook.yml --connection=local
17+
18+
EXPOSE 80

0 commit comments

Comments
 (0)