#Mount Docker Socket in a Dagger module

1 messages · Page 1 of 1 (latest)

severe cape
#

Hi, I am building a Dagger module for LocalStack. LocalStack allows users to create mock/emulated AWS resources locally. One of our requirements is to mount Docker socket into the LocalStack container which would allow users to create resources that require a Docker container to emulate, like Lambda function & EC2 instances. How can I do that in Dagger, since I saw some threads that it is not supported any longer. I tried using this command:

dagger call serve --auth-token=ls-XXXXXX up --ports 4566:4566 --ports 443:443 --docker-sock /var/run/docker.sock
✔ connect 0.2s
✔ load module 7.1s
✘ parsing command line arguments 0.0s
! unknown flag: --docker-sock

How can I fix that, or use any other workarounds?

crude schooner
#

Oh I see the issue. you're trying to pass --docker-sock to the up function. Trying moving the flag before up

severe cape
#

Here is the output:

dagger -c '.help serve'
✔ connect 0.3s
✔ load module 0.7s
✔ serving dependency modules 0.0s

Start a LocalStack service with appropriate configuration.

If auth_token is provided, starts LocalStack Pro edition.
Otherwise starts LocalStack Community edition.

Args:
    auth_token: Optional LocalStack auth token for Pro edition
    configuration: Optional string of configuration variables in format "KEY1=value1,KEY2=value2"
                 Example: "DEBUG=1,LS_LOG=trace"
    
Returns:
    A running LocalStack service container

USAGE
  serve [options]

OPTIONAL ARGUMENTS
  --auth-token string      (default: "")
  --configuration string   (default: "")

RETURNS
  Service - A content-addressed service providing TCP connectivity.
  
  Use "serve | .help" for more details.


Full trace at https://dagger.cloud/LocalStack/traces/3ef2ea8e37b0ccc252242a053ed8e3cf
#

This results in the same:

dagger call serve --auth-token=ls-XXXXX --configuration='DEBUG=1' --docker-sock /var/run/docker.sock up --ports 4566:4566 --ports 443:443
✔ connect 0.2s
✔ load module 1.2s
✘ parsing command line arguments 0.0s
! unknown flag: --docker-sock

Full trace at https://dagger.cloud/LocalStack/traces/d28399ed9a2687ad74948294d7f41f44
crude schooner
#

mm I don't see a docker-sock argument. Did you define one?

severe cape
#

Let me know if I'm doing something stupid 😅

#

Ah makes sense, I didn't realize I need to define one. Do you have an example that I can follow?

#

Or maybe I'll just take a stab at writing something that should work IMO

crude schooner
#

The docker socket has no special meaning for dagger. If you want to run a docker client in a function and connect to the docker engine in your host, you need to define an argument if type Socket . Then mount it in a container with docker client installed. then execute docker client from there

severe cape
#

Was not able to make it work. Saw your pass comment on this:

Pass the docker socket as an argument to your function, then mount it. There is a core Socket type, and a Container.withMountedSocket. The Dagger CLI accepts the path of a unix socket on your client machine.

And added this line

container = container.with_mounted_socket(docker_sock)

..which didn't work (no attribute named as such)

I still have a very blank mental picture of Dagger so far, so any help would be appreciated.

#

Here is my complete code for reference

@object_type
class LocalstackDaggerModule:
    @function
    def serve(
        self, 
        auth_token: Optional[str] = None,
        configuration: Optional[str] = None,
        docker_sock: str = "/var/run/docker.sock"
    ) -> dagger.Service:
        
        # Determine image based on auth token
        image = "localstack/localstack-pro:latest" if auth_token else "localstack/localstack:latest"
        
        # Start with base container config
        container = dag.container().from_(image)
        
        # Mount Docker socket
        container = container.with_mounted_socket(docker_sock)
        
        # Add auth token if provided
        if auth_token:
            container = container.with_env_variable("LOCALSTACK_AUTH_TOKEN", auth_token)
            
        # Add configuration variables if provided
        if configuration:
            for config_pair in configuration.split(','):
                if '=' in config_pair:
                    key, value = config_pair.strip().split('=', 1)
                    container = container.with_env_variable(key, value)
            
        # Add common ports (4566)
        container = (
            container
            .with_exposed_port(4566)
        )

            
        # Return as service
        return container.as_service()
severe cape
#

I was able to figure it out

#

Needed to read the message and docs better 😅

crude schooner
#

Nice 🙂 Sorry I dropped out for a minute there, was on a plane

severe cape
#

Thank You, more requests coming in 😅

nova coyote
#

Hey,
I want to build some images using go releaser.
I can't figure out what am I doing wrong here to attach docker socket to this:
Code:

  @func()
  release(gitdir: Directory, dockersocket: Socket): Directory {
    dag.container()
    return dag.goreleaser()
      .withGoCache()
      .withSource(gitdir)
      .ctr()
      .withWorkdir("/mnt/go")
      // .terminal() // debug
      .withUnixSocket("unix:///var/run/docker.sock", dockersocket)
      .withExec(["goreleaser", "release", "--snapshot"])
      .directory("/mnt/go/dist")
  }

I tried running this: dagger call release --gitdir . --dockersocket unix:///var/run/docker.sock
and dagger call release --gitdir . --dockersocket unix:///Users/utc/Library/Containers/com.docker.docker/Data/docker-cli.sock

But both are giving me:

  ⨯ release failed after 11s                                          
    error=                                                            
    │ docker build failed: failed to build jammutkarsh/unkey-dagger-health:0.2.0-SNAPSHOT-729d3b83: exit status 1: ERROR: Cannot connect to the Docker daem    
on at unix:///var/run/docker.sock. Is the docker daemon running?      
    │ Learn more at https://goreleaser.com/errors/docker-build        
! process "goreleaser release --snapshot" did not complete successfully: exit code: 1

I am on Intel Mac, using ts sdk

tough tundra
#

so it should be /var/run/docker.sock