Skip to content

Conversation

@suyashbhawsar
Copy link

Type of Change

  • Bug fix
  • New feature
  • Breaking change
  • Documentation update

Motivation and Context

This PR adds the container cp command to copy files and directories between containers and the local filesystem, similar to docker cp. This is a commonly requested feature that enables easier file transfer workflows with containers.

Why is this change needed?

  • Users often need to copy configuration files, logs, or data between containers and the host
  • Currently there's no built-in way to transfer files without mounting volumes
  • This brings feature parity with Docker's cp command

Implementation Details

The implementation uses tar archives for file transfer (same approach as Docker):

  • copyToContainer: Local tar creates archive, streams to container tar for extraction
  • copyFromContainer: Container tar creates archive, streams to local tar for extraction

New files:

  • CopyPath.swift - Path parsing utility (distinguishes container:path vs local paths)
  • ContainerCopy.swift - Main command implementation
  • TestCLICopy.swift - 45 unit tests for edge cases

Features:

  • Copy files/directories to and from running containers
  • Rename during copy (different source/destination names)
  • -L/--follow-link flag to dereference symlinks
  • -a/--archive flag to preserve permissions (uid/gid)
  • -q/--quiet flag to suppress progress output

Usage examples:

container cp mycontainer:/app/config.json ./config.json
container cp ./local-file.txt mycontainer:/tmp/file.txt
container cp -L mycontainer:/var/log/ ./logs/

Testing

  • Tested locally
  • Added/updated tests
  • Added/updated docs

Unit tests (45 tests):

  • Path parsing for various formats (absolute, relative, container paths)
  • Unicode filenames (Chinese characters, emoji)
  • Special characters (spaces, brackets, quotes, etc.)
  • Hidden files, symlinks, edge cases

Integration tests performed:

  • Basic file copy (to/from container)
  • Directory copy with nested structure
  • Files with spaces, unicode, special characters
  • Symlink handling with/without -L flag
  • Error cases (non-existent paths, stopped containers)
  • Rename during copy

Implements Docker-style file copy between containers and the local
filesystem using tar archives for transfer.

Features:
- Copy files/directories to and from running containers
- Support for rename during copy (different source/destination names)
- -L/--follow-link flag to dereference symlinks
- -a/--archive flag to preserve permissions (uid/gid)
- -q/--quiet flag to suppress progress output
- Comprehensive error handling for edge cases

Usage examples:
  container cp mycontainer:/app/config.json ./config.json
  container cp ./local-file.txt mycontainer:/tmp/file.txt
  container cp -L mycontainer:/var/log/ ./logs/

Includes 45 unit tests for path parsing edge cases covering:
- Unicode filenames, emoji, special characters
- Spaces, hidden files, symlinks
- Various path formats and error conditions
@suyashbhawsar suyashbhawsar force-pushed the feature/container-cp-command branch from f4762e9 to 67a0d63 Compare January 7, 2026 21:01
@dcantah
Copy link
Contributor

dcantah commented Jan 8, 2026

@suyashbhawsar Hi! We really appreciate this work, but I think the right place for this change (and it would require quite a bit more work) would be in https://github.com/apple/containerization. copying in and out should be on the core types and not rely on the container having tar installed, it should be done by the guest agent.

@suyashbhawsar
Copy link
Author

@suyashbhawsar Hi! We really appreciate this work, but I think the right place for this change (and it would require quite a bit more work) would be in https://github.com/apple/containerization. copying in and out should be on the core types and not rely on the container having tar installed, it should be done by the guest agent.

Thanks, I'll take a look at the containerization repo and will integrate this there

@dcantah
Copy link
Contributor

dcantah commented Jan 11, 2026

@suyashbhawsar We have quite a few design choices (swift does not have a native tar library, and we don't want to link any external libraries to the guest agent if we can avoid it) to work through on that end unfortunately. We want to encourage contribution, but this is probably not the best issue to start with :(. Let me know if there's something in here you'd like to tackle and I'd be happy to help. Just give me a ping

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants