Skip to content

container/cp: don't print success message when copy fails - #7515

Merged
vvoland merged 1 commit into
docker:masterfrom
xh123637:fix/7514-no-success-on-copy-failure
Nov 12, 2026
Merged

vvoland merged 1 commit into
docker:masterfrom
xh123637:fix/7514-no-success-on-copy-failure

Conversation

@xh123637

Copy link
Copy Markdown
Contributor

Fixes: #7514

Summary

docker cp printed the Successfully copied ... summary unconditionally in
the non-quiet paths of both copyFromContainer and copyToContainer, even
when the copy itself failed. Copying a directory from a container onto an
existing local regular file therefore printed
Successfully copied 0B to ... before reporting cannot copy directory
and exiting with code 1.

This change only prints the summary when the copy succeeded, in both
directions. The quiet paths already returned the error without printing a
summary and are unchanged.

Release notes (optional)

Fix `docker cp` printing a "Successfully copied" message when the copy fails.

Testing

New regression tests in cli/command/container/cp_test.go:

  • TestCopyFromContainerDoesNotReportSuccessOnFailure: copying a directory
    onto an existing regular file returns cannot copy directory, leaves the
    destination unchanged, and prints no success message.
  • TestCopyToContainerDoesNotReportSuccessOnFailure: a failed upload prints
    no success message.

Both tests failed before this change (the success message was printed) and
pass after it. The existing TestCopy* / TestRunCopy* tests in
cli/command/container still pass, and gofmt and go vet are clean for
the changed package.

Note: TestContainerExportOutputToFile fails in this environment both with
and without this change (gid expectation mismatch), so it is unrelated to
this change. No end-to-end run against a real daemon was done.

Comment thread cli/command/container/cp.go Outdated
Comment on lines +337 to +342
reportedSize := copiedSize
if !cpRes.Stat.Mode.IsDir() {
reportedSize = cpRes.Stat.Size
if res == nil {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should have a look at this code in general, because it's rather convoluted (definitely for a follow-up), but perhaps as part of this PR, we should consider renaming res to err - looking at the code didn't make it clear that res is just an error, not some other "result" return type.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done — renamed res to err in copyFromContainer (commit 317a13c). Since err was already declared in this function, the CopyTo call now assigns to it directly. I left the broader cleanup of this code for a follow-up, as you suggested.

@thaJeztah

Copy link
Copy Markdown
Member

Thanks for updating; could you;

Use a known identity (sorry, no anonymous contributions.)

@denlo-0

denlo-0 commented Nov 12, 2026

Copy link
Copy Markdown

Thanks for the fix! I tested 317a13c against a real Docker Engine and confirmed that it resolves the issue. Successful copies still work as expected.

@xh123637
xh123637 force-pushed the fix/7514-no-success-on-copy-failure branch from 317a13c to ee70962 Compare November 12, 2026 17:33
@xh123637

Copy link
Copy Markdown
Contributor Author

Thanks for the pointers — all three addressed in the new head commit ee70962:

  • Squashed into a single commit.
  • Removed the Fixes: line from the commit message (the issue link stays in the PR description).
  • The sign-off now uses my real name: Signed-off-by: Xiong Han <[email protected]>, with the commit author/committer set to the same name. DCO is green.

No code changes — the tree is identical to the previous head.

thaJeztah
thaJeztah previously approved these changes Nov 12, 2026

@thaJeztah thaJeztah left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@codecov-commenter

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.

📢 Thoughts on this report? Let us know!

@thaJeztah

Copy link
Copy Markdown
Member

Looks like a linter isn't happy now 😢

cli/command/container/cp.go:357:1: cyclomatic complexity 17 of func `copyToContainer` is high (> 16) (gocyclo)
81.65 func copyToContainer(ctx context.Context, dockerCLI command.Cli, copyConfig cpConfig) error {
81.65 ^

@thaJeztah
thaJeztah force-pushed the fix/7514-no-success-on-copy-failure branch from ee70962 to 07910e5 Compare November 12, 2026 20:57
@thaJeztah

Copy link
Copy Markdown
Member

Pushed a fix (added some //nolint comments for now)

vvoland
vvoland previously approved these changes Nov 12, 2026
The non-quiet copy paths printed the "Successfully copied ..." summary
unconditionally, even when the copy failed. Copying a directory onto an
existing regular file therefore printed "Successfully copied 0B" before
reporting "cannot copy directory". Only print the summary when the copy
succeeded, in both the from-container and to-container directions.

Also rename the res variable in copyFromContainer to err: it only holds
the error returned by archive.CopyTo, so name it err to make that clear
instead of suggesting some other "result" return type.

Signed-off-by: Xiong Han <[email protected]>
Signed-off-by: Sebastiaan van Stijn <[email protected]>
@thaJeztah
thaJeztah force-pushed the fix/7514-no-success-on-copy-failure branch from 07910e5 to 25552ba Compare November 12, 2026 21:11
@thaJeztah
thaJeztah requested a review from vvoland November 12, 2026 21:11
@thaJeztah

Copy link
Copy Markdown
Member

Derp; one //nolint too many; should be good now 😅

@vvoland
vvoland merged commit eaae503 into docker:master Nov 12, 2026
100 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

docker cp prints “Successfully copied” before reporting “cannot copy directory”

5 participants