Last active
December 5, 2017 14:09
-
-
Save kingbuzzman/b48be91757fc60e97f6a9a189d006bd8 to your computer and use it in GitHub Desktop.
Simple one step setup for: https://docs.docker.com/get-started/part2
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from flask import Flask | |
from redis import Redis, RedisError | |
import os | |
import socket | |
# Connect to Redis | |
redis = Redis(host="redis", db=0, socket_connect_timeout=2, socket_timeout=2) | |
app = Flask(__name__) | |
@app.route("/") | |
def hello(): | |
try: | |
visits = redis.incr("counter") | |
except RedisError: | |
visits = "<i>cannot connect to Redis, counter disabled</i>" | |
html = "<h3>Hello {name}!</h3>" \ | |
"<b>Hostname:</b> {hostname}<br/>" \ | |
"<b>Visits:</b> {visits}" | |
return html.format(name=os.getenv("NAME", "world"), hostname=socket.gethostname(), visits=visits) | |
if __name__ == "__main__": | |
app.run(host='0.0.0.0', port=80) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Use an official Python runtime as a parent image | |
FROM python:2.7-slim | |
# Set the working directory to /app | |
WORKDIR /app | |
# Copy the current directory contents into the container at /app | |
ADD . /app | |
# Install any needed packages specified in requirements.txt | |
RUN pip install --trusted-host pypi.python.org -r requirements.txt | |
# Make port 80 available to the world outside this container | |
EXPOSE 80 | |
# Define environment variable | |
ENV NAME World | |
# Run app.py when the container launches | |
CMD ["python", "app.py"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Flask | |
Redis |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Created for: https://stackoverflow.com/questions/47630767/docker-get-startedpython-not-working/47655089
To run this, do: (this will download all 3 files shown here, build the docker image, run the container, curl the url, then exit)
if you want to keep it running for longer:
and then do whatever you want -- use curl/browser what have you..., when you're done, kill it (
CTRL-C
)