Skip to content

Commit

Permalink
Add dockerimage.BuildFromContainerfile
Browse files Browse the repository at this point in the history
  • Loading branch information
AndreasBergmeier6176 committed Aug 22, 2022
1 parent f157c69 commit 52dc295
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 5 deletions.
20 changes: 15 additions & 5 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,16 @@
# Changelog

# v0.0.4
- Add initial `gke` package.
- Add function `AddAuthForCluster` for having local config.
- Add function `StreamContainerLog` for delegating a containers remote log to local files.
- Add new type `PodRun` for executing a Pod for testing.
## v0.0.5 - Migrate `dockerimage` to _sherlock-mavrodi_

- Add function `dockerimage.BuildFromContainerfile` for building Container Image.

**Bugfixes**
- Correct docs of `StreamContainerLog`


---
## v0.0.4 - Migrate `gke` package to _sherlock-mavrodi_

- Add function `gke.AddAuthForCluster` for having local config.
- Add function `gke.StreamContainerLog` for delegating a containers remote log to local files.
- Add new type `gke.test.PodRun` for executing a Pod for testing.
17 changes: 17 additions & 0 deletions pkg/dockerimage/build.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package dockerimage

import "os"

// BuildFromContainerfile builds a Container image from a Containerfile.
// Invokes docker via CLI since Docker's dependencies and API are beyond brittle.
func BuildFromContainerfile(path string, tag string) error {

dir, _ := os.Getwd()
os.Chdir(path)
defer os.Chdir(dir)
// Normally you would want to use a Golang API here.
// Since the Docker API is a very thin shim, badly documented
// and talking to Dockerd or Buildkit has a lot of quirks
// just call the binary here and be done with it.
return executeDockerCommand("build", "-f", "Containerfile", ".", "-t", tag)
}
42 changes: 42 additions & 0 deletions pkg/dockerimage/exec.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package dockerimage

import (
"io"
"os"
"os/exec"
"sync"
)

// executeDockerCommandWithStdout calls docker CLI with `arg`.
// Ensures that output is written to provided `io.Writer`.
func executeDockerCommandWithStdout(stdout io.Writer, arg ...string) error {
cmd := exec.Command("docker", arg...)
errReader, err := cmd.StderrPipe()
if err != nil {
return err
}
defer errReader.Close()
outReader, err := cmd.StdoutPipe()
if err != nil {
return err
}
defer outReader.Close()
wg := sync.WaitGroup{}
wg.Add(2)
go func() {
defer wg.Done()
io.Copy(os.Stderr, errReader)
}()
go func() {
defer wg.Done()
io.Copy(stdout, outReader)
}()
err = cmd.Run()
wg.Wait()
return err
}

// executeDockerCommand call docker CLI with `arg`.
func executeDockerCommand(arg ...string) error {
return executeDockerCommandWithStdout(os.Stdout, arg...)
}

0 comments on commit 52dc295

Please sign in to comment.