Skip to content

Instantly share code, notes, and snippets.

@koenbollen
Last active April 4, 2017 14:25
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 koenbollen/312ad5348c03dcbf005167e2b156fec4 to your computer and use it in GitHub Desktop.
Save koenbollen/312ad5348c03dcbf005167e2b156fec4 to your computer and use it in GitHub Desktop.

Docker Deploy Hook

The Docker Deploy Hook project is a simple docker image that runs a small go server that listens on a specified, secret, endpoint and pulls and re-deploys another docker container.

We use this at Blendle to auto deploy our docker service when our CI calls this hook.


Example

docker run -P -v /var/run/docker.sock:/var/run/docker.sock -e ENDPOINT=/secret -e USERNAME=hubuser -e PASSWORD=hubpass -e "CMD=sleep 500" blendle/ddh

Environment (options)

The go script uses these environment variables as config:

ENV Default Description
PASSWORD Your dockerhub username when pulling an image
USERNAME Your dockerhub password when pulling an image
DOCKERSOCKET unix:///var/run/docker.sock The URI used to connect to the Docker host
ENDPOINT / The secret endpoint that triggers the deploy (CHANGE THIS!)
IMAGE ubuntu The image name to pull and re-create
CONTAINER_NAME ubuntu-test The name of the instance container to kill and start again
CMD The command to run inside the deployed container, can be empty
PASS_ENV PASS_ENV A space seperated list of environment variables to pass on to the newly created container
LINKS Links for the docker container, format is "container:alias" seperated by space
package main
import (
"fmt"
"os"
"strings"
"github.com/fsouza/go-dockerclient"
"github.com/go-martini/martini"
)
func main() {
endpoint := os.Getenv("ENDPOINT")
imageName := os.Getenv("IMAGE")
containerName := os.Getenv("CONTAINER_NAME")
username := os.Getenv("USERNAME")
password := os.Getenv("PASSWORD")
if len(username) == 0 || len(password) == 0 {
fmt.Println("missing USERNAME/PASSWORD env!")
os.Exit(1)
}
dockerURI := os.Getenv("DOCKERSOCKET")
client, _ := docker.NewClient(dockerURI)
err := client.Ping()
if err != nil {
fmt.Println("unable to connect to docker:", err)
fmt.Println("(did you use `docker run -v /var/run/docker.sock:/var/run/docker.sock ...`?)")
os.Exit(1)
}
image := docker.PullImageOptions{Repository: imageName, Tag: "latest"}
auth := docker.AuthConfiguration{Username: username, Password: password}
passes := strings.Split(os.Getenv("PASS_ENV"), " ")
envs := make([]string, len(passes))
for i, env := range passes {
envs[i] = env + "=" + os.Getenv(env)
}
config := docker.Config{Image: imageName, Env: envs}
if len(os.Getenv("CMD")) != 0 {
// TODO: Parse this CMD string as bashlike:
config.Cmd = strings.Split(os.Getenv("CMD"), " ")
}
create := docker.CreateContainerOptions{Name: containerName, Config: &config}
hostConfig := docker.HostConfig{PublishAllPorts: true}
links := os.Getenv("LINKS")
if len(links) != 0 {
hostConfig.Links = strings.Split(links, " ")
}
deploy := make(chan int, 100)
go func() {
for {
_ = <-deploy
fmt.Println("Pulling image:", imageName)
client.PullImage(image, auth)
fmt.Println("Removing old container:", containerName)
client.RemoveContainer(docker.RemoveContainerOptions{ID: containerName, Force: true})
fmt.Println("Creating new container:", containerName)
container, err := client.CreateContainer(create)
if err != nil {
fmt.Println("Unable to create new container:", err)
} else {
fmt.Println("Starting container:", container.ID)
client.StartContainer(container.ID, &hostConfig)
}
}
}()
m := martini.Classic()
m.Get(endpoint, func() string {
deploy <- 0
return "OK\n"
})
m.Post(endpoint, func() string {
deploy <- 0
return "OK\n"
})
m.RunOnAddr(":8080")
}
FROM google/golang
MAINTAINER Koen Bollen <koen@blendle.com>
RUN go get github.com/go-martini/martini
RUN go get github.com/fsouza/go-dockerclient
ENV DOCKERSOCKET unix:///var/run/docker.sock
ENV ENDPOINT /
ENV IMAGE ubuntu
ENV CONTAINER_NAME ubuntu-test
#ENV CMD sleep 500
#ENV USERNAME you
#ENV PASSWORD secret
#ENV LINKS "your-db:db something-redis:redis"
ENV PASS_ENV PASS_ENV
EXPOSE 8080
WORKDIR /gopath/src/app
ADD . /gopath/src/app/
ENTRYPOINT go run /gopath/src/app/main.go
Copyright (c) 2015 Blendle
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment