#Docker to Dagger

1 messages · Page 1 of 1 (latest)

near rapids
#

Hello,
I am trying to replace my Dockerfile by creating a new container in a Dagger main.go file. I have looked through all of the documentation and have not found anything that helps with my specific issue.

Currently, I have a Dockerfile that has something like this:

services:
  crdb:
    image: crdbImage
    volumes:
      - "local/path:remote/path"

In my Dagger main.go file, I have something like:

ctx := context.Background()

    // initialize Dagger client
    client, err := dagger.Connect(ctx, dagger.WithLogOutput(os.Stderr))
    if err != nil {
        panic(err)
    }
    defer client.Close()

    src := client.Host().Directory(".")

    //crdb
    crdb := client.Container().
        From("crdbImage").
        WithDirectory("/src", src).
        WithMountedCache("/remote/path", client.CacheVolume("/remote/path")).
        WithExposedPort(12345).
        AsService()

    _, err = crdb.Start(ctx)
    if err != nil {
        panic(err)
    }

When I run this, I can an error that says it can't access the remote path it's supposed to.

My question is how do I transfer the volumes from Dockerfile into the main.go file so that it will work the same?

TIA

wooden robin
near rapids
#

I continue to get this error even with the WithMountedDirectory: ls: cannot access 'remote/path': No such file or directory

wooden robin
wooden robin
#

I'm signing off for today. Please leave a message if you can't figure it out and we can continue async tomorrow 🙏

near rapids
#

I can't share too much code unfortunately.

Are Docker volumes and Dagger's cache volumes essentially the same thing? I'm just trying to convert (transfer may have been the wrong word to use) the Docker volume into Dagger somehow.

wooden robin
#

Are Docker volumes and Dagger's cache volumes essentially the same thing? I'm just trying to convert (transfer may have been the wrong word to use) the Docker volume into Dagger somehow.

Dagger volumes are similar to Docker volumes in the sense they you can store persistent data across your pipeline execution. But they're different in the way that you can't bind-mount things from the host to the pipeline as you can do with docker run (https://docs.docker.com/storage/bind-mounts/).

Take into account that Dagger is more like docker build than docker run in that sense. As you probably know, docker build doesn't have the option to specify volumes with -va-la docker run since volumes work differently when building vs running a container

#

If you can maybe put a simple example about what you're trying to achieve, it'll be easier to unblock you

near rapids
#

I am trying to use Dagger to run integration tests rather than send it through Tilt, which needed a docker yaml file to build the containers for CRDB and other applications used to support the int test functionality. We just want to replace that yaml file with Dagger.

In the yaml file for CRDB, we have:

services:
  crdb:
    container_name: container
    image: crdbImage
    platform: platform
    ports:
      - "12345:12345"
      - "0000:0000"
    command: command1 --command2
    volumes:
      - "local/path:remote/path"

In the Dagger file's main.go, its the same as the one I shared above. I have replaced the mounted cache with what you sent, the WithMountedDirectory func

wooden robin
#

mind me asking what database is CRDB?

#

is that cockroach?

#

so I can put together a complete example to show you

near rapids
#

Yes its CockroachDB, sorry

wooden robin
#
package main

import (
    "context"
    "os"
    "time"

    "dagger.io/dagger"
)

func main() {
    ctx := context.background()

    // initialize dagger client
    client, err := dagger.connect(ctx, dagger.withlogoutput(os.stderr))
    if err != nil {
        panic(err)
    }
    defer client.close()

    src := client.host().directory(".")

    // crdb
    crdb := client.container().
        from("cockroachdb/cockroach").
        withexec([]string{"start-single-node"}).
        withmounteddirectory("/remote/path", src).
        withexposedport(26257).
        asservice()

    _, err = crdb.start(ctx)
    if err != nil {
        panic(err)
    }

    // sleep
    time.sleep(1 * time.minute)
}

this works @near rapids

#

that basically starts a cockroach service and they I added a time.Sleep at the end so the app waits since I don't have more to do there

#

then, in the cockroach contianer, all the contents of my local directory are located in /remote/path