replace docker cli to docker sdk#1144
Conversation
Signed-off-by: nXtCyberNet <rohantech2005@gmail.com>
bestbeforetoday
left a comment
There was a problem hiding this comment.
Just an initial review pass with some minor comments.
| "github.com/docker/docker/api/types/container" | ||
| "github.com/docker/docker/client" | ||
| "github.com/docker/docker/pkg/stdcopy" |
There was a problem hiding this comment.
github.com/docker/docker looks to redirect to github.com/moby/moby. The Docker Engine SDK documentation also refers to github.com/moby/moby/client. We should probably use this current package name unless we need to pin to a previous version for compatibility reasons.
There was a problem hiding this comment.
It's is done intentionally as the docker compose SDK is pinned to GitHub.com/docker/docker , so initially it's done through cli but in future I need to do it
|
|
||
| // NewDockerManager initializes a client compatible with v28+ via API negotiation. | ||
| func NewDockerManager() (*DockerManager, error) { | ||
| c, err := client.NewClientWithOpts(client.FromEnv, client.WithAPIVersionNegotiation()) |
There was a problem hiding this comment.
In the latest version of the Docker SDK, NewClientWithOpts looks to be deprecated in favour of New. For older versions it is not so will depend on which version is used.
There was a problem hiding this comment.
Yes, it's deprecated in github.com/moby/moby/client but not in github.com/docker/docker since that version was pinned for compatibility with the Docker Compose SDK.
| return dm.cli.ContainerStop(context.Background(), containerName, container.StopOptions{Timeout: &timeout}) | ||
| } | ||
|
|
||
| func (dm *DockerManager) ComposeCommand(action ComposeAction, projectDir, file, projectName string) (string, error) { |
There was a problem hiding this comment.
It might be cleaner to avoid the ComposeAction argument and have separate ComposeUp and ComposeDown methods. They can still use a private method for the shared implementation.
|
|
||
| fmt.Println("\033[1m", ">", binary, strings.Join(args, " "), "\033[0m") | ||
|
|
||
| cmd := exec.Command(binary, args...) //#nosec G204 |
There was a problem hiding this comment.
It might be nice if we could use something like the Compose SDK instead of exec CLI commands here too. That could happen in a later change though.
There was a problem hiding this comment.
Okay!! Thank you, sir, for the review.
I'll refactor this part to use the Compose SDK (instead of exec CLI) in the next commit
|
Hey, thanks again for the feedback! |
|
Also, the CI failures were due to not pushing the updated go.mod after adding the dependency. I’ll include the updated module files in the next commit. |
bestbeforetoday
left a comment
There was a problem hiding this comment.
Thank you for the updates.
One other issue that I think does need to be corrected is to rename scenario/go/docker_sdk.go to something like scenario/go/docker_test.go. This is only used by tests and should not be exposed as a public API so the filename should end in _test.go.
Signed-off-by: nXtCyberNet <rohantech2005@gmail.com>
Signed-off-by: Rohan Dev <86916212+nXtCyberNet@users.noreply.github.com>
Signed-off-by: nXtCyberNet <rohantech2005@gmail.com>
…ic-gateway into feat/docker_sdk
Signed-off-by: Rohan Dev <86916212+nXtCyberNet@users.noreply.github.com>
Thank you for the suggestion. I’ve renamed scenario/go/docker_sdk.go to scenario/go/docker_test.go as it is only used for tests. I’ve also updated the implementation to use the Docker Compose SDK instead of the CLI. |
Refactored Docker interactions to replace CLI-based container operations with the
official Docker Go SDK (github.com/docker/docker v28+). This improves reliability,
reduces dependency on shell execution, and provides better error handling.
fixes #1108
Key changes:
exec, start, and stop operations
ContainerExec, ContainerStart, ContainerStop, and ComposeCommand
does not provide native compose support
(v1/v2/v5) or docker compose plugin, preferring the standalone binary
replaced with length check)
environment variable flags (-e)
Tested:
cd scenario
go test -timeout 30m -tags pkcs11 -v -args ../features/