⚡ ~/naveed Interview Prep
⚡ Portfolio Home 👤 About Naveed Bio & Credentials 📫 Contact Naveed Get In Touch ✍️ Engineering Blog Deep Dives 🎯 Interview Hub 1,000+ Scenarios ☸️ Kubernetes Mastery Hub 24 Modules 🎮 DevOps Arcade & Quizzes Subnet Blitz ⚡ 🗺️ DevOps Roadmaps PDFs & Guides 🤖 Morpheus Analysis AI Quant ↗ 🛠️ Developer Tools Utilities 🧪 Labs & Experiments 📄 Interactive CV & Certs 🔗 All Links & Socials ⚡ Join The Dispatch (Weekly SRE Newsletter) →
← Back to All Docker & Containers Interview Questions Scenario 23 of 159 in Docker & Containers
Senior DevOps / SRE [L2] Docker Production Scenario [L2]
🎯 Target Role / Context: Docker Scenario #23 (L2) · Docker

Q: How do you copy a file from a container to the host?

docker cp <container>:/path/to/file /host/path.

#Docker #L2 #Containers #Linux
🎙️ Candidate Opening & Architectural Context
"To <strong>copy a file from a container to the host</strong>, use <code>docker cp &lt;container&gt;:/path/in/container /path/on/host</code>. It works on running and stopped containers and does not need a shell or <code>tar</code> in the image. Interviewers ask it because it is an everyday debugging task with several traps: ownership, symlinks, mounted paths and the fact that you can pull files from a crashed container for forensics. A thorough answer lists alternatives such as bind mounts, streaming <code>tar</code> and BuildKit <code>--output</code> for build artifacts."
Advertisement
⚡ Recommended Practice Lab

Want to master this scenario in a live sandbox? KodeKloud's Docker Certified Associate (DCA) Hands-On Lab Course covers this exact problem with hands-on terminal drills.

🛠️ Production Runbook & Step-by-Step Resolution

Direct Answer: To <strong>copy a file from a container to the host</strong>, use <code>docker cp &lt;container&gt;:/path/in/container /path/on/host</code>. It works on running and stopped containers and does not need a shell or <code>tar</code> in the image. Interviewers ask it because it is an everyday debugging task with several traps: ownership, symlinks, mounted paths and the fact that you can pull files from a crashed container for forensics. A thorough answer lists alternatives such as bind mounts, streaming <code>tar</code> and BuildKit <code>--output</code> for build artifacts.

1️⃣

Mental model: docker cp talks to the daemon

Unlike exec cat, docker cp reads the container filesystem through the Docker API, so the container does not have to run or contain any tools. Choose the method by whether the container is running, how big the data is, and whether the task will repeat.

  • Direction: either way works. docker cp c:/src dest copies out, and docker cp src c:/dest copies in.
  • Stopped containers: the writable layer persists until removal, so you can recover logs and dumps from an exited container.
  • Directories: they copy recursively. A trailing /. on the source copies the contents rather than the directory itself.
  • No shell required: works with distroless and scratch images, where exec cannot help.
  • Path rules: the container path is always absolute inside the container filesystem, and relative host paths resolve against your current directory, so check pwd before copying large trees.
2️⃣

Hands-on: copy a file from a container to the host

Basic forms and the flags that matter.

docker cp my-app:/var/log/app/error.log ./error.log
docker cp my-app:/etc/nginx/. ./nginx-conf/        # contents only
docker cp -a my-app:/data ./data-backup           # keep UID/GID

# Works on a stopped container too
docker ps -a --filter name=my-app
docker cp my-app:/tmp/core.1234 ./core.1234
  • Single file: copy to the current directory with ..
  • Directory contents: use /. to avoid nesting an extra folder.
  • Preserve ownership: -a (archive) copies UID/GID information. By default files on the host end up owned by the invoking user.
  • Follow symlinks: -L follows a symlink in the source path instead of copying the link.
  • Overwrite behaviour: copying onto an existing host file replaces it without prompting. Copy into a new directory when you are collecting evidence to avoid clobbering earlier captures.
