@@ -157,6 +157,46 @@ func (dc *DockerClient) ExecContainer(containerName string, command []string) (s
157157 return string (output ), nil
158158}
159159
160+ // ExecContainer executes a command inside a running container by name and returns the combined stdout/stderr.
161+ func (dc * DockerClient ) ExecContainerOptions (containerName string , execConfig container.ExecOptions ) (string , error ) {
162+ L .Info ().Strs ("Command" , execConfig .Cmd ).Str ("ContainerName" , containerName ).Msg ("Executing command" )
163+ ctx := context .Background ()
164+ containers , err := dc .cli .ContainerList (ctx , container.ListOptions {
165+ All : true ,
166+ })
167+ if err != nil {
168+ return "" , fmt .Errorf ("failed to list containers: %w" , err )
169+ }
170+ var containerID string
171+ for _ , cont := range containers {
172+ for _ , name := range cont .Names {
173+ if name == "/" + containerName {
174+ containerID = cont .ID
175+ break
176+ }
177+ }
178+ }
179+ if containerID == "" {
180+ return "" , fmt .Errorf ("container with name '%s' not found" , containerName )
181+ }
182+
183+ execID , err := dc .cli .ContainerExecCreate (ctx , containerID , execConfig )
184+ if err != nil {
185+ return "" , fmt .Errorf ("failed to create exec instance: %w" , err )
186+ }
187+ resp , err := dc .cli .ContainerExecAttach (ctx , execID .ID , container.ExecStartOptions {})
188+ if err != nil {
189+ return "" , fmt .Errorf ("failed to attach to exec instance: %w" , err )
190+ }
191+ defer resp .Close ()
192+ output , err := io .ReadAll (resp .Reader )
193+ if err != nil {
194+ return "" , fmt .Errorf ("failed to read exec output: %w" , err )
195+ }
196+ L .Info ().Str ("Output" , string (output )).Msg ("Command output" )
197+ return string (output ), nil
198+ }
199+
160200// CopyFile copies a file into a container by name
161201func (dc * DockerClient ) CopyFile (containerName , sourceFile , targetPath string ) error {
162202 ctx := context .Background ()
0 commit comments