Skip to content

Copying a file (or directory) inside a container is confusing #3673

Closed

Description

@samalba

Reporting this as I am sure several users will fall into this.

TL;DR: It took me a while to understand the API, in particular @aluzzardi had to explain to me that a container's fs can be either initialized from a From() or WithFS(), but both are mutually exclusive!

In my example, I just wanted to copy a file to a base image. Here is what I went through the api step by step:

    1. I want to add a file, ok let's just call with_file() on a container → the method does not exist
    1. Ok, I understand that with_file() can only be called on an FS, let's create a new empty FS with the file I want, and I'll add it to my base image:
    # Create an FS layer with only the binary
    directory = await (
        client.container().fs().with_file("/usr/local/bin/app", app_binary.value).id()
    )

    # Publish a new container based on alpine, containing the app binary
    image = await (
        client.container()
        .from_("alpine:3.16")
        .with_fs(directory.value)
        .with_entrypoint("/usr/local/bin/app")
        .publish("samalba/greetings-api")
    )
    1. As a result, my published image just has an empty rootfs, not an alpine anymore. It took me a while to understand that with_fs() replaced from_() (not an append)

At the end, the code works (see the link above), but it's not intuitive. We should either support with_file() on a container object or allow adding an FS to a container (via a merge?).

Activity

  1. shykes commented on Nov 4, 2022

    @shykes
    Contributor

    Yeah that was also reported by someone else (David Jackson I think?)

    Two solutions I see:

    1. add container.with_file() as a convenience like you said
    2. with the current API:
    base = await (
      client.container()
      .from(“alpine:3.16”)
      .id()
    )
    
    fs = await (
      base.fs()
      .with_file(“/usr/local/bin/app", app_binary.value)
      .id()
    )
    
    image = await (
      base
      .with_fs(fs)
      .with_entrypoint("/usr/local/bin/app")
      .publish("samalba/greetings-api")
    )
  2. aluzzardi commented on Nov 4, 2022

    @aluzzardi
    Contributor

    Just stumbled upon another thread that's been hit by this confusion (how to do the equivalent of a multi stage build).

    I noticed @kpenfound using .WithMountedFile().Exec("cp", ...) -- figuring out the .FS() .WithFS() is really not straightforward. I started typing a message suggesting to use WithFile instead then I realized we'd hit this issue.

    There's no easy way around this.

    add container.with_file() as a convenience like you said

    WithFile is just one, but this applies to any function that alters the filesystem:

    • WithFile
    • WithNewFile
    • WithoutFile
    • WithDirectory
    • WithNewDirectory
    • WithoutDirectory

    with the current API:

    This is what @samalba ended up doing but it's not intuitive -- I had to guide him through that. Same as @kpenfound (linked snippet) or the user on the Discord thread above.

    Also -- base.with_fs() ends up being a container().from("alpine:3.16").with_fs(); the with_fs bit wipes out from.

    Relevant discussion from API design thread: #3151 (comment)

    In summary from and with_fs can ONLY be used on a direct client.container() -- using them anywhere else (e.g. base.with_fs(), base.from(), ...) doesn't work

  3. kpenfound commented on Nov 5, 2022

    @kpenfound
    Contributor

    Just to make sure I'm following, it's not currently possible to have both a from() and a with_fs(), so to use a specific base you're stuck with the exec('cp'... right? Or am I still missing the solution?

  4. shykes commented on Nov 5, 2022

    @shykes
    Contributor

    Just stumbled upon another thread that's been hit by this confusion (how to do the equivalent of a multi stage build).

    I noticed @kpenfound using .WithMountedFile().Exec("cp", ...) -- figuring out the .FS() .WithFS() is really not straightforward. I started typing a message suggesting to use WithFile instead then I realized we'd hit this issue.

    I think it’s too early to tell. Just because someone doesn’t know a feature doesn’t exist in an API, doesn’t mean that API isn’t straightforward. The whole programming model is unusual.

    add container.with_file() as a convenience like you said

    WithFile is just one, but this applies to any function that alters the filesystem:

    Right. Not great.

    with the current API:

    This is what @samalba ended up doing but it's not intuitive -- I had to guide him through that.

    Ok, more detail here would be helpful. What about it was not intuitive?

    Same as @kpenfound (linked snippet) or the user on the Discord thread above.

    I also don’t know how exactly Kyle felt it was unintuitive. All I know so far is he didn’t know there was another way. But would love to get more details @kpenfound .

    Also -- base.with_fs() ends up being a container().from("alpine:3.16").with_fs(); the with_fs bit wipes out from.

    Not sure if that’s a bug or intentional. But given @samalba ’s example, seems like we should fix it asap. No reason to wipe IMO.

    Relevant discussion from API design thread: #3151 (comment)

    I don’t see anything about the wiping implementation of with_fs, seems to be all about the API design itself, bikeshedding various options etc.

    In summary from and with_fs can ONLY be used on a direct client.container() -- using them anywhere else (e.g. base.with_fs(), base.from(), ...) doesn't work

    Definitely a bug to be fixed IMO.

  5. shykes commented on Nov 5, 2022

    @shykes
  6. shykes commented on Nov 5, 2022

    @shykes
    Contributor

    Relevant: #3264 , filed by @vito .

  7. kpenfound commented on Nov 5, 2022

    @kpenfound
    Contributor

    I also don’t know how exactly Kyle felt it was unintuitive. All I know so far is he didn’t know there was another way. But would love to get more details @kpenfound .

    For me, if my previous question was correct, it's not that the FS() is unintuitive but rather I want my build artifact to end up on a specific base, so the .From().WithFS() would have to work. Similar to David's feedback, what I'm really looking for is the equivalent of the dockerfile COPY --from

  8. shykes commented on Nov 5, 2022

    @shykes
  9. aluzzardi commented on Nov 5, 2022

    @aluzzardi
  10. vito commented on Nov 5, 2022

    @vito
    Contributor

    Also confused; as far as I can tell, WithFS does precisely what #3707 asks already: it only replaces the root filesystem. Mounts and image config remain.

    In summary from and with_fs can ONLY be used on a direct client.container() -- using them anywhere else (e.g. base.with_fs(), base.from(), ...) doesn't work

    Definitely a bug to be fixed IMO.

    Confused by this too; you can use From to get an initial rootfs + config and then WithFS to replace its rootfs with an extended one while keeping the image config, which is the only way to do something like COPY --from at the moment. As far as I can tell this is working as intended, and this thread has already pointed to it as the current solution to this problem:

    // dir containing /usr/local/bin/app as in original example
    var dir *dagger.Directory
    
    ctr := client.Container().From("alpine:3.16.2")
    
    // COPY --from=dir /usr/local/bin/app /usr/local/bin/app
    ctr = ctr.WithFS(ctr.FS().WithFile("/usr/local/bin/app", dir.File("/usr/local/bin/app")))

    So then #3264 is just requesting some sugar in the API to make that feel less clunky, but it's a little unfortunate that taking it to its logical conclusion effectively means squashing all the Directory.With* methods directly into Container. Maybe that's OK? It's not a huge stretch to say a Container is a superset of a Directory.

    // shorthand for ctr.WithFS(ctr.FS().[...]), implemented by the API so all SDKs don't have to
    ctr = ctr.WithFile("/usr/local/bin/app", dir.File("/usr/local/bin/app"))

    Judging by what OP tried, one contributing factor may be that WithFS sounds additive, like "overlay this filesystem onto it" when really it replaces it wholesale. I wonder if it would be clearer if we went back to the original "Rootfs" name? I'd wager whatever capitalization scheme we land on (team Rootfs here) would be less confusing than removing a key component of the word. This might be purely subjective though; to me WithRootfs sounds like a replacement, but maybe to others it's equally unclear.

    Side note: another option might be to add Merge, since that seems closer to what the OP was trying to do:

    // dir containing /usr/local/bin/app as in original example
    var dir *dagger.Directory
    
    ctr := client.Container().From("alpine:3.16.2")
    ctr.WithFS(ctr.FS().Merge(dir))
    
    // or, assuming #3264 
    ctr.Merge(dir)
  11. aluzzardi commented on Nov 5, 2022

    @aluzzardi
    Contributor

    Also confused; as far as I can tell, WithFS does precisely what #3707 asks already: it only replaces the root filesystem. Mounts and image config remain.

    Yep -- everything is working fine. My bad, I created more confusion than necessary. I didn't mean it's not working, I meant it's not doing what the user thinks it's doing.

    Let me rephrase:

    1. From().WithFS() is working as intended. No bug there, and no-one has complained about this
    2. To achieve COPY --from, the solution is indeed ctr = ctr.WithFS(ctr.FS().WithFile(...)) (or a variation thereof)
    3. Neither Sam, Kyle nor the Discord user reached this solution on their own (main problem)
    4. As @vito mentioned, one additional issue that led to even more confusion isWithFS sounds additive (orthogonal problem)

    Agree with @vito's assessment. Decision tree is:

    main problem (COPY --from)

    orthogonal problem (WithFS sounds additive)

    • OP's confusion stemmed from
      • WithFile --> add a file
      • WithDirectory --> add a directory
      • WithFS --> replace the filesystem, but sounds like a synonym of WithDirectory
    • Snippet of DM chat from @samalba when he was debugging, hope you don't mind :)
      • My container became an empty layer. It's as if with_fs replaced the initial root filesystem

      • Isn't with_fs supposed to make a copy?

    • Should we use a different name (e.g. RootFS / WithRootFS) that sounds less like a synonym of Directory? (team rootfs here as well)
      • @samalba Would you have been less confused by what Container().From("alpine").WithRootFS(dir) would do?
    • Should we make WithFS additive (@vito's side note)?
      • I don't think we should -- sometimes we do want to replace and we'd lose the ability
      • Weird API -- WithDirectory("/", ...) and WithFS become essentially the same
  12. kpenfound commented on Nov 6, 2022

    @kpenfound
    Contributor

    Thanks for all of this discussion, I definitely overlooked the ctr.WithFS(ctr.FS().WithFile(...)) possibility even after reading through everything before Alex's comment. That makes sense as a better solution than Exec(cp.., but I agree it would feel more natural if there was at least an option to With* a Directory or File in a way that is additive to the rootfs

  13. shykes commented on Nov 6, 2022

    @shykes
    Contributor

    Thanks for clarifying @aluzzardi ! I agree with the decision tree.

  14. shykes commented on Nov 6, 2022

    @shykes
    Contributor

    Decision tree is:

    main problem (COPY --from)

    I have a default reluctance to adding too many different ways to do the same thing, but this might be the exception that confirms the rule. Two arguments in favor:

    • Dockerfile feature parity makes sense. Replacing / imitating a Dockerfile will be a “happy path” for may beginners I believe.
    • Container already has directory. It’s not exactly the same since container.directory includes mounted contents, whereas container.fs.directory does not. But still “sets the stage” for more parity.

    orthogonal problem (WithFS sounds additive)

    • OP's confusion stemmed from

      • WithFile --> add a file
      • WithDirectory --> add a directory
      • WithFS --> replace the filesystem, but sounds like a synonym of WithDirectory
    • Snippet of DM chat from @samalba when he was debugging, hope you don't mind :)

      • My container became an empty layer. It's as if with_fs replaced the initial root filesystem

      • Isn't with_fs supposed to make a copy?

    • Should we use a different name (e.g. RootFS / WithRootFS) that sounds less like a synonym of Directory? (team rootfs here as well)

      • @samalba Would you have been less confused by what Container().From("alpine").WithRootFS(dir) would do?

    I agree with @vito that WithRootFS is better. I think we should do this regardless of other improvements.

    • Should we make WithFS additive (@vito's side note)?

      • I don't think we should -- sometimes we do want to replace and we'd lose the ability
      • Weird API -- WithDirectory("/", ...) and WithFS become essentially the same

    I agree that we should not do this.

  15. shykes commented on Nov 10, 2022

    @shykes
    Contributor

    One more datapoint: @davidwallacejackson did not find it intuitive to use the "withFS.()" method.

    So that's a +1 for adding methods to Container, dockerfile-style.

  16. aluzzardi commented on Nov 15, 2022

    @aluzzardi
    Contributor

    Implemented in #3827 - -WithDirectory, WithFile, WithNewFile

  17. aluzzardi commented on Nov 17, 2022

    @aluzzardi
    Contributor

    I agree with @vito that WithRootFS is better. I think we should do this regardless of other improvements.

    Implemented in #3882

  18. samalba commented on Dec 22, 2022

    @samalba
    ContributorAuthor

    I am closing this given #3882 and #3827, we can re-open this issue in the future if needed.

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions