-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathpythonDocker.txt
More file actions
74 lines (47 loc) · 1.63 KB
/
pythonDocker.txt
File metadata and controls
74 lines (47 loc) · 1.63 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
1) sudo nano 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"]
2) sudo nano requirements.txt
Flask
Redis
3) sudo nano requirements.txt
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)
4) docker build -t friendlyhello .
5) docker run -p 4000:80 friendlyhello
6)curl http://localhost:4000
7)sudo docker images ls -a
8)sudo docker tag 6edf906dcc81 andalike/friendlyhello
9)sudo docker build -t andalike/friendlyhello .
10)sudo docker run -p 4000:80 andalike/friendlyhello
11)sudo docker tag 6edf906dcc81 andalike/friendlyhello
12)sudo docker push andalike/friendlyhello