55 "bytes"
66 "context"
77 "encoding/binary"
8- "errors"
98 "fmt"
109 "github.com/docker/docker/api/types/container"
1110 "github.com/docker/docker/client"
@@ -15,6 +14,7 @@ import (
1514 "golang.org/x/sync/errgroup"
1615 "io"
1716 "os"
17+ "os/exec"
1818 "path/filepath"
1919 "strings"
2020 "sync"
@@ -69,6 +69,14 @@ func DefaultTCName(name string) string {
6969 return fmt .Sprintf ("%s-%s" , name , uuid .NewString ()[0 :5 ])
7070}
7171
72+ // runCommand executes a command and prints the output.
73+ func runCommand (name string , args ... string ) error {
74+ cmd := exec .Command (name , args ... )
75+ cmd .Stdout = os .Stdout
76+ cmd .Stderr = os .Stderr
77+ return cmd .Run ()
78+ }
79+
7280// DockerClient wraps a Docker API client and provides convenience methods
7381type DockerClient struct {
7482 cli * client.Client
@@ -242,29 +250,19 @@ func WriteAllContainersLogs() error {
242250 return eg .Wait ()
243251}
244252
245- func BuildImageOnce (ctx context.Context , once * sync.Once , dctx , dfile , nameAndTag string ) error {
246- var (
247- p * tc.DockerProvider
248- dockerCtx string
249- err error
250- )
253+ func BuildImageOnce (once * sync.Once , dctx , dfile , nameAndTag string ) error {
254+ var err error
251255 once .Do (func () {
252- nt := strings . Split ( nameAndTag , ":" )
253- if len ( nt ) != 2 {
254- err = errors . New ( "BuildImageOnce, tag must be in 'repo:tag' format" )
255- return
256+ dfilePath := filepath . Join ( dctx , dfile )
257+ err = runCommand ( "docker" , "build" , "-t" , nameAndTag , "-f" , dfilePath , dctx )
258+ if err != nil {
259+ err = fmt . Errorf ( "failed to build Docker image: %w" , err )
256260 }
257- p , err = tc .NewDockerProvider ()
258- dockerCtx , err = filepath .Abs (dctx )
259- _ , err = p .BuildImage (ctx , & tc.ContainerRequest {
260- FromDockerfile : tc.FromDockerfile {
261- Repo : nt [0 ],
262- Tag : nt [1 ],
263- Context : dockerCtx ,
264- Dockerfile : dfile ,
265- PrintBuildLog : true ,
266- },
267- })
268261 })
269262 return err
270263}
264+
265+ func BuildImage (dctx , dfile , nameAndTag string ) error {
266+ dfilePath := filepath .Join (dctx , dfile )
267+ return runCommand ("docker" , "build" , "-t" , nameAndTag , "-f" , dfilePath , dctx )
268+ }
0 commit comments