@@ -14,7 +14,10 @@ import (
1414 "github.com/testcontainers/testcontainers-go/wait"
1515)
1616
17- var defaultDockerDaemonPort = "2375/tcp"
17+ const (
18+ defaultDockerDaemonPortNumber = "2375"
19+ defaultDockerDaemonPort = defaultDockerDaemonPortNumber + "/tcp"
20+ )
1821
1922// Container represents the Docker in Docker container type used in the module
2023type Container struct {
@@ -23,59 +26,49 @@ type Container struct {
2326
2427// Run creates an instance of the Docker in Docker container type
2528func Run (ctx context.Context , img string , opts ... testcontainers.ContainerCustomizer ) (* Container , error ) {
26- req := testcontainers.ContainerRequest {
27- Image : img ,
28- ExposedPorts : []string {
29- defaultDockerDaemonPort ,
30- },
31- HostConfigModifier : func (hc * container.HostConfig ) {
29+ moduleOpts := []testcontainers.ContainerCustomizer {
30+ testcontainers .WithCmd (
31+ "dockerd" , "-H" , "tcp://0.0.0.0:" + defaultDockerDaemonPortNumber , "--tls=false" ,
32+ ),
33+ testcontainers .WithEnv (map [string ]string {
34+ "DOCKER_HOST" : "tcp://localhost:" + defaultDockerDaemonPortNumber ,
35+ }),
36+ testcontainers .WithExposedPorts (defaultDockerDaemonPort ),
37+ testcontainers .WithWaitStrategy (wait .ForListeningPort (defaultDockerDaemonPort )),
38+ testcontainers .WithHostConfigModifier (func (hc * container.HostConfig ) {
3239 hc .Privileged = true
3340 hc .CgroupnsMode = "host"
3441 hc .Tmpfs = map [string ]string {
3542 "/run" : "" ,
3643 "/var/run" : "" ,
3744 }
3845 hc .Mounts = []mount.Mount {}
39- },
40- Cmd : []string {
41- "dockerd" , "-H" , "tcp://0.0.0.0:2375" , "--tls=false" ,
42- },
43- Env : map [string ]string {
44- "DOCKER_HOST" : "tcp://localhost:2375" ,
45- },
46- WaitingFor : wait .ForListeningPort ("2375/tcp" ),
47- }
48-
49- genericContainerReq := testcontainers.GenericContainerRequest {
50- ContainerRequest : req ,
51- Started : true ,
46+ }),
5247 }
5348
54- for _ , opt := range opts {
55- if err := opt .Customize (& genericContainerReq ); err != nil {
56- return nil , err
57- }
58- }
49+ moduleOpts = append (moduleOpts , opts ... )
5950
60- container , err := testcontainers .GenericContainer (ctx , genericContainerReq )
51+ ctr , err := testcontainers .Run (ctx , img , moduleOpts ... )
6152 var c * Container
62- if container != nil {
63- c = & Container {Container : container }
53+ if ctr != nil {
54+ c = & Container {Container : ctr }
6455 }
6556
6657 if err != nil {
67- return c , fmt .Errorf ("generic container : %w" , err )
58+ return c , fmt .Errorf ("run dind : %w" , err )
6859 }
6960
7061 return c , nil
7162}
7263
7364// Host returns the endpoint to connect to the Docker daemon running inside the DinD container.
7465func (c * Container ) Host (ctx context.Context ) (string , error ) {
75- return c .PortEndpoint (ctx , "2375/tcp" , "http" )
66+ return c .PortEndpoint (ctx , defaultDockerDaemonPort , "http" )
7667}
7768
7869// LoadImage loads an image into the DinD container.
70+ // It creates a temporary file to save the image and then copies it to the container.
71+ // This temporary file is deleted after the function returns.
7972func (c * Container ) LoadImage (ctx context.Context , image string ) (err error ) {
8073 var provider testcontainers.GenericProvider
8174 if provider , err = testcontainers .ProviderDocker .GetProvider (); err != nil {
0 commit comments