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