@@ -112,6 +112,51 @@ func NewDockerClient() (*DockerClient, error) {
112112 return & DockerClient {cli : cli }, nil
113113}
114114
115+ // ExecContainer executes a command inside a running container by name and returns the combined stdout/stderr.
116+ func (dc * DockerClient ) ExecContainer (containerName string , command []string ) (string , error ) {
117+ L .Info ().Strs ("Command" , command ).Str ("ContainerName" , containerName ).Msg ("Executing command" )
118+ ctx := context .Background ()
119+ containers , err := dc .cli .ContainerList (ctx , container.ListOptions {
120+ All : true ,
121+ })
122+ if err != nil {
123+ return "" , fmt .Errorf ("failed to list containers: %w" , err )
124+ }
125+ var containerID string
126+ for _ , cont := range containers {
127+ for _ , name := range cont .Names {
128+ if name == "/" + containerName {
129+ containerID = cont .ID
130+ break
131+ }
132+ }
133+ }
134+ if containerID == "" {
135+ return "" , fmt .Errorf ("container with name '%s' not found" , containerName )
136+ }
137+
138+ execConfig := container.ExecOptions {
139+ Cmd : command ,
140+ AttachStdout : true ,
141+ AttachStderr : true ,
142+ }
143+ execID , err := dc .cli .ContainerExecCreate (ctx , containerID , execConfig )
144+ if err != nil {
145+ return "" , fmt .Errorf ("failed to create exec instance: %w" , err )
146+ }
147+ resp , err := dc .cli .ContainerExecAttach (ctx , execID .ID , container.ExecStartOptions {})
148+ if err != nil {
149+ return "" , fmt .Errorf ("failed to attach to exec instance: %w" , err )
150+ }
151+ defer resp .Close ()
152+ output , err := io .ReadAll (resp .Reader )
153+ if err != nil {
154+ return "" , fmt .Errorf ("failed to read exec output: %w" , err )
155+ }
156+ L .Info ().Str ("Output" , string (output )).Msg ("Command output" )
157+ return string (output ), nil
158+ }
159+
115160// CopyFile copies a file into a container by name
116161func (dc * DockerClient ) CopyFile (containerName , sourceFile , targetPath string ) error {
117162 ctx := context .Background ()
@@ -340,55 +385,6 @@ func RemoveTestContainers() error {
340385 return nil
341386}
342387
343- // ExecContainer executes a command inside a running container by name and returns the combined stdout/stderr.
344- func ExecContainer (containerName string , command []string ) (string , error ) {
345- L .Info ().Strs ("Command" , command ).Str ("ContainerName" , containerName ).Msg ("Executing command" )
346- p , err := tc .NewDockerProvider ()
347- if err != nil {
348- return "" , err
349- }
350- ctx := context .Background ()
351- containers , err := p .Client ().ContainerList (ctx , container.ListOptions {
352- All : true ,
353- })
354- if err != nil {
355- return "" , fmt .Errorf ("failed to list containers: %w" , err )
356- }
357- var containerID string
358- for _ , cont := range containers {
359- for _ , name := range cont .Names {
360- if name == "/" + containerName {
361- containerID = cont .ID
362- break
363- }
364- }
365- }
366- if containerID == "" {
367- return "" , fmt .Errorf ("container with name '%s' not found" , containerName )
368- }
369-
370- execConfig := container.ExecOptions {
371- Cmd : command ,
372- AttachStdout : true ,
373- AttachStderr : true ,
374- }
375- execID , err := p .Client ().ContainerExecCreate (ctx , containerID , execConfig )
376- if err != nil {
377- return "" , fmt .Errorf ("failed to create exec instance: %w" , err )
378- }
379- resp , err := p .Client ().ContainerExecAttach (ctx , execID .ID , container.ExecStartOptions {})
380- if err != nil {
381- return "" , fmt .Errorf ("failed to attach to exec instance: %w" , err )
382- }
383- defer resp .Close ()
384- output , err := io .ReadAll (resp .Reader )
385- if err != nil {
386- return "" , fmt .Errorf ("failed to read exec output: %w" , err )
387- }
388- L .Info ().Str ("Output" , string (output )).Msg ("Command output" )
389- return string (output ), nil
390- }
391-
392388type ContainerResources struct {
393389 CPUs float64 `toml:"cpus" validate:"gte=0"`
394390 MemoryMb uint `toml:"memory_mb"`
0 commit comments