`
apple578994358
  • 浏览: 2962 次
  • 性别: Icon_minigender_1
  • 来自: 深圳
社区版块
存档分类
最新评论

docker安装以及dockerFile第一个例子

 
阅读更多
step1
yum install docker
step2
docker --version
step3
systemctl docker start
step4
docker images
step5.mkdir app
step6
cd app    依次创建三个文件 DockerFile  requirements.txt    app.py
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
ADD . /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"]

requirements.txt
Flask
Redis

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)


docker build -t friendlyhello .

docker images

后台运行
docker run -d -p 4000:80 friendlyhello



测试
curl http://localhost:4000  或在浏览器输入  http://localhost:4000 看到信息表示第一个例子成功
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics