Repository navigation
Closed
Labels
Description
What are you trying to do?
Dagger should be capable of loading a file into a docker container without having to mount a file system e.g. directory or work with a secret.
The equivalent in docker would be a docker run -v /path/file:/path/file image command.
@aluzzardi @jpadams FYI
Why is this important to you?
Currently reading in just a file is not trivial to do as the #Mount definition's contents field is not capable of working with a value that is a string e.g a file path
import (
"dagger.io/dagger"
"dagger.io/dagger/core"
"universe.dagger.io/docker"
)
dagger.#Plan & {
client: {
filesystem: {
".": read: {
contents: dagger.#FS
}
"./foobar.txt": read: {
contents: string
}
}
}
actions: {
build: docker.#Build & {
steps: [
docker.#Dockerfile & {
source: client.filesystem.".".read.contents
dockerfile: path: "./dockerfile"
},
docker.#Run & {
always: true
mounts: {
"k8s": {
dest: "/path/in/container/file"
contents: client.filesystem."./foobar.txt".read.contents // empty disjunction error
}
}
command: {
name: "/bin/bash"
args: [
"-c",
"""
# do something with the file in the container
""",
]
}
},
]
}
}
}
How are you currently working around this?
My current workaround relies on using field name interpolation with an env variable and looks something like this:
import (
"dagger.io/dagger"
"dagger.io/dagger/core"
"universe.dagger.io/docker"
)
dagger.#Plan & {
client: {
env: FOOBAR: string
commands: secret: {
name: "cat"
args: ["\(client.env.FOOBAR)"]
stdout: dagger.#Secret
}
filesystem: {
".": read: {
contents: dagger.#FS
}
}
}
actions: {
build: docker.#Build & {
steps: [
docker.#Dockerfile & {
source: client.filesystem.".".read.contents
dockerfile: path: "./dockerfile"
},
docker.#Run & {
always: true
mounts: {
"k8s": {
dest: "/path/in/container/file"
contents: client.commands.secret.stdout // this works as dagger.#FS is a valid within the #Mount definition
}
}
command: {
name: "/bin/bash"
args: [
"-c",
"""
# do whatever
""",
]
}
},
]
}
}
}
This is more verbose and coming from a docker background, feels counterproductive.
Another option is this, but it's still quite verbose:
build: docker.#Build & {
_config: core.#WriteFile & {
input: dagger.#Scratch
path: "test.txt"
contents: client.filesystem."./test.txt".read.contents
}
steps: [
alpine.#Build & {
packages: bash: _
},
docker.#Run & {
always: true
mounts: {
"test": {
dest: "/test"
contents: _config.output
}
}
command: {
name: "bash"
args: [
"-c",
"""
ls -l /test
cat /test/test.txt
""",
]
}
},
]
}
Activity
aluzzardi commentedon Apr 14, 2022
Thank you @RonanQuigley!
I don't think buildkit supports mounting a "file", but we could potentially fake it by automating the hack (e.g. converting to
stringmount to allb.Mkfileon the fly and then mounting it).Unless I'm mistaken and it's actually possible to
llb.Locala single file and mount it? /cc @sipsmaAlternatively, to better match the buildkit AP, we could add a helper to high level packages (e.g. maybe a
writeorcopyfield to thedocker.#Runaction?)/cc @shykes @helderco @jpadams
RonanQuigley commentedon Apr 14, 2022
Re-reading this, I should clarify something (sorry long day 😅)
What I've described with those snippets is essentially read the contents of a fixed file name and loading it into a container. Ideally what I'd like to see is the ability to do this:
FOOBAR=/path/to/file.<ext> dagger do <whatever>and
FOOBARcan be passed to a field inside of a mount config to load a given file.sipsma commentedon Apr 14, 2022
Almost but not quite,
llb.Localcurrently requires loading in a directory, but you can load in the parent directory of the file and then mount just that single file anywhere, e.g. if you have a file atparent-dir/foofileyou can doTo load in
parent-dirand then placefoofileat/some/other/pathin the container.For the use case here, I'm wondering if we can already do this via the
sourcefield incore.#Mount? I can give it a try quickly.sipsma commentedon Apr 14, 2022
@RonanQuigley the following works for me:
Here I have my local directory
.with a file in itfoo.txt. I then mount it into the container at/bar.txtand print its contents (which match that of the localfoo.txt). Is this what you were looking for?helderco commentedon Apr 15, 2022
We decided against this for security reasons, in the sense that we don't want users to load files from their computer automatically from actions they're reusing from others, without being aware. You have to go through your plan's
clientto do that. It's by design.In your analogy, an action is like a container image. The plan is what's doing
docker run(declaratively), so the arguments that connect to your local machine (like-v <host>:<container>), are done through theclienttop-level field.But dagger is more like
docker buildthandocker run.docker.#Runis like a Dockerfile'sRUNinstruction. Mounts don't come directly from your host, they come from the context, which is what dagger does when reading into adagger.#FS.RonanQuigley commentedon Apr 15, 2022
@sipsma That's certainly cleaner, but the configuration of the file path and the directory is still fixed. For example, if I tried to do this, dagger does not allow it.
@helderco Sorry, I'm not following 😅. Could you explain a bit more what you mean here? The reason I ask is that I don't understand how security is a factor when I'm already working around it to achieve the same result.
Perhaps a use case might help here:
I'd like to run
kubectlcommands inside of a docker container. To do so, it requires the configuration of the relevant cluster(s) to target. This config is found in a config file. This config file is not something that lives alongside the dagger plan code i.e. it can be anywhere on the user's host machine depending on where they've decided to put it. Based on what I've seen so far, dagger only supports fixed paths (please correct me if I'm mistaken!). As you can see in the example above, field name interpolation also does not work.Sometimes you do need to add files that just cannot be added into a container at build time. Based off of what you've said, it suggests to me that this is not what dagger is designed for, or is that unfair to say? If so, how would you solve this problem?
Is this documented anywhere? If not, I think it is worth outlining to new users about how they can transfer docker concepts to dagger plans. My mental model for dagger is based on my prior experiences with docker, so it looks like I'm making the wrong assumptions here.
helderco commentedon Apr 15, 2022
We focus on reusable actions, not reusable plans, so consider this action from a third party that you import into your plan:
Even though you can send the private key from your plan (see cli.#Run), at least you're doing it explicitly. We made the decision that anything that communicates with the host machine, must come from the
clienttop level field in thedagger.#Plan.So a reusable action is like a reusable Dockerfile, but it's much more powerful because of how you can compose and combine the building blocks. You have a lot of control with dagger, e.g., having access to the
#FSthat any step can manipulate, as well as the imageconfig. In the end, a Dockerfile creates a docker image, in sequencial steps. Even though you can do that with dagger, there's so much more you can do.You're partly right in that the current API doesn't allow dynamic paths coming from the output of an action, but that's not intended by design. Currently, they have to be known before running the actions. See Dynamic path in following issue:
There's other users with your exact use case. You can see how this one used
client: commandto go around it:Doesn't seem like something that can't be done, but I need clarity on this one. Can you explain what you mean? An example perhaps?
Yes, it could be useful. Especially if we can explain how dagger is so much more then what can be done with a single
docker build. They're not equal, they just leverage the same underlying engine and building blocks (i.e., Buildkit).RonanQuigley commentedon Apr 21, 2022
So the contents of a mount won't support files because you're looking for any interaction with the filesystem to come from the client?
Sure, let me expand on the
kubectluse case, this time bringing in helm and a dockerfile.The end goal would be to make all of these actions reusable. That way an end user of these actions could:
However, in my experience, the current limitations with dagger make this quite tricky and the current design has room for improvement:
Does that help at all @helderco? #2196 does look like a similar issue to me.
helderco commentedon Apr 21, 2022
I didn't understand your comment here:
Do you mean you wish you didn't have to resort to a command and just interpolate the var in the
filesystempath?helderco commentedon Apr 21, 2022
Yes, but to be clear, you mean passing a file path directly to an action to be mounted. You can still load that file in a
dagger.#FSand pass that to the action to be mounted.Most of what you want to do here seems related to not being able to use
client: filesystemwith dynamic paths. The issue isn't interpolation, it's that the path isn't known when the plan is loaded (it's built during run). I agree there's room for improvement, this in particular is being tracked in:The fact that dynamic paths aren't allowed is not by design, it's just an upcoming feature for dynamic actions that's not ready yet, but the above issue attempts to solve this use case in the meantime.
As for reusability, you shouldn't reference
client: filesystemdirectly in your reusable actions. You need to create your inputs for internal use, and fill them withclientwhen integrating in the plan. Just look at any action inuniverse.dagger.io.Here's a simple example: https://docs.dagger.io/1205/container-images#specifying-a-build-in-cue
helderco commentedon Apr 28, 2022
Do you have any further questions @RonanQuigley?
This should be closed with a resolution/wontfix, just asking for one last feedback from @shykes.
To sum up, what OP is requesting is that you be able to set a mount from a file path, whithout having to go through
client: filesystem::It's different than #2290, but it may have been inspired by a misunderstanding here.
RonanQuigley commentedon Apr 28, 2022
Not quite. I think there's a bit of talking past each other going on here.
Supporting interpolation via the fileystem is another way to achieve the same result, but it would be nice to do this sort of thing:
[I've not been able to look at dagger this past week, so let me know if this is no longer correct]
FOO=/path/to/file.<ext> dagger do buildThat way you don't need to hop through an intermediary step, like in the examples I used in my earliers posts.
On the contrary, after reading that pull request through, I think that is what I'm looking for as you could then read a string from
client.envto act as the path, right?Also, the
mountfield of docker.#Run is of typecore.#Mountdagger/pkg/universe.dagger.io/docker/run.cue
Line 18 in 8398f82
helderco commentedon Apr 28, 2022
My example in #2200 (comment) just makes it clearer what we're talking about, it doesn't matter where that "./foobar.txt" comes from.
No, that issue is about mounting a file from it's content, not a path to your host machine.
helderco commentedon May 3, 2022
Thank you for your suggestion, but as I've explained, we decided against letting actions access the host machine directly. Any interaction with the host machine must come through the plan's
clientfield.About your dynamic path difficulties, it's being tracked here:
If you're interested in mounting a file's contents directly (rather than via path):
#Mountsupports inline file #2290