#Best way to inline a script/multiple commands with with_exec()

1 messages · Page 1 of 1 (latest)

sleek copper
#

Heya!
I have a container in which I need to run a script (which itself starts up a database) and then run some tests with cargo, some which use said database.

Because of some factors outside of my control I can't do something like instantiating that database as a service that gets passed into that container. I need to do it as described above.

I noticed that calling the two commands successively:

c.with_exec(['./entrypoint.sh'])
 .with_exec(['bash', '-c', 'cargo test'])
 .stdout()

Doesn't work as expected. The first command executes successfully, but the database is not up when running the tests. My guess is the database gets shut down between the calls.

Is there an easy way to ensure these two commands get executed consecutively without stopping the container in between?

cerulean shale
#

I am not sure this is possible because services must run as a service, remember that Dagger engine is closer to docker build than it is to docker run

Can you share more about why this is the case?

Because of some factors outside of my control I can't do something like instantiating that database as a service that gets passed into that container. I need to do it as described above.

urban sonnet
#

Hey @sleek copper! I've faced this same issue currently when doing some daggerizations with @red crescent. You can actually take a look at the recording here: https://www.youtube.com/watch?v=Lwj1_WvPhCk. TL;DR: we need to run containerd inside a container and within that same container run some commands. Meaning, you have a long lived process and some commands that need said process to function.

Our solution was rather "simple", do a with exec using a multi line string and start the database using the plain old & background operator. Something like this:

        WithExec([]string{"sh", "-c", `
docker-entrypoint.sh containerd &
go test -v ./e2e/exec/containerd_test.go
     `}, dagger.ContainerWithExecOpts{InsecureRootCapabilities: true})

Powered by Restream https://restream.io

Finding interesting OSS projects to Daggerize and learn along the way

▶ Play video
cerulean shale
#

@urban sonnet nevermind I think I get it - its not like the container itself hangs around, its just combining and making the overall step live long enough to do what it needs to do (I think! :D)

sleek copper
#

@cerulean shale I'm trying to migrate an existing pipeline without making too many changes. You're on point that I was looking for something along the lines of docker run.
@urban sonnet Thanks for that, this approach worked!
I'm wondering if Dagger could have some sugar like with_execs(execs: list[list[str]]) -> dagger.Container which would basically do what you did behind the scenes. I'd be more readable for cases like that, but I imagine it doesn't play too nicely with caching.

red crescent