Last active
June 30, 2016 11:40
-
-
Save yamamoto-febc/7256da58368a7152108e764f2eabe7a3 to your computer and use it in GitHub Desktop.
DockerとSwarmで既存アプリをサーバーレス化してみよう! ref: http://qiita.com/yamamoto-febc/items/1a92768729e0f5ee248b
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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