Skip to content

Commit

Permalink
Add option to mount the docker socket
Browse files Browse the repository at this point in the history
  • Loading branch information
Julien Duchesne committed Oct 5, 2018
1 parent ce3288a commit 13023c6
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 2 deletions.
9 changes: 8 additions & 1 deletion docker.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,10 @@ import (
const (
minimumDockerVersion = "1.25"
tgfImageVersion = "TGF_IMAGE_VERSION"
dockerSocketFile = "/var/run/docker.sock"
)

func callDocker(args ...string) int {
func callDocker(withDockerMount bool, args ...string) int {
command := append([]string{config.EntryPoint}, args...)

// Change the default log level for terragrunt
Expand Down Expand Up @@ -71,6 +72,12 @@ func callDocker(args ...string) int {
"-v", fmt.Sprintf("%s%s:%s", convertDrive(currentDrive), rootFolder, filepath.ToSlash(filepath.Join("/", mountPoint, rootFolder))),
"-w", sourceFolder,
}

if withDockerMount {
withDockerMountArgs := []string{"-v", fmt.Sprintf(dockerSocketMountPattern, dockerSocketFile), "--group-add", getDockerGroup()}
dockerArgs = append(dockerArgs, withDockerMountArgs...)
}

if !noHome {
currentUser := must(user.Current()).(*user.User)
home := filepath.ToSlash(currentUser.HomeDir)
Expand Down
20 changes: 20 additions & 0 deletions docker_unix.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package main

import (
"github.com/coveo/gotemplate/utils"
"os"
"strings"
)

const dockerSocketMountPattern = "%[1]s:%[1]s"

func getDockerGroup() string {
cmd, tempFile, err := utils.GetCommandFromString("stat -c '%g' " + dockerSocketFile)
if err != nil {
panic(err)
}
if tempFile != "" {
defer func() { os.Remove(tempFile) }()
}
return strings.TrimSpace(string(must(cmd.Output()).([]byte)))
}
7 changes: 7 additions & 0 deletions docker_windows.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package main

const dockerSocketMountPattern = "/%[1]s:%[1]s"

func getDockerGroup() string {
return "root"
}
4 changes: 3 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,7 @@ func main() {
getAllVersions = app.Switch("all-versions", "Get versions of TGF & all others underlying utilities (alias --av)").Bool()
pruneImages = app.Switch("prune", "Remove all previous versions of the targeted image").Bool()
getCurrentVersion = app.Switch("current-version", "Get current version information (alias --cv)").Bool()
withDockerMount = app.Switch("with-docker-mount", "Mounts the docker socket to the image so the host's docker api is usable (alias --wd)").Bool()
entrypoint = app.Argument("entrypoint", "Override the entry point for docker", 'E').PlaceHolder("terragrunt").String()
image = app.Argument("image", "Use the specified image instead of the default one").PlaceHolder("coveo/tgf").String()
imageVersion = app.Argument("image-version", "Use a different version of docker image instead of the default one (alias --iv)").PlaceHolder("version").Default("-").String()
Expand All @@ -155,6 +156,7 @@ func main() {
app.Switch("nt", "alias for no-temp").Hidden().BoolVar(&noTemp)
app.Switch("cv", "alias for current-version").Hidden().BoolVar(getCurrentVersion)
app.Switch("av", "alias for all-versions").Hidden().BoolVar(getAllVersions)
app.Switch("wd", "alias for with-docker").Hidden().BoolVar(withDockerMount)
app.Argument("da", "alias for docker-arg").Hidden().StringsVar(&dockerOptions)
app.Argument("iv", "alias for image-version").Default("-").Hidden().StringVar(imageVersion)
app.Argument("mp", "alias for mount-point").Hidden().StringVar(&mountPoint)
Expand Down Expand Up @@ -234,7 +236,7 @@ func main() {
}
}

os.Exit(callDocker(unmanaged...))
os.Exit(callDocker(*withDockerMount, unmanaged...))
}

func validateVersion(version string) bool {
Expand Down

0 comments on commit 13023c6

Please sign in to comment.