#dagger cli and Mounting host directory as volumes inside container

1 messages · Page 1 of 1 (latest)

violet talon
#

I'm trying to use a host folder inside the container but I couldn't. The documentations available are mostly for SDK. I would like to do this on my bash script. How to accomplish this?

#!/bin/bash
tag=`jq .tag input.json`
hostdir="/tmp"

alpine=$(dagger query <<EOF | jq -r .container.from.withDirectory.withWorkdir.withExec.stdout
{
  container {
    from(address:"alpine:latest") {
      withDirectory(path:"/src", directory:"$hostdir") {
      withWorkdir(path:".") {
      withExec(args:["sh", "-c",
        """
        echo tag version is $tag
        ls -ltr /src
        uname -a
        """
        ]) {
        stdout
      }
    }
    }}
  }
}
EOF
)

echo $alpine

frank gorge
#

👋 this would be the way to achieve that

#!/bin/bash
tag=`jq .tag input.json`


hostdir=$(dagger query <<EOF | jq -r .host.directory.id
{
    host {
        directory(path:"/tmp") {
            id
        }
    }

}
EOF
)

echo $hostdir


alpine=$(dagger query <<EOF | jq -r .container.from.withDirectory.withWorkdir.withExec.stdout
{
  container {
    from(address:"alpine:latest") {
      withDirectory(path:"/src", directory:"$hostdir") {
      withWorkdir(path:".") {
      withExec(args:["sh", "-c",
        """
        echo tag version is $tag
        ls -ltr /src
        uname -a
        """
        ]) {
        stdout
      }
    }
    }}
  }
}
EOF
)

echo $alpine
#

since withDirectory expects a Directory, you need to pass an id obtained from a host.directory call

violet talon
#

Thanks! That worked. How do I suppress the output being displayed twice? Tried dagger query with -s which didn't help.

violet talon
frank gorge
#

The output is being displayed twice because it gets printed to the tty. The $alpine variable will only hold the value once

violet talon
#

OK. I built my project using the shared host folder(s). Now I want to write back the artifacts folder to host. Tried below, and it throws error. Not able to find the appropriate function input or syntax.

alpine=$(dagger query --focus <<EOF | jq -r .container.from.withDirectory.withDirectory.withDirectory.withWorkdir.withExec.stdout
{
  container {
    from(address:"debian:bullseye-slim") {
      withDirectory(path:"/nodesrc", directory:"$hostdir") {
        withDirectory(path:"/kernelsrc", directory:"$kerndir") {
            withDirectory(path:"/src", directory:"$srcdir") {
            withWorkdir(path:"/src") {
                withExec(args:["bash","rombuild.sh"]) {
                {
                    export(path:"/src")
                }
                stdout
                }
             }
    }}}}
  }
}
EOF
)

Error:

┃ 1: dagger query --focus ERROR: make request: returned error 422 Unprocessable Entity: {"errors":[{"message":"Expected Name, found {","locations":[{"line":9,"colu
┃ mn":17}],"extensions":{"code":"GRAPHQL_PARSE_FAILED"}}],"data":null}     
frank gorge
#

👋 here's an example how to achieve that:

alpine=$(dagger query <<EOF | jq -r .container.from.withExec.stdout
{
  container {
    from(address:"alpine:latest") {
      withExec(args:["sh", "-c",
        """
        echo hello
        """
        ]) {
        stdout
        directory(path:"/etc") {
            export(path:"etc")
        }
      }
    }
  }
}
EOF
)

echo $alpine
violet talon
frank gorge