You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Copying a file (or directory) inside a container is confusing #3673
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!
I want to add a file, ok let's just call with_file() on a container → the method does not exist
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 binarydirectory=await (
client.container().fs().with_file("/usr/local/bin/app", app_binary.value).id()
)
# Publish a new container based on alpine, containing the app binaryimage=await (
client.container()
.from_("alpine:3.16")
.with_fs(directory.value)
.with_entrypoint("/usr/local/bin/app")
.publish("samalba/greetings-api")
)
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?).
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.
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
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?
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.
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
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
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 examplevardir*dagger.Directoryctr:=client.Container().From("alpine:3.16.2")
// COPY --from=dir /usr/local/bin/app /usr/local/bin/appctr=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 toctr=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 examplevardir*dagger.Directoryctr:=client.Container().From("alpine:3.16.2")
ctr.WithFS(ctr.FS().Merge(dir))
// or, assuming #3264 ctr.Merge(dir)
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:
From().WithFS() is working as intended. No bug there, and no-one has complained about this
To achieve COPY --from, the solution is indeed ctr = ctr.WithFS(ctr.FS().WithFile(...)) (or a variation thereof)
Neither Sam, Kyle nor the Discord user reached this solution on their own (main problem)
As @vito mentioned, one additional issue that led to even more confusion isWithFS sounds additive (orthogonal problem)
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
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.
Reacted by Alex Suraci, Kyle Penfound and Joel Longtine
Activity
shykes commentedon Nov 4, 2022
Yeah that was also reported by someone else (David Jackson I think?)
Two solutions I see:
container.with_file()as a convenience like you saidaluzzardi commentedon Nov 4, 2022
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 useWithFileinstead then I realized we'd hit this issue.There's no easy way around this.
WithFileis just one, but this applies to any function that alters the filesystem:WithFileWithNewFileWithoutFileWithDirectoryWithNewDirectoryWithoutDirectoryThis 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 acontainer().from("alpine:3.16").with_fs(); thewith_fsbit wipes outfrom.Relevant discussion from API design thread: #3151 (comment)
In summary
fromandwith_fscan ONLY be used on a directclient.container()-- using them anywhere else (e.g.base.with_fs(),base.from(), ...) doesn't workkpenfound commentedon Nov 5, 2022
Just to make sure I'm following, it's not currently possible to have both a
from()and awith_fs(), so to use a specific base you're stuck with theexec('cp'...right? Or am I still missing the solution?shykes commentedon Nov 5, 2022
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.
Right. Not great.
Ok, more detail here would be helpful. What about it was not intuitive?
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 .
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.
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.Definitely a bug to be fixed IMO.
shykes commentedon Nov 5, 2022
shykes commentedon Nov 5, 2022
Relevant: #3264 , filed by @vito .
kpenfound commentedon Nov 5, 2022
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 dockerfileCOPY --fromshykes commentedon Nov 5, 2022
aluzzardi commentedon Nov 5, 2022
vito commentedon Nov 5, 2022
Also confused; as far as I can tell,
WithFSdoes precisely what #3707 asks already: it only replaces the root filesystem. Mounts and image config remain.Confused by this too; you can use
Fromto get an initial rootfs + config and thenWithFSto replace its rootfs with an extended one while keeping the image config, which is the only way to do something likeCOPY --fromat 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: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 intoContainer. Maybe that's OK? It's not a huge stretch to say a Container is a superset of a Directory.Judging by what OP tried, one contributing factor may be that
WithFSsounds 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 meWithRootfssounds 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:
aluzzardi commentedon Nov 5, 2022
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:
From().WithFS()is working as intended. No bug there, and no-one has complained about thisCOPY --from, the solution is indeedctr = ctr.WithFS(ctr.FS().WithFile(...))(or a variation thereof)WithFSsounds additive (orthogonal problem)Agree with @vito's assessment. Decision tree is:
main problem (
COPY --from)ctr.WithFS(ctr.FS().WithFile(...))-->ctr.WithFile(...)? Container: addwithDirectoryandwithFile#3264orthogonal problem (
WithFSsounds additive)WithFile--> add a fileWithDirectory--> add a directoryWithFS--> replace the filesystem, but sounds like a synonym ofWithDirectoryRootFS/WithRootFS) that sounds less like a synonym ofDirectory? (teamrootfshere as well)Container().From("alpine").WithRootFS(dir)would do?WithFSadditive (@vito's side note)?WithDirectory("/", ...)andWithFSbecome essentially the samekpenfound commentedon Nov 6, 2022
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 thanExec(cp.., but I agree it would feel more natural if there was at least an option toWith*a Directory or File in a way that is additive to the rootfsshykes commentedon Nov 6, 2022
Thanks for clarifying @aluzzardi ! I agree with the decision tree.
shykes commentedon Nov 6, 2022
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:
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.I agree with @vito that
WithRootFSis better. I think we should do this regardless of other improvements.I agree that we should not do this.
shykes commentedon Nov 10, 2022
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.
aluzzardi commentedon Nov 15, 2022
And another help post on Discord from today: https://discord.com/channels/707636530424053791/1041730164133986385/1041730164133986385
aluzzardi commentedon Nov 15, 2022
Implemented in #3827 - -
WithDirectory,WithFile,WithNewFilealuzzardi commentedon Nov 17, 2022
Implemented in #3882
container: Support WithDirectory, WithFile & WithNewFile
container: Support WithDirectory, WithFile & WithNewFile (#3827)
samalba commentedon Dec 22, 2022
I am closing this given #3882 and #3827, we can re-open this issue in the future if needed.