Skip to content

Instantly share code, notes, and snippets.

@e-roux
Last active February 21, 2019 06:51
Show Gist options
  • Save e-roux/9720d887f053b519565161cdb74fa19f to your computer and use it in GitHub Desktop.
Save e-roux/9720d887f053b519565161cdb74fa19f to your computer and use it in GitHub Desktop.
#!/bin/bash
# From https://docs.docker.com/compose/gettingstarted/
cd /tmp
# Create a directory for the project
mkdir composetest
cd composetest
# Create a file called app.py in your project directory
cat << EOF > app.py
import time
import redis
from flask import Flask
app = Flask(__name__)
# In this example, redis is the hostname of the redis container
# on the application’s network.
# We use the default port for Redis, 6379
cache = redis.Redis(host='redis', port=6379)
def get_hit_count():
retries = 5
while True:
try:
return cache.incr('hits')
except redis.exceptions.ConnectionError as exc:
if retries == 0:
raise exc
retries -= 1
time.sleep(0.5)
@app.route('/')
def hello():
count = get_hit_count()
return f'Hello World! I have been seen {count} times.\n'
if __name__ == "__main__":
app.run(host="0.0.0.0", debug=True)
EOF
# Create requirements.txt in your project directory
cat << EOF > requirements.txt
flask
redis
EOF
# This tells Docker to:
# Build an image starting with the Python image.
# Add the current directory . into the path /code in the image.
# Set the working directory to /code.
# Install the Python dependencies.
# Set the default command for the container to python app.py.
cat << EOF > Dockerfile
FROM python:alpine
ADD . /code
WORKDIR /code
RUN pip install -r requirements.txt
CMD ["python", "app.py"]
EOF
cat << EOF > docker-compose.yml
version: '3'
services:
web:
build: .
ports:
- "5000:5000"
volumes:
- .:/code
redis:
image: "redis:alpine"
EOF
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment