Skip to content

Instantly share code, notes, and snippets.

@yamamoto-febc
Last active June 30, 2016 11:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yamamoto-febc/7256da58368a7152108e764f2eabe7a3 to your computer and use it in GitHub Desktop.
Save yamamoto-febc/7256da58368a7152108e764f2eabe7a3 to your computer and use it in GitHub Desktop.
DockerとSwarmで既存アプリをサーバーレス化してみよう! ref: http://qiita.com/yamamoto-febc/items/1a92768729e0f5ee248b
package main
import (
"net/http"
"github.com/bfirsh/go-dcgi"
"github.com/docker/engine-api/client"
"github.com/docker/engine-api/types/container"
)
func main() {
// Dockerクライアントの初期化
cli, err := client.NewClient("unix:///var/run/docker.sock", "v1.23", nil, nil)
if err != nil {
panic(err)
}
// 起動するコンテナのネットワーク/ボリュームの設定
hostConfig := &container.HostConfig{
NetworkMode: "serverlessdockervotingapp_default",
Binds: []string{"/var/run/docker.sock:/var/run/docker.sock"},
}
// リクエストURLごとにDockerコンテナを起動して処理するハンドラを登録
http.Handle("/vote/", &dcgi.Handler{
Image: "bfirsh/serverless-vote",
Client: cli,
HostConfig: hostConfig,
Root: "/vote", // strip /vote from all URLs
})
http.Handle("/result/", &dcgi.Handler{
Image: "bfirsh/serverless-result",
Client: cli,
HostConfig: hostConfig,
Root: "/result",
})
// 80番ポートで待ち受け開始
http.ListenAndServe(":80", nil)
}
version: "2"
services:
entrypoint:
build: entrypoint
ports:
- 80:80
volumes:
- "/var/run/docker.sock:/var/run/docker.sock"
db:
image: postgres:9.4
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment