Skip to content

[Request]: Add container cp command to copy files into and out of running containers #232

Description

@euanh

Feature or enhancement request details

Many container runtimes offer a cp command which can copy files into and out of running containers.

Usually the cp command has a few of main modes of operation:

  • copy a single file in or out of the container
  • recursively copy a directory in or out of the container
  • create a tar archive of a path in the container and stream it out to the host, or receive a tar stream from the host and unpack it in the container

There are also often options to control the details of how symlinks and permissions are handled.

Swift SDK Generator currently relies on the tar streaming mode to extract Swift SDKs from container images: Sources/SwiftSDKGenerator/Generator/SwiftSDKGenerator.swift#L122

It's possible to work around the lack of cp by execing tar in the container image and piping the output to tar on the host, however this isn't a general solution because the container might not have a tar binary.

Code of Conduct

  • I agree to follow this project's Code of Conduct

Activity

  1. added theissue type on Jun 18, 2025
  2. katiewasnothere commented on Jul 11, 2025

    @katiewasnothere
    Contributor

    Update on this: I started the work here. The overall design we were going for was to create a tar file of the requested content either on the host or container side depending on the direction requested, then send that via the vminit GRPC client and untar on the receiving side.

    Since swift does not have any native libraries for dealing with tar files, we looked into using our ContainerizationArchive library instead. However, it looks like the swift linux SDK does not include libarchive (and some others) today, which we would need for using ContainerizationArchive for the vminitd side of the code since that runs in the linux guest.

    One option would be for us to make our own tar swift library for use. Another option is to manually pull in the libarchive library to the linux SDK root when we build (I would like to avoid this unless absolutely necessary). I'd love to hear if others have alternative suggestions!

  3. katiewasnothere commented on Jul 11, 2025

    @katiewasnothere
    Contributor

    Here's the exact list of libraries we're missing when building vminitd with swift linux SDK

    [1/1] Planning build
    Building for debugging...
    error: link command failed with exit code 1 (use -v to see invocation)
    ld.lld: error: unable to find library -lbz2
    ld.lld: error: unable to find library -llzma
    ld.lld: error: unable to find library -larchive
    clang: error: linker command failed with exit code 1 (use -v to see invocation)
    [129/131] Linking vminitd
    

    My containerization branch is here in case anyone is curious. However, the code was not done and the branch is still in a bad shape so keep that in mind 😅

  4. gleison commented on Jul 17, 2025

    @gleison

    Hi!
    There is a workaround to copy a file to container?
    I tried something like that:

    container exec -i container_id sh -c 'cat > ./file' < ./file

    but i get a error:

    container/RunCommand.swift:223: Fatal error: 'try!' expression unexpectedly raised an error: Error Domain=NSCocoaErrorDomain Code=512 "The file couldn’t be saved." UserInfo={NSUnderlyingError=0x600000390630 {Error Domain=NSPOSIXErrorDomain Code=22 "Invalid argument"}}

  5. katiewasnothere commented on Jul 17, 2025

    @katiewasnothere
    Contributor

    Hi @gleison you could try adding a virtiofs mount to your container and copying your files into that. Would that work for your scenario?

  6. ggeoffre commented on Sep 21, 2025

    @ggeoffre

    One of the more common scenarios I have used the 'docker cp' for in the past was to copy an initialization script to a database container like the following:

    docker cp init.cql cassandra-container:/tmp
    docker exec -it cassandra-container cqlsh -f /tmp/init.cql
    

    It is very convenient in such cases to be able to copy the init.cql file over and over to reset the database. This way I do not have to rebuild the container, or stop, delete and run a fresh image of a pre-built all the time. I have also noticed that the "docker cp" command is able to create directories (and sub directories) inside the container even if they are not there in the first place.

  7. added this to the 2026-02 milestone on Jan 24, 2026
  8. modified the milestones: 2026-02, 2026-03 on Feb 11, 2026
  9. modified the milestones: 2026-04, 2026-05 on Apr 25, 2026
  10. mvanhorn commented on Apr 30, 2026

    @mvanhorn
    Contributor

    I see this issue is labeled next so the team plans to ship it. If there's room for an external contribution while you're busy with other work this month (per @jglogan's note on #1463), I'd like to scope a cp implementation that follows the ContainerExport pattern.

    Sketch of the approach:

    • New Sources/ContainerCommands/Container/ContainerCp.swift as a built-in subcommand under the existing Container group in Application.swift. Symmetric with Export/Exec rather than a plugin, since every other lifecycle command is built-in.
    • Two positional arguments parsed as Docker does: detect : to determine direction. Both host → container and container → host modes, plus - for stdin/stdout streaming so callers like Swift SDK Generator (cited in the issue) can pipe directly.
    • -L / --follow-link flag matching Docker's default-no-follow behavior.
    • Container → host reuses the tar archive path that ContainerExport already produces; host → container needs a new import(id:archive:targetPath:) on the API server unless one already exists.
    • Tests under Tests/CLITests/Subcommands/Cp/ modeled on TestCLIRunLifecycle.swift.

    Two questions before I open a PR:

    1. Is there an internal design doc or in-flight branch I should be aware of so this doesn't conflict?
    2. If host → container and container → host don't both fit cleanly in one PR, would you prefer to split (start with host → container) or ship together?
  11. JaewonHur commented on Apr 30, 2026

    @JaewonHur
    Contributor

    @mvanhorn Hi! We have a PR working on it #1190. Sorry for delay! I really hope this PR merged soon.

  12. jglogan commented on May 21, 2026

    @jglogan
    Contributor

    Closed by #1190.

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

Metadata

Metadata

Assignees

Labels

No labels
No labels

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions