Skip to content

Mounts should support file paths  #2200

Closed

Description

@RonanQuigley

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

  1. aluzzardi commented on Apr 14, 2022

    @aluzzardi
    Contributor

    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 string mount to a llb.Mkfile on the fly and then mounting it).

    Unless I'm mistaken and it's actually possible to llb.Local a single file and mount it? /cc @sipsma

    Alternatively, to better match the buildkit AP, we could add a helper to high level packages (e.g. maybe a write or copy field to the docker.#Run action?)

    /cc @shykes @helderco @jpadams

  2. RonanQuigley commented on Apr 14, 2022

    @RonanQuigley
    Author

    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 FOOBAR can be passed to a field inside of a mount config to load a given file.

  3. sipsma commented on Apr 14, 2022

    @sipsma
    Contributor

    Unless I'm mistaken and it's actually possible to llb.Local a single file and mount it?

    Almost but not quite, llb.Local currently 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 at parent-dir/foofile you can do

    llb.Image("alpine:latest").Run(
        llb.AddMount("/some/other/path", llb.Local("parent-dir"), llb.SourcePath("foofile")),
        llb.Shlex("cat /some/other/path"),
    )
    

    To load in parent-dir and then place foofile at /some/other/path in the container.

    For the use case here, I'm wondering if we can already do this via the source field in core.#Mount? I can give it a try quickly.

  4. sipsma commented on Apr 14, 2022

    @sipsma
    Contributor

    @RonanQuigley the following works for me:

    import (
    	"dagger.io/dagger"
    	"dagger.io/dagger/core"
    	"universe.dagger.io/docker"
    	"universe.dagger.io/alpine"
    )
    
    dagger.#Plan & {
    	client: {
    		filesystem: {
    			".": read: {
    				contents: dagger.#FS
    			}
    		}
    	}
    	actions: {
    		image: alpine.#Build
    		build: docker.#Run & {
    			input: image.output
    			mounts: file: {
    				dest:     "/bar.txt"
    				contents: client.filesystem.".".read.contents
    				source:   "foo.txt"
    			}
    			always: true
    			command: {
    				name: "/bin/sh"
    				args: [
    					"-c",
    					"""
    						     cat /bar.txt
    						""",
    				]
    			}
    		}
    	}
    }
    

    Here I have my local directory . with a file in it foo.txt. I then mount it into the container at /bar.txt and print its contents (which match that of the local foo.txt). Is this what you were looking for?

  5. helderco commented on Apr 15, 2022

    @helderco
    Contributor

    What I've described with those snippets is essentially read the contents of a fixed file name and loading it into a container.

    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 client to 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 the client top-level field.

    But dagger is more like docker build than docker run. docker.#Run is like a Dockerfile's RUN instruction. Mounts don't come directly from your host, they come from the context, which is what dagger does when reading into a dagger.#FS.

  6. RonanQuigley commented on Apr 15, 2022

    @RonanQuigley
    Author

    @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.

    dagger.#Plan & {
    	client: {
            env: FOO: string
            env: BAR: string
    		filesystem: {
    			"\(client.env.FOO)": read: {
    				contents: dagger.#FS
    			}
    		}
    	}
    	actions: {
    		image: alpine.#Build
    		build: docker.#Run & {
    			input: image.output
    			mounts: file: {
    				dest:     "/bar.txt"
    				contents: client.filesystem."\(client.env.FOO)".read.contents
    				source:   "\(client.env.BAR)"
    			}
    			always: true
    			command: {
    				name: "/bin/sh"
    				args: [
    					"-c",
    					"""
    						cat /bar.txt
    						""",
    				]
    			}
    		}
    	}
    }
    

    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 client to do that. It's by design.

    @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 kubectl commands 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?

    But dagger is more like docker build than docker run. docker.#Run is like a Dockerfile's RUN instruction. Mounts don't come directly from your host, they come from the context, which is what dagger does when reading into a dagger.#FS.

    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.

  7. helderco commented on Apr 15, 2022

    @helderco
    Contributor

    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.

    We focus on reusable actions, not reusable plans, so consider this action from a third party that you import into your plan:

    #Build: {
        input: dagger.#FS
        _run: docker.#Run & {
            mounts: key: {
                source: "~/.ssh/id_rsa"
                ... 
        }
        ...
    }

    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 client top level field in the dagger.#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 #FS that any step can manipulate, as well as the image config. 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.

    I'd like to run kubectl commands inside of a docker container. [...] Based on what I've seen so far, dagger only supports fixed paths (please correct me if I'm mistaken!).

    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: command to go around it:

    	client: {
    		commands: kubeconfig: {
    			name: "cat"
    			args: [env.KUBECONFIG]
    			stdout: dagger.#Secret
    		}
    	}

    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?

    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?

    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.

    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).

  8. RonanQuigley commented on Apr 21, 2022

    @RonanQuigley
    Author

    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 client top level field in the dagger.#Plan

    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?

    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?

    Sure, let me expand on the kubectl use 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:

    • load in specific a specific directory for a helm chart
    • load in a specific dockerfile
    • load in a specific kubeconfig file

    However, in my experience, the current limitations with dagger make this quite tricky and the current design has room for improvement:

    
    import (
        "dagger.io/dagger"
        "universe.dagger.io/docker"
        "universe.dagger.io/bash"
        "dagger.io/dagger/core"    
    )
    
    _k8sConfigDestination: "/k8s/config" 
    
    dagger.#Plan & {
        client: {
            env: {
                KUBECONFIG: string
                KUBECONTEXT: string 
                KUBEENVIRONMENT: string 
                HELM_FOLDER: string | *"dagger-helm"
                TAG: string | *"latest" 
                DOCKERFILE: string | *"app-dockerfile"        
                HARBOR_USERNAME: string 
                HARBOR_PASSWORD: dagger.#Secret               
            }
    		commands: {
                kubeconfig: {
                    // Will work because we assign stdout to dagget.#Secret
                    // What I would like to see done differently here is to 
                    // support a string as an env variable as that is still an explicit way of passing in a path
                    name: "cat"
                    args: [env.KUBECONFIG]
                    stdout: dagger.#Secret
                }
            }
    		filesystem: {
    			".": read: contents: dagger.#FS   
                "./_build": write: contents: actions.run.output 
                // won't work due to field name interpolation
                "\(client.env.HELM_FOLDER)": read: contents: dagger.#FS
    		}
            network: "unix:///var/run/docker.sock": connect: dagger.#Socket
    	}
        // the aim is to make these actions reusable  
        actions: {
            builds: {
                dagger_base_build: docker.#Build & {
                    steps: [
                        docker.#Dockerfile & {
                            source: client.filesystem["."].read.contents
                            dockerfile: path: "./base-dockerfile"
                        },		        
                        docker.#Copy & {
                            // will work as dest accepts a string
                            dest:     client.env.HELM_FOLDER    
                            // won't work because it uses interpolation                
                            contents: client.filesystem."\(client.env.HELM_FOLDER)".read.contents
                        }		                    
                    ]
                }
                dagger_app_build: docker.#Build & {
                    steps: [
                        docker.#Dockerfile & {
                            // will work, but is static so you've tied a consumer of a reusable action
                            // to a very specific root directory 
                            source: client.filesystem["."].read.contents
                            dockerfile: path: client.env.DOCKERFILE
                        }        
                    ]
                }
            }
            push: {
                for name, app in builds {
                   "\(name)": docker.#Push & {
                        image: app.output 
                        auth: {
                            username: client.env.HARBOR_USERNAME
                            secret: client.env.HARBOR_PASSWORD
                        }
                        dest: "registry/project/\(name):\(client.env.TAG)"
                    }
                }
            }
            pull: docker.#Pull & {
                source: "registry/project/dagger_base_build:latest"
                auth: {
                    username: client.env.HARBOR_USERNAME
                    secret: client.env.HARBOR_PASSWORD
                }    
            }
            deploy_k8s:  bash.#Run & {
                always: true
                mounts: {
                    "k8s": {
                        dest: _k8sConfigDestination
                        // works fine due to the command workaround above
                        contents: client.commands.kubeconfig.stdout
                    }    
                    "docker": {
                        dest:     "/var/run/docker.sock"
                        contents: client.network."unix:///var/run/docker.sock".connect
                    }
                }
                input: pull.output
                script: {
                    directory: client.filesystem.".".read.contents
                    filename: "whatever"
                }  
                env: KUBECONFIG: _k8sConfigDestination
                args: []  
            }     
        }    
    }
    

    Does that help at all @helderco? #2196 does look like a similar issue to me.

  9. helderco commented on Apr 21, 2022

    @helderco
    Contributor

    I didn't understand your comment here:

                kubeconfig: {
                    // Will work because we assign stdout to dagget.#Secret
                    // What I would like to see done differently here is to 
                    // support a string as an env variable as that is still an explicit way of passing in a path
                    name: "cat"
                    args: [env.KUBECONFIG]
                    stdout: dagger.#Secret
                }

    Do you mean you wish you didn't have to resort to a command and just interpolate the var in the filesystem path?

  10. helderco commented on Apr 21, 2022

    @helderco
    Contributor

    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?

    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.#FS and pass that to the action to be mounted.

    The end goal would be to make all of these actions reusable. That way an end user of these actions could:

    • load in specific a specific directory for a helm chart
    • load in a specific dockerfile
    • load in a specific kubeconfig file

    However, in my experience, the current limitations with dagger make this quite tricky and the current design has room for improvement

    Most of what you want to do here seems related to not being able to use client: filesystem with 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: filesystem directly in your reusable actions. You need to create your inputs for internal use, and fill them with client when integrating in the plan. Just look at any action in universe.dagger.io.

    Here's a simple example: https://docs.dagger.io/1205/container-images#specifying-a-build-in-cue

  11. helderco commented on Apr 28, 2022

    @helderco
    Contributor

    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::

    mounts: {
        "k8s": {
            dest: "/file"
            // mount this file from the host machine
            // like in: docker run -v ./foobar.txt:/file <image> <command>
            source: "./foobar.txt
        }
    }

    It's different than #2290, but it may have been inspired by a misunderstanding here.

  12. RonanQuigley commented on Apr 28, 2022

    @RonanQuigley
    Author

    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 build

    	client: {
            env: {
                FOO: string
            }
    	}
    	actions: {
    		image: alpine.#Build
    		build: docker.#Run & {
    			input: image.output
    			mounts: file: {
    				dest:     "/bar.txt"            
    				path: client.env.FOO
    			}
    		}
    	}
    }
    

    That way you don't need to hop through an intermediary step, like in the examples I used in my earliers posts.

    It's different than #2290, but it may have been inspired by a misunderstanding here.

    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.env to act as the path, right?

    Also, the mount field of docker.#Run is of type core.#Mount

    mounts: [name=string]: core.#Mount
    which means that it should work with docker.#Run too.

  13. helderco commented on Apr 28, 2022

    @helderco
    Contributor

    Supporting interpolation via the fileystem is another way to achieve the same result,

    My example in #2200 (comment) just makes it clearer what we're talking about, it doesn't matter where that "./foobar.txt" comes from.

    It's different than #2290, but it may have been inspired by a misunderstanding here.

    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.env to act as the path, right?

    No, that issue is about mounting a file from it's content, not a path to your host machine.

  14. helderco commented on May 3, 2022

    @helderco
    Contributor

    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 client field.

    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):

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions