Skip to content

Instantly share code, notes, and snippets.

@wware
Last active June 21, 2018 15:52
Show Gist options
  • Save wware/0836322f1d7f600880ecc666a671f0e6 to your computer and use it in GitHub Desktop.
Save wware/0836322f1d7f600880ecc666a671f0e6 to your computer and use it in GitHub Desktop.

Messing with Ansible and Docker

Nobody can remember how to use Docker, it is far too confusing. So here are some hints.

sudo docker build -t simple_flask:dockerfile .
sudo docker run -p 5000:5000 simple_flask:dockerfile python hello.py

My actual intention is to learn Ansible. But in order to run Ansible you need Docker, unless you happen to have a rackful of machines at your disposal which I do not.

Basic tinkering

Look at these instructions to install Ansible properly. More Ansible stuff here but it does not talk about using Docker. Wait, this one might work. Honorable mention for 1 and 2.

Working from this page, I have started to make Ansible do some things. Here we start and then stop three HTTP servers.

sudo ansible-playbook docker_go.yml

curl http://127.0.0.1:5000/   ==> value1
curl http://127.0.0.1:5001/   ==> value2
curl http://127.0.0.1:5002/   ==> value3

sudo ansible-playbook docker_stop.yml

So that is pretty cool already, and I know Ansible has a bunch more stuff like applying names to groups of machines (webserver, database, etc) so that you can run bunches of commands on them. Slick.

Using Ansible to set up SSH communications

---
- hosts: localhost
tasks:
- name: Pull Ubuntu image
docker_image:
name: ubuntu
- name: Build simple Flask server
docker_image:
path: .
name: simple_flask
tag: 0.1
- name: Create Flask server container1
docker_container:
name: my-flask1
image: simple_flask:0.1
ports:
- "5000:5000"
env:
KEY: value1
command: python hello.py
- name: Create Flask server container2
docker_container:
name: my-flask2
image: simple_flask:0.1
ports:
- "5001:5000"
env:
KEY: value2
command: python hello.py
- name: Create Flask server container3
docker_container:
name: my-flask3
image: simple_flask:0.1
ports:
- "5002:5000"
env:
KEY: value3
command: python hello.py
---
- hosts: localhost
tasks:
- name: Remove Flask server container1
docker_container:
name: my-flask1
state: absent
- name: Remove Flask server container2
docker_container:
name: my-flask2
state: absent
- name: Remove Flask server container3
docker_container:
name: my-flask3
state: absent
FROM ubuntu
Maintainer Will
RUN apt-get update
RUN apt-get install -y python python-pip wget
RUN pip install Flask
ADD hello.py /home/hello.py
WORKDIR /home
import os
from flask import Flask
from werkzeug import serving
app = Flask(__name__)
@app.route("/")
def hello():
return "Hello World! This is Flask ({0}).\n".format(os.environ.get("KEY"))
serving.run_simple("0.0.0.0", 5000, app)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment