3.5 使用 Dockerfiles

Deis 支持通过 Dockerfiles 部署应用程序。一个 Dockerfile 能自动化制作一个 Docker 镜像的步骤。Dockerfiles 是难以置信的强大,但是要求一些额外的工作来定义你需要的应用程序运行环境。

预先准备应用程序

如果你没有一个已经存在的应用程序,你可以 clone 一个示例应用程序来证明 Dockerfile 工作流。

  1. $ git clone https://github.com/deis/helloworld.git
  2. $ cd helloworld

Dockerfile 要求

为了部署 Dockerfile 应用程序,它们必须是的以下的需求一致:

  • Dockerfile 必须暴露(EXPOSE)仅仅一个端口
  • 暴露的端口必须是一个 HTTP 服务,可以连接到一个 HTTP router
  • 必须为正在运行的容器规定一个默认的 CMD

注意 Dockerfiles 暴露超过一个端口将在 issue 1156 讨论(hit)。

创建一个应用程序

使用 deis createController 创建一个应用程序。

  1. $ deis create
  2. Creating application... done, created folksy-offshoot
  3. Git remote deis added

使用 push 部署

使用 git push deis master 部署你的应用程序。

  1. $ git push deis master
  2. Counting objects: 13, done.
  3. Delta compression using up to 8 threads.
  4. Compressing objects: 100% (13/13), done.
  5. Writing objects: 100% (13/13), 1.99 KiB | 0 bytes/s, done.
  6. Total 13 (delta 2), reused 0 (delta 0)
  7. -----> Building Docker image
  8. Uploading context 4.096 kB
  9. Uploading context
  10. Step 0 : FROM deis/base:latest
  11. ---> 60024338bc63
  12. Step 1 : MAINTAINER OpDemand <info@opdemand.com>
  13. ---> Using cache
  14. ---> 2af5ad7f28d6
  15. Step 2 : RUN wget -O /tmp/go1.2.1.linux-amd64.tar.gz -q https://go.googlecode.com/files/go1.2.1.linux-amd64.tar.gz
  16. ---> Using cache
  17. ---> cf9ef8c5caa7
  18. Step 3 : RUN tar -C /usr/local -xzf /tmp/go1.2.1.linux-amd64.tar.gz
  19. ---> Using cache
  20. ---> 515b1faf3bd8
  21. Step 4 : RUN mkdir -p /go
  22. ---> Using cache
  23. ---> ebf4927a00e9
  24. Step 5 : ENV GOPATH /go
  25. ---> Using cache
  26. ---> c6a276eded37
  27. Step 6 : ENV PATH /usr/local/go/bin:/go/bin:$PATH
  28. ---> Using cache
  29. ---> 2ba6f6c9f108
  30. Step 7 : ADD . /go/src/github.com/deis/helloworld
  31. ---> 94ab7f4b977b
  32. Removing intermediate container 171b7d9fdb34
  33. Step 8 : RUN cd /go/src/github.com/deis/helloworld && go install -v .
  34. ---> Running in 0c8fbb2d2812
  35. github.com/deis/helloworld
  36. ---> 13b5af931393
  37. Removing intermediate container 0c8fbb2d2812
  38. Step 9 : ENV PORT 80
  39. ---> Running in 9b07da36a272
  40. ---> 2dce83167874
  41. Removing intermediate container 9b07da36a272
  42. Step 10 : CMD ["/go/bin/helloworld"]
  43. ---> Running in f7b215199940
  44. ---> b1e55ce5195a
  45. Removing intermediate container f7b215199940
  46. Step 11 : EXPOSE 80
  47. ---> Running in 7eb8ec45dcb0
  48. ---> ea1a8cc93ca3
  49. Removing intermediate container 7eb8ec45dcb0
  50. Successfully built ea1a8cc93ca3
  51. -----> Pushing image to private registry
  52. Launching... done, v2
  53. -----> folksy-offshoot deployed to Deis
  54. http://folksy-offshoot.local.deisapp.com
  55. To learn more, use `deis help` or visit http://deis.io
  56. To ssh://git@local.deisapp.com:2222/folksy-offshoot.git
  57. * [new branch] master -> master
  58. $ curl -s http://folksy-offshoot.local.deisapp.com
  59. Welcome to Deis!
  60. See the documentation at http://docs.deis.io/ for more information.

因为一个 Dockerfile 应用程序被检测到,在第一次部署的时,cmd 进程类型被自动扩展成 1。

定义进程类型

Docker 容器有默认的命令,通常由 CMD 指令规定。Deis 使用 cmd 进程类型引用这个默认命令。

Deis 也支持扩展其他的进程类型(process types)定义在一个 Procfile。为了使用这个功能,你必须:

  1. 使用你的 repository root 目录的 Procfile 定义进程类型(process types)
  2. 包含一个 start 可执行程序,可以使用 start <process-type> 调用