# Docker 部署(Docker Deployment)

# 介绍(Introduction)

长期以来,环境一直是部署的难题。如果您的项目中有冲突的配置,您将不得不花费大量时间来解决它们。幸运的是,虚拟化为我们提供了一个很好的解决思路。Docker 就是其中之一。如果您不了解 Docker,可以访问 Docker 官网 (opens new window) 了解更多。

# 构建镜像(Build Image)

我们以一个最简单的 Sanic 工程作为例子。假设项目SanicDocker 的路径是/path/to/SanicDocker

目录结构如右侧所示:

# /path/to/SanicDocker
SanicDocker
├── requirements.txt
├── dockerfile
└── server.py

server.py代码如右侧所示:

app = Sanic("MySanicApp")
@app.get('/')
async def hello(request):
    return text("OK!")
if __name__ == '__main__':
    app.run(host='0.0.0.0', port=8000)

小提示

请注意这里的 host 不能为 127.0.0.1 。在 docker 容器中,127.0.0.1 只表示本机本容器中的一个虚拟网卡,只接受本容器中的应用相互通讯。更多信息请参考 Docker 网络 (opens new window)

代码准备好后,我们编写 Dockerfile 文件,其内容如下:


FROM sanicframework/sanic:3.8-latest
WORKDIR /sanic
COPY . .
RUN pip install -r requirements.txt
EXPOSE 8000
CMD ["python", "server.py"]

运行下面的命令,构建镜像:

docker build -t my-sanic-image .

# 启动容器(Start Container)

构建完镜像,我们将用构建好的 my-sanic-image 镜像来启动容器。

docker run --name mysanic -p 8000:8000 -d my-sanic-image

此时,在浏览器中访问 http://localhost:8000/ 即可看见 Sanic 服务器的返回结果

OK!

# 使用 docker-compose 编排(Use docker-compose)

当您的项目中涵盖了多个不同的组件或服务时,您可以使用 docker-compose (opens new window) 来编排容器。以刚才打包好的 my-sanic-imagenginx 为例,我们来实现通过 nginx 代理 Sanic 服务器。

首先我们需要准备 nginx 的配置文件,将其命名为 mysanic.conf 并写入内容:

server {
    listen 80;
    listen [::]:80;
    location / {
      proxy_pass http://mysanic:8000/;
      proxy_set_header Upgrade $http_upgrade;
      proxy_set_header Connection upgrade;
      proxy_set_header Accept-Encoding gzip;
    }
}

然后我们来准备 docker-compose.yml 文件,其内容为:

version: "3"
services:
  mysanic:
    image: my-sanic-image
    ports:
      - "8000:8000"
    restart: always
  mynginx:
    image: nginx:1.13.6-alpine
    ports:
      - "80:80"
    depends_on:
      - mysanic
    volumes:
      - ./mysanic.conf:/etc/nginx/conf.d/mysanic.conf
    restart: always
networks:
  default:
    driver: bridge

接下来我们来启动容器:

docker-compose up -d

此时,在浏览器中访问 http://localhost:80/ 即可看见 Sanic 服务器的返回结果

OK!
MIT Licensed
Copyright © 2018-present Sanic Community Organization

~ Made with ❤️ and ☕️ ~