Advertisement
3️⃣

Alternatives when cp is not enough

Pick the method that fits automation, large trees or build artifacts.

# Stream a directory with compression
docker exec my-app tar -C /data -czf - . > data.tgz

# Export build output directly to the host (BuildKit)
docker build --target artifacts --output type=local,dest=./out .
ls ./out
  • Stream with tar: docker exec my-app tar -C /data -cf - . | tar -xf - -C ./out handles many files and lets you compress. It needs tar in the image.
  • Pipe a single file: docker exec my-app cat /etc/hosts > hosts.txt is simple but requires a running container and a cat binary.
  • Bind mount up front: if you know you will need the output, mount a host directory so files appear without copying.
  • Build artifacts: with BuildKit, docker build --output type=local,dest=./out . exports files from a build stage (for example a FROM scratch stage) with no container to manage.
  • Compose services: docker compose cp web:/app/out.txt ./out.txt addresses the service by name, which is convenient when container names include project prefixes.
4️⃣

Verify the copy and avoid pitfalls

A successful exit code does not guarantee you copied the right thing. Check each symptom against the evidence before changing anything.

  • Check integrity: compare sha256sum of the host file with docker exec my-app sha256sum /path when it matters.
  • Special mounts: files under tmpfs mounts and under /proc or /sys cannot be copied with docker cp. Read them with exec.
  • Permissions: unreadable output often means root ownership with -a. Fix with sudo chown.
  • Large or sensitive files: copying core dumps or database files can expose secrets. Store them with restricted permissions and delete after use.
  • Size awareness: copying a multi-gigabyte directory goes through the daemon and can fill the host disk. Check docker exec my-app du -sh /path first.
5️⃣

Production practice: avoid manual copies

Repeated copying is a sign that output should go somewhere durable.

  • Ship logs and dumps out: write logs to stdout and use a log driver or agent. Mount a volume for heap dumps.
  • Kubernetes: kubectl cp ns/pod:/path ./path requires tar in the container. kubectl exec with cat or ephemeral containers works otherwise.
  • CI artifacts: use BuildKit --output or your CI artifact upload rather than docker cp from a leftover container.
  • Remote daemons: with a remote Docker context the file lands on your local machine, not the remote host, which is useful for pulling diagnostics but slow over poor links.
💡 The Senior SRE Gold Nugget (Key Architectural Takeaway)
"Pro-Tip: <code>docker cp</code> works on stopped containers and needs no shell, which makes it the first move for pulling logs or dumps from a crashed container before you remove it."
⚡ 60-Second Elevator Pitch Talking Points
  • I use <code>docker cp container:/path ./dest</code>, which reads through the daemon so it works on stopped containers and shell-less images.
  • I verify with a checksum and use <code>-a</code> when ownership matters, or tar streaming for large trees.
  • For repeat needs I mount volumes or use BuildKit <code>--output</code> instead of manual copying.
⛔ Critical Candidate Mistakes to Avoid in This Scenario

Senior hiring managers and bar raisers evaluate how you prioritize during outages. Avoid these common anti-patterns:

  1. Removing a crashed container before copying its files The writable layer disappears with the container. Copy logs and dumps first.
  2. Expecting docker cp to read tmpfs or /proc It does not support those mounts, so you get errors or empty results. Use exec to read them.
  3. Forgetting the trailing /. on directories Without it you copy the directory itself, creating an unexpected nested folder.
  4. Using exec cat on binary files via a TTY Allocating a TTY (-t) can corrupt binary output. Use docker cp or no -t.
Advertisement
Want more Docker & Containers scenarios?
Explore our complete collection of scenario-based Docker & Containers interview runbooks.
Browse All Docker & Containers Questions →