#Simple example of loading a local directory into a container using the GO SDK?

1 messages · Page 1 of 1 (latest)

dusty scarab
#

I'm shamed to say I've been working on this all afternoon and its just not clicking for me. I simply want to load a relative directory outside my dagger module, from my local file system into a container and list the file using the GO SDK. This is a simple step to just build some local GO web server files for quick testing. I can find plenty of dir examples using you own dagger client connections, etc, and then executing a 'go run' on the main.go file vs using the dagger cli to trigger the function directly. I'm trying to do it the quickstart method but without building GO files from a Git repo.

kind pasture
#

👋 for security reasons, Dagger doesn't allow you to specify files or directories outside your working directory or your git repo (if you're using any). The way to achieve this is to run dagger call within the context of your source files and use the -m flag to reference your module somewhere else in the system

#

you should be able to do something like

cd /my/project

dagger -m ../../my-dagger-module call foo -dir .
dusty scarab
#

@kind pasture Ah, that makes more sense. I was trying to pass just pass my app directory into a build container and just ls -la it to confirm they were there and they were never show up but these were always run from within the module directory. Bit late here now but will test tomorrow and report back. Thank you! 🍻

dusty scarab
#

Well it does trigger correctly, but I still have issues with Dagger accessing parts of my localfile system. I don't know why it can't find these directories.

Dagger function in my main.go

func (m *DaggerPipelines) GetLocalDir(ctx context.Context, source string) (*dagger.Directory, error) {

    // Check our path to confirm we're looking at the right directory
    fmt.Println("Target path:", source)

    // Connect to the Dagger Engine
    client, err := dagger.Connect(ctx, dagger.WithLogOutput(os.Stdout))
    if err != nil {
        return nil, fmt.Errorf("failed to connect to Dagger Engine: %s", err)
    }
    
    // Defer the client close
    defer client.Close()
    
    // Set the client, might not be necessary
    m.client = client
    
    // Attempt to access the directory through Dagger's host interface
    sourceDir := m.client.Host().Directory(source)
    if sourceDir == nil {
        return nil, fmt.Errorf("no directory found at '%s'", source)
    }

    fmt.Printf("Type of targetDir: %T\n", source)

    return sourceDir, nil
}
#

Error:

dagger -m ./dagger_pipelines call get-local-dir  --source=$(pwd)/app
✔ connect 0.7s
✔ initialize 0.3s
✘ DaggerPipelines.getLocalDir(source: "/Users/caseyphillips/Documents/Projects/bnt-web/app"): Directory! 0.2s
! call function "GetLocalDir": process "/runtime" did not complete successfully: exit code: 2
┃ Resolved absolute path: /Users/caseyphillips/Documents/Projects/bnt-web/app
┃ Type of targetDir: string
┃ marshal: json: error calling MarshalJSON for type *dagger.Directory: input: host.directory resolve:
┃ ost directory /Users/caseyphillips/Documents/Projects/bnt-web/app: rpc error: code = Unknown desc =
┃ esolve : lstat /Users: no such file or directory
┃
  ✘ Host.directory(path: "/Users/caseyphillips/Documents/Projects/bnt-web/app"): Directory! 0.0s
  ! host directory /Users/caseyphillips/Documents/Projects/bnt-web/app: rpc error: code = Unknown desc = resolve : lstat /Users: no such file or directory
    ✘ upload /Users/caseyphillips/Documents/Projects/bnt-web/app from buildkitsandbox (client id: ooxt3acst0fb1qmo4z4z4iy6i) 0.0s
    ! rpc error: code = Unknown desc = resolve : lstat /Users: no such file or directory

Error: response from query: input: daggerPipelines.getLocalDir resolve: call function "GetLocalDir": process "/runtime" did not complete successfully: exit code: 2

Stdout:
Resolved absolute path: /Users/caseyphillips/Documents/Projects/bnt-web/app
Type of targetDir: string
marshal: json: error calling MarshalJSON for type *dagger.Directory: input: host.directory resolve: host directory /Users/caseyphillips/Documents/Projects/bnt-web/app: rpc error: code = Unknown desc = resolve : lstat /Users: no such file or directory
#

Perhaps its Dagger's own permissions to my file system? There isn't a lot of info about this in the docs, and I can't find any simple examples of this from the more recent code bases. This seems like it would be documented on step 1 or in the first burb of the Quick start and shouldn't be this confusing. I know I'm probably dong something stupid but the docs def need some better real world examples. Most folks will probably start playing around with this like Make at first to get a understanding of what is going on. Happy to put in a PR on the docs around that if someone can walk me through the "correct" way of doing this sort thing.

kind pasture
# dusty scarab Perhaps its Dagger's own permissions to my file system? There isn't a lot of inf...

hey @dusty scarab! I'm sorry about the confusion, we're constantly improving our docs and we recently went through a big reorg after the "dagger functions" announcement. Please keep sharing your feedback, it's very valuable for us so we pave the path for future users.

Re: your current errors, seems like you're mixing the former dagger SDK model with the new functions approach. Particularly the dagger.Connect part and the client.Host() is from our previous (but still possible) way to use dagger without the function model (https://archive.docs.dagger.io/0.9/).

Since you're using functions, and we strongly recommend it, the tutorial to accomplish what you're looking for is this one https://docs.dagger.io/quickstart/428201/custom-function. Keep an eye on the build-src argument that your function receives than you can later use whatever you need in your pipeline.

#

I see that in your code you defined source as a string. If you change the type to dagger.Directory as shown in the quickstart I shared above, you can then use that directory in your pipeline as needed. All the dagger.Connect and m.client.Host() part is not required. It'd be great if you can share what actually confused you that made write your example like that cc @supple tartan

supple tartan