@@ -26,9 +26,8 @@ import (
2626 "encoding/json"
2727 "errors"
2828 "fmt"
29- "github.com/distribution/reference"
30- "github.com/docker/docker/api/types/image"
3129 "io"
30+ "net/netip"
3231 "os"
3332 "os/exec"
3433 "os/signal"
@@ -38,10 +37,12 @@ import (
3837 "time"
3938
4039 "github.com/kubesmarts/logic-operator/cli/pkg/metadata"
41- "github.com/docker/docker/api/types/container"
42- "github.com/docker/docker/client"
43- "github.com/docker/docker/pkg/stdcopy"
44- "github.com/docker/go-connections/nat"
40+ "github.com/distribution/reference"
41+ "github.com/containerd/errdefs"
42+ "github.com/moby/moby/api/types/container"
43+ "github.com/moby/moby/api/types/network"
44+ "github.com/moby/moby/api/pkg/stdcopy"
45+ "github.com/moby/moby/client"
4546)
4647
4748const (
@@ -55,11 +56,11 @@ type DockerLogMessage struct {
5556}
5657
5758type DockerClient interface {
58- ImageList (ctx context.Context , options image. ListOptions ) ([]image. Summary , error )
59+ ImageList (ctx context.Context , options client. ImageListOptions ) (client. ImageListResult , error )
5960}
6061
6162func getDockerClient () (* client.Client , error ) {
62- cli , err := client .NewClientWithOpts (client .FromEnv , client . WithAPIVersionNegotiation () )
63+ cli , err := client .New (client .FromEnv )
6364 if err != nil {
6465 return nil , fmt .Errorf ("failed to create Docker client: %s" , err )
6566 }
@@ -102,12 +103,12 @@ func getDockerContainerID() (string, error) {
102103 return "" , err
103104 }
104105
105- containers , err := cli .ContainerList (context .Background (), container. ListOptions {})
106+ result , err := cli .ContainerList (context .Background (), client. ContainerListOptions {})
106107 if err != nil {
107108 return "" , err
108109 }
109110
110- for _ , c := range containers {
111+ for _ , c := range result . Items {
111112 // Check if the container has the expected image name or other identifying information
112113 if strings .Contains (c .Image , metadata .DevModeImage ) {
113114 return c .ID , nil
@@ -130,7 +131,8 @@ func StopContainer(containerTool string, containerID string) error {
130131 fmt .Printf ("unable to create client for docker" )
131132 return err
132133 }
133- if err := cli .ContainerStop (context .Background (), containerID , container.StopOptions {}); err != nil {
134+ _ , err = cli .ContainerStop (context .Background (), containerID , client.ContainerStopOptions {})
135+ if err != nil {
134136 fmt .Printf ("Unable to stop container %s: %s" , containerID , err )
135137 return err
136138 }
@@ -257,12 +259,12 @@ func CheckImageExists(cli DockerClient, ctx context.Context, imageName string) (
257259 } else {
258260 imageName = fmt .Sprintf ("%s:%s" , reference .Path (named ), "latest" )
259261 }
260- images , err := cli .ImageList (ctx , image. ListOptions {All : true })
262+ result , err := cli .ImageList (ctx , client. ImageListOptions {All : true })
261263 if err != nil {
262264 return false , fmt .Errorf ("error listing images: %s" , err )
263265 }
264266
265- for _ , i := range images {
267+ for _ , i := range result . Items {
266268 for _ , tag := range i .RepoTags {
267269 if strings .HasSuffix (tag , imageName ) {
268270 return true , nil
@@ -297,37 +299,43 @@ func waitToImageBeReady(reader io.ReadCloser) error {
297299 return nil
298300}
299301
300- func createDockerContainer (cli * client.Client , ctx context.Context , portMapping string , path string ) (container.CreateResponse , error ) {
301- containerConfig := & container.Config {
302- Image : metadata .DevModeImage ,
303- }
304- hostConfig := & container.HostConfig {
305- AutoRemove : true ,
306- PortBindings : nat.PortMap {
307- metadata .DockerInternalPort : []nat.PortBinding {
308- {
309- HostIP : "0.0.0.0" ,
310- HostPort : portMapping ,
302+ func createDockerContainer (cli * client.Client , ctx context.Context , portMapping string , path string ) (client.ContainerCreateResult , error ) {
303+ port := network .MustParsePort (string (metadata .DockerInternalPort ))
304+ hostIP := netip .MustParseAddr ("0.0.0.0" )
305+ createOpts := client.ContainerCreateOptions {
306+ Config : & container.Config {
307+ Image : metadata .DevModeImage ,
308+ ExposedPorts : network.PortSet {port : struct {}{}},
309+ },
310+ HostConfig : & container.HostConfig {
311+ AutoRemove : true ,
312+ PortBindings : network.PortMap {
313+ port : []network.PortBinding {
314+ {
315+ HostIP : hostIP ,
316+ HostPort : portMapping ,
317+ },
311318 },
312319 },
313- },
314- Binds : [] string {
315- fmt . Sprintf ( "%s:%s" , path , metadata . VolumeBindPath ) ,
320+ Binds : [] string {
321+ fmt . Sprintf ( "%s:%s" , path , metadata . VolumeBindPath ),
322+ } ,
316323 },
317324 }
318325
319- resp , err := cli .ContainerCreate (ctx , containerConfig , hostConfig , nil , nil , "" )
326+ resp , err := cli .ContainerCreate (ctx , createOpts )
320327 if err != nil {
321328 return resp , fmt .Errorf ("\n Unable to create container %s: %s" , metadata .DevModeImage , err )
322329 }
323330 return resp , nil
324331}
325332
326- func startDockerContainer (cli * client.Client , ctx context.Context , resp container. CreateResponse ) error {
333+ func startDockerContainer (cli * client.Client , ctx context.Context , resp client. ContainerCreateResult ) error {
327334 fmt .Printf ("\n Created container with ID %s" , resp .ID )
328335 fmt .Println ("\n ⏳ Starting your container and SonataFlow project..." )
329336
330- if err := cli .ContainerStart (ctx , resp .ID , container.StartOptions {}); err != nil {
337+ _ , err := cli .ContainerStart (ctx , resp .ID , client.ContainerStartOptions {})
338+ if err != nil {
331339 return fmt .Errorf ("\n Unable to start container %s" , resp .ID )
332340 }
333341
@@ -362,28 +370,30 @@ func runDockerContainer(portMapping string, path string) error {
362370
363371 return nil
364372}
365- func processOutputDuringContainerExecution (cli * client.Client , ctx context.Context , resp container.CreateResponse ) error {
366- statusCh , errCh := cli .ContainerWait (ctx , resp .ID , container .WaitConditionNotRunning )
373+ func processOutputDuringContainerExecution (cli * client.Client , ctx context.Context , resp client.ContainerCreateResult ) error {
374+ waitResult := cli .ContainerWait (ctx , resp .ID , client.ContainerWaitOptions {
375+ Condition : container .WaitConditionNotRunning ,
376+ })
367377
368378 //Print all container logs
369- out , err := cli .ContainerLogs (ctx , resp .ID , container. LogsOptions {ShowStdout : false , ShowStderr : true , Follow : true })
379+ logsResult , err := cli .ContainerLogs (ctx , resp .ID , client. ContainerLogsOptions {ShowStdout : false , ShowStderr : true , Follow : true })
370380 if err != nil {
371381 return fmt .Errorf ("\n Error getting container logs: %s" , err )
372382 }
373383
374384 go func () {
375- _ , err := stdcopy .StdCopy (os .Stdout , os .Stderr , out )
385+ _ , err := stdcopy .StdCopy (os .Stdout , os .Stderr , logsResult )
376386 if err != nil {
377387 fmt .Errorf ("\n Error copying container logs to stdout: %s" , err )
378388 }
379389 }()
380390
381391 select {
382- case err := <- errCh :
392+ case err := <- waitResult . Error :
383393 if err != nil {
384394 return fmt .Errorf ("\n Error starting the container %s: %s" , resp .ID , err )
385395 }
386- case <- statusCh :
396+ case <- waitResult . Result :
387397 //state of the container matches the condition, in our case WaitConditionNotRunning
388398 }
389399
@@ -412,14 +422,14 @@ func IsContainerRunning(containerID string) (bool, error) {
412422 if err != nil {
413423 return false , fmt .Errorf ("unable to create docker client: %w" , err )
414424 }
415- containerJSON , err := cli .ContainerInspect (context .Background (), containerID )
425+ result , err := cli .ContainerInspect (context .Background (), containerID , client. ContainerInspectOptions {} )
416426 if err != nil {
417- if client . IsErrNotFound (err ) {
427+ if errdefs . IsNotFound (err ) {
418428 return false , nil
419429 }
420430 return false , fmt .Errorf ("unable to inspect container %s with docker: %w" , containerID , err )
421431 }
422- return containerJSON .State .Running , nil
432+ return result . Container .State .Running , nil
423433
424434 } else if errPodman := CheckPodman (); errPodman == nil {
425435 cmd := exec .Command ("podman" , "inspect" , containerID , "--format" , "{{.State.Running}}" )
0 commit comments