#How to copy directory from container into host

1 messages · Page 1 of 1 (latest)

grim sail
#

I porting the Elixir SDK into the Dagger repo and stuck with copying code to host after generate the api. Let's take a look at this example:

package main

import (
    "context"
    "os"

    "dagger.io/dagger"
    "github.com/wingyplus/must"
)

func main() {
    client := must.Must(dagger.Connect(context.Background(), dagger.WithLogOutput(os.Stderr)))
    defer client.Close()
    con := client.
        Container().
        From("ubuntu").
        WithExec([]string{"mkdir", "hello"}).
        WithExec([]string{"sh", "-c", "echo 'Hello' >> hello/a.txt"})

    client.Host().Directory(".").WithDirectory("hello", con.Directory("hello")).ID(context.Background())
}

I simulate it by create a new container that create directory and file inside that directory. And use client.Host().Directory(".").WithDirectory to copy from container to the host. I thought that could work because copy directory from container to container is working but container to host is not, and dagger engine return a result like it can copy. 😦

#

I found the workaround by using entries query to list the files inside directory like:

package main

import (
    "context"
    "io/ioutil"
    "os"

    "dagger.io/dagger"
    "github.com/wingyplus/must"
)

func main() {
    client := must.Must(dagger.Connect(context.Background(), dagger.WithLogOutput(os.Stderr)))
    defer client.Close()
    con := client.
        Container().
        From("ubuntu").
        WithExec([]string{"mkdir", "hello"}).
        WithExec([]string{"sh", "-c", "echo 'Hello' >> hello/a.txt"})

    os.MkdirAll("hello", 0o755)
    dir := con.Directory("hello")
    for _, file := range must.Must(dir.Entries(context.Background())) {
        contents := must.Must(dir.File(file).Contents(context.Background()))
        ioutil.WriteFile("hello/"+file, []byte(contents), 0o600)
    }
}

This workaround looks enough for Elixir SDK use case but if we have many child directory under that directory? This need to traverse file entries and writing it one by one?

pearl isle
#

I think you’re looking for Directory.Export. It’s the only way to write to the host filesystem

grim sail
#

@pearl isle Thank you. The Directory.Export is perfectly fit for this use case. 🙇

#

But is client.Host().Directory(".").WithDirectory("hello", con.Directory("hello")).ID(context.Background()) consider as a bug?

pearl isle
#

No, that creates a new directory based on an immutable snapshot of a host directory.