Created
July 17, 2019 21:56
-
-
Save ahamid/6c8b1523494cd515e9f2e2a6a8a53555 to your computer and use it in GitHub Desktop.
Docker tutorial part3
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
version: "3" | |
services: | |
web: | |
# replace username/repo:tag with your name and image details | |
image: aaronh0/get-started:part2 | |
deploy: | |
replicas: 1 | |
resources: | |
limits: | |
cpus: "0.1" | |
memory: 50M | |
restart_policy: | |
condition: on-failure | |
ports: | |
- "4000:80" | |
networks: | |
- webnet | |
networks: | |
webnet: |
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 | |
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"] |
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