[Docker入门-2] Docker Containers 的创建和使用
DockerfileDockerfile 定义了容器内部环境,虚拟了网络、存储等资源,该环境与系统的其他部分隔离。外部访问服务资源时,Dockerfile会说明需要“复制”到哪些文件哪些环境创建Dockerfilevi Dockerfile文件内容如下:# Use an official Python runtime as a parent imageFROM python:2....
·
更多精彩内容:http://www.codeye.top/
Dockerfile
- Dockerfile 定义了容器内部环境,虚拟了网络、存储等资源,该环境与系统的其他部分隔离。外部访问服务资源时,Dockerfile会说明需要“复制”到哪些文件哪些环境
创建Dockerfile
vi 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"]
Dockerfile文件中包含了app.py和requirements.txt文件(说明即将进行哪些操作)
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)
requirements.txt文件内容
Flask
Redis
内容如下:
创建一个Docker镜像
docker build --tag=friendlyhello .
运行应用
docker run -p 4000:80 friendlyhello
更多推荐
已为社区贡献1条内容
所有评论(0)