Docker是.......html
以MacOS为例, 其余操做系统的docker安装与运行,请绕道至 Docker官网python
而后dmg文件安装便可.redis
和其余软件同样, 运行Docker便可docker
在 docker.io 上面注册你我的的帐号, 用于建立同步你我的的容器镜像flask
回到本机, 咱们使用命令行登入便可ubuntu
docker login ## 后续根据提示,输入你的帐号密码登入
官方给出了一个示例命令vim
docker run hello-world
运行结果以下:浏览器
控制打印了一段说明文字bash
Hello from Docker! This message shows that your installation appears to be working correctly. To generate this message, Docker took the following steps: 1. The Docker client contacted the Docker daemon. 2. The Docker daemon pulled the "hello-world" image from the Docker Hub. (amd64) 3. The Docker daemon created a new container from that image which runs the executable that produces the output you are currently reading. 4. The Docker daemon streamed that output to the Docker client, which sent it to your terminal. To try something more ambitious, you can run an Ubuntu container with: $ docker run -it ubuntu bash Share images, automate workflows, and more with a free Docker ID: https://cloud.docker.com/ For more examples and ideas, visit: https://docs.docker.com/engine/userguide/
大体介绍了 docker run xxxx
命令后,docker内部帮我完成的后续工做
官网给出了一个docker运行python的web应用的demo. 详细步骤以下:
# 我根据我的代码习惯, 建立了以下目录, (其实咱们只须要一个新的有操做权限的目录空间) mkdir -p ~/code/docker/demo-python cd ~/code/docker/demo-python
# 切换到工做目录 cd ~/code/docker/demo-python # 建立 Dockerfile touch Dockerfile vim Dockerfile # 编辑文件 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
和 app.py
Flask Redis
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)
# -t 参数为 指定tagname docker build -t demopython . # 查看刚构建的镜像 docker images
# -p 参数: 使用本机4000端口映射容器中80端口服务 docker run -p 4000:80 demopython
打开浏览器访问服务 http://localhost:4000