Skip to content

Commit

Permalink
Improve docker compose setup and clean-up
Browse files Browse the repository at this point in the history
Signed-off-by: Byron Ruth <[email protected]>
  • Loading branch information
bruth committed Aug 4, 2022
1 parent 39a2a1c commit 029f93d
Showing 1 changed file with 37 additions and 17 deletions.
54 changes: 37 additions & 17 deletions cmd/nbe/docker.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"io/ioutil"
"os"
"os/exec"
"os/signal"
"path/filepath"
"strings"

Expand Down Expand Up @@ -259,34 +260,40 @@ func (r *ComposeRunner) Run(imageTag string) error {
return err
}

err = copyFile(composeFile, filepath.Join(buildDir, "docker-compose.yaml"))
if err != nil {
return err
}

err = createFile(filepath.Join(buildDir, ".env"), []byte(fmt.Sprintf("IMAGE_TAG=%s", imageTag)))
if err != nil {
return fmt.Errorf("create .env: %w", err)
}

// Best effort to bring containers down..
defer exec.Command(
"docker",
"compose",
"--project-name", uid,
"--project-directory", buildDir,
"--file", composeFile,
"down",
"--remove-orphans",
"--timeout", "3",
).Run()
defer func() {
cmd := exec.Command(
"docker",
"compose",
"--project-name", uid,
"down",
"--remove-orphans",
"--timeout", "3",
)
cmd.Dir = buildDir
cmd.Run()
}()

cmd := exec.Command(
"docker",
"compose",
"--project-name", uid,
"--project-directory", buildDir,
"--file", composeFile,
"pull",
"--include-deps",
"--quiet",
"--ignore-pull-failures",
)
cmd.Dir = buildDir

stderrb := bytes.NewBuffer(nil)
cmd.Stderr = stderrb
Expand All @@ -300,8 +307,6 @@ func (r *ComposeRunner) Run(imageTag string) error {
"docker",
"compose",
"--project-name", uid,
"--project-directory", buildDir,
"--file", composeFile,
"up",
)
} else {
Expand All @@ -316,18 +321,33 @@ func (r *ComposeRunner) Run(imageTag string) error {
"compose",
"--ansi", ansi,
"--project-name", uid,
"--project-directory", buildDir,
"--file", composeFile,
"run",
"--no-TTY",
"--rm",
"app",
)
}

cmd.Dir = buildDir
cmd.Stdout = stdout
cmd.Stderr = stderr
cmd.Stdin = stdin

return cmd.Run()
done := make(chan error, 1)
sigch := make(chan os.Signal, 1)

signal.Notify(sigch, os.Interrupt)

go func() {
done <- cmd.Run()
}()

// Wait for interrupt or once the command finishes.
// This ensures the `docker compose down` runs.
select {
case err := <-done:
return err
case <-sigch:
return nil
}
}

1 comment on commit 029f93d

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.