#How to use WithExec with WithDefaultArgs

1 messages · Page 1 of 1 (latest)

cursive ice
#

I want to build an image and push it to a repository. Then, I want to run it with as-service up to locally test the sites as a service. I can do that when I only have WithDefaultArgs, but when I add a WithExec above WithDefaultArgs, running it as a service fails at the health check
This code fail at healthcheck.

return dag.Container(dagger.ContainerOpts{Platform: "linux/amd64"}).
        From("nginx:alpine").
        WithoutEntrypoint().
        WithWorkdir(path).
        WithDirectory(path, dir.Directory(fmt.Sprintf("%s", site))).
        WithFile(fmt.Sprintf("/%s/replace-env-vars.sh", site), m.Source.File("scripts/replace-env-vars.sh")).
        WithFile("/etc/nginx/nginx.conf", m.Source.File(fmt.Sprintf("docker/%s/nginx.conf", site))).
        WithFile("/etc/nginx/mime.types", m.Source.File("docker/mime.types")).
        WithFile(fmt.Sprintf("/%s/_/js/vendor/mermaid.min.js", site), m.Source.File("assets/js/mermaid.min.js")).
        WithExec([]string{"apk", "add", "--no-cache", "bash"}).
        WithDefaultArgs([]string{"sh", "-c", fmt.Sprintf("/bin/bash /%s/replace-env-vars.sh /%s && nginx -g 'daemon off;'", site, site)}).
        WithExposedPort(80)
#

I can work around this by writing another function to return a service, while the above function is just to publish the image. I just want to know if anyone has a way to do this

paper kestrel
#

@cursive ice I think at the moment AsService ignores DefaultArgs and Entrypoint. Instead it uses the last WithExec as the service command. We discussed changing that as part of https://github.com/dagger/dagger/issues/7426

In the meantime, if you want to do:

ctr := mybuild()
ctr.Publish()
ctr.AsService()

Then you could perhaps add an extra WithExec:

ctr := mybuild()
ctr.Publish()
ctr.WithExec().AsService()

In theory this should pickup the default args and have the service use that.

Note I am on a phone and can't test this at the moment.

Sorry for the confusion.

GitHub

Problem The Service API is powerful, but confusing - especially to beginners. Service.start() starts a service, but you're not supposed to use it, except in a niche use case. Service.up() also ...

cursive ice
#

According to my code , I cannot run dagger call --source=. server --site=docs as-service up --ports 8080:80 to run the function that returns a Container as a service. I have to write another function with a WithExec that runs the same command as WithDefaultArgs and AsService and returns a Service to have a running local site.

From my code, I just build the code and return a Container, without publishing it to OCI yet.

paper kestrel
cursive ice
#
    ! checking for port 80/tcp: context canceled

Error: response from query: input: container.from.withoutEntrypoint.withWorkdir.withDirectory.withFile.withFile.withFile.withFile.withExec.withDefaultArgs.withExposedPort.asService.up resolve: failed to start host service: start upstream: service exited before healthcheck```

It return this error message
paper kestrel
crisp storm
# cursive ice According to my code , I cannot run `dagger call --source=. server --site=docs a...

According to my code , I cannot run dagger call --source=. server --site=docs as-service up --ports 8080:80 to run the function that returns a Container as a service.

this is accurate @cursive ice. It's one of the things that we're fixing in the issue Solomon linked above.

You can't currently publish and use the same container as a service via as-service up given the limitations you came up with. The current workaround is hanlding the service and publish functions separately and set the corresponding WithExec and WithDefaultArgs on each accordingly.

cursive ice