Skip to content

Instantly share code, notes, and snippets.

@kingbuzzman
Last active December 5, 2017 14:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kingbuzzman/b48be91757fc60e97f6a9a189d006bd8 to your computer and use it in GitHub Desktop.
Save kingbuzzman/b48be91757fc60e97f6a9a189d006bd8 to your computer and use it in GitHub Desktop.
Simple one step setup for: https://docs.docker.com/get-started/part2
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)
# 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"]
@kingbuzzman
Copy link
Author

kingbuzzman commented Dec 5, 2017

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)

mkdir -p dockerapp && \
curl -sL https://gist.github.com/kingbuzzman/b48be91757fc60e97f6a9a189d006bd8/download | tar -xvz -C dockerapp --strip-components=1 && \
docker build -t friendlyhello dockerapp && \
docker run --rm -p 4000:80 --name friendlyhello -d friendlyhello && \
sleep 2 && \
curl -s http://localhost:4000/ && \
docker kill friendlyhello  

if you want to keep it running for longer:

docker run --rm -p 4000:80 friendlyhello

and then do whatever you want -- use curl/browser what have you..., when you're done, kill it (CTRL-C)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment