[root@linux-node2 ~]# mkdir /docker_file/test/ -p
[root@linux-node2 ~]# cd /docker_file/test/
[root@linux-node2 test]# vim 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"]
[root@linux-node2 test]# vim requirements.txt
Flask
Redis
[root@linux-node2 test]# vim 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 = "<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)
[root@linux-node2 test]# ls
app.py Dockerfile requirements.txt
[root@linux-node2 test]# docker build -t test_hello .
Sending build context to Docker daemon 5.12kB
Step 1/7 : FROM python:2.7-slim
2.7-slim: Pulling from library/python
f17d81b4b692: Pull complete
7429ec5d1bbc: Pull complete
45b34d043e88: Pull complete
49d33f4617f3: Pull complete
Digest: sha256:3b9c77ba2cdb829f6d41cb64a3e6b3fb7f40a9143648c506864b7fbf272dc77e
Status: Downloaded newer image for python:2.7-slim
---> 804b0a01ea83
Step 2/7 : WORKDIR /app
---> Running in d0e518da3b5c
Removing intermediate container d0e518da3b5c
---> 5b2dab7e55c4
Step 3/7 : COPY . /app
---> 9a2e0140a18f
Step 4/7 : RUN pip install --trusted-host pypi.python.org -r requirements.txt
---> Running in 6b607fb50a79
Collecting Flask (from -r requirements.txt (line 1))
Downloading https://files.pythonhosted.org/packages/7f/e7/08578774ed4536d3242b14dacb4696386634607af824ea997202cd0edb4b/Flask-1.0.2-py2.py3-none-any.whl (91kB)
Collecting Redis (from -r requirements.txt (line 2))
Downloading https://files.pythonhosted.org/packages/3b/f6/7a76333cf0b9251ecf49efff635015171843d9b977e4ffcf59f9c4428052/redis-2.10.6-py2.py3-none-any.whl (64kB)
Collecting itsdangerous>=0.24 (from Flask->-r requirements.txt (line 1))
Downloading https://files.pythonhosted.org/packages/76/ae/44b03b253d6fade317f32c24d100b3b35c2239807046a4c953c7b89fa49e/itsdangerous-1.1.0-py2.py3-none-any.whl
Collecting Jinja2>=2.10 (from Flask->-r requirements.txt (line 1))
Downloading https://files.pythonhosted.org/packages/7f/ff/ae64bacdfc95f27a016a7bed8e8686763ba4d277a78ca76f32659220a731/Jinja2-2.10-py2.py3-none-any.whl (126kB)
Collecting Werkzeug>=0.14 (from Flask->-r requirements.txt (line 1))
Downloading https://files.pythonhosted.org/packages/20/c4/12e3e56473e52375aa29c4764e70d1b8f3efa6682bef8d0aae04fe335243/Werkzeug-0.14.1-py2.py3-none-any.whl (322kB)
Collecting click>=5.1 (from Flask->-r requirements.txt (line 1))
Downloading https://files.pythonhosted.org/packages/fa/37/45185cb5abbc30d7257104c434fe0b07e5a195a6847506c074527aa599ec/Click-7.0-py2.py3-none-any.whl (81kB)
Collecting MarkupSafe>=0.23 (from Jinja2>=2.10->Flask->-r requirements.txt (line 1))
Downloading https://files.pythonhosted.org/packages/4d/de/32d741db316d8fdb7680822dd37001ef7a448255de9699ab4bfcbdf4172b/MarkupSafe-1.0.tar.gz
Building wheels for collected packages: MarkupSafe
Running setup.py bdist_wheel for MarkupSafe: started
Running setup.py bdist_wheel for MarkupSafe: finished with status 'done'
Stored in directory: /root/.cache/pip/wheels/33/56/20/ebe49a5c612fffe1c5a632146b16596f9e64676768661e4e46
Successfully built MarkupSafe
Installing collected packages: itsdangerous, MarkupSafe, Jinja2, Werkzeug, click, Flask, Redis
Successfully installed Flask-1.0.2 Jinja2-2.10 MarkupSafe-1.0 Redis-2.10.6 Werkzeug-0.14.1 click-7.0 itsdangerous-1.1.0
Removing intermediate container 6b607fb50a79
---> 12fb4ab34b57
Step 5/7 : EXPOSE 80
---> Running in 8b15fb767dbc
Removing intermediate container 8b15fb767dbc
---> f97bdebfeda1
Step 6/7 : ENV NAME World
---> Running in c3f41a441bd4
Removing intermediate container c3f41a441bd4
---> 717a1c74af73
Step 7/7 : CMD ["python", "app.py"]
---> Running in fd4d4c918250
Removing intermediate container fd4d4c918250
---> e9ece21d7dbb
Successfully built e9ece21d7dbb
Successfully tagged test_hello:latest
[root@linux-node2 test]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
test_hello latest e9ece21d7dbb 10 minutes ago 132MB
python 2.7-slim 804b0a01ea83 2 weeks ago 120MB
hello-world latest 4ab4c602aa5e 7 weeks ago 1.84kB
[root@linux-node2 test]# docker run -p 6666:80 test_hello
Dockerfile
最新推荐文章于 2024-10-10 15:08:54 发布