Sign inSign up

minghong/python

By minghong

•Updated almost 8 years ago

python with flask and redis

Image
0

111

minghong/python repository overview

  1. test.dockerfile

⁠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

COPY . /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"]

  1. requirement.txt Flask Redis

  2. app.py 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 = "cannot connect to Redis, counter disabled"

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)

Tag summary

Content type

Image

Digest

Size

46.1 MB

Last updated

almost 8 years ago

docker pull minghong/python:part2