Kubernetes Tutorial: Learn how to use kubectl to copy files from a pod to your local environment now!

This instructional covers a practical use case that appears with some frequency when developing Kubernetes (k8s)-based microservices and that involves using the kubectl command to copy a file from a pod to a local directory on your machine.

Kubectl Copy File From Pod To Local TOC

The kubectl cp command allows you to copy files from a pod to your local machine and helps to streamline file retrieval and management in Kubernetes.

Mastering this command is an essential skill for Kubernetes administrators, and it can also benefit engineers who are practicing CKAD exam questions and answers by providing hands-on experience with file management in k8s.

The format of the kubectl command used to copy a file from a pod to your local environment appears below.

				
					kubectl cp ${namespace}/${pod_name}:${path_to_file} ${path_on_your_machine}/${filename}
				
			

In the next section we’ll walk through an example where we execute the kubectl cp command on a pod running in minikube.

This article was last updated on June 12, 2025.

Do you need to hire an Interim CTO?

If you need to appoint a temporary Chief Technology Officer then I may be able to help you! Find a time that works for you on my calendar and we can discuss your requirements in more detail.

Use kubectl to copy files from a pod to your local environment

This section provides us with the prerequisites required to run this example, along with a test setup geared toward readers who are just becoming familiar with Kubernetes or who may be working on CKAD sample questions.

Prerequisites

This tutorial requires the following:

If you do not have a running pod to use then in the next step we will create one.

Step One (OPTIONAL: If you do not have a pod to test against, start here): Create the alpine-spin pod.

If you do not have a pod available to test against then we need to create one so we’ll use the alpine-spin image.

The alpine-spin image is a lightweight container image based on Alpine Linux and is designed for testing or demonstration purposes in Kubernetes and is suitable for our needs.

The following is an example configuration file for a pod based on the alpine-spin image:

				
					apiVersion: v1
kind: Pod
metadata:
  name: alpine-spin-pod
  namespace: alpine-spin-ns
  labels:
    role: myrole
spec:
  containers:
    - name: alpine-spin-container
      image: kubegoldenguide/alpine-spin:1.0.0
      ports:
        - name: web
          containerPort: 80
          protocol: TCP
				
			

Save this configuration in the file named alpine-spin-configuration.yaml and then execute the following command to create the pod in the alpine-spine-ns namespace in minikube from :

				
					kubectl apply -f ./alpine-spin-configuration.yaml
				
			

An example of a successful execution of this command appears below.

In the first (optional) step we create the alpine-spin-pod pod using the following commands: 1.) kubectl create namespace alpine-spin-ns 2.) kubectl apply -f ./alpine-spin-configuration.yaml 3.) kubectl get pods --namespace alpine-spin-ns
Step One: Create the alpine-spin-pod pod in with full output

In the next step we’ll copy the /var/hosts file from the alpine-spin-pod pod to a local directory on our machine.

Step Two: Use kubectl to copy files from a pod to your local environment

In this step we will use the kubectl cp command to copy a file from the alpine-spin-pod pod to a local directory on our machine.

The following kubectl command will do exactly this:

				
					kubectl cp alpine-spin-ns/alpine-spin-pod:/etc/hosts ./alpine-spin-pod-var/hosts-from-alpine-spin-pod
				
			

A breakdown of this command is as follows:

  • kubectl cp — the kubectl copy command.
  • alpine-spin-ns — the k8s namespace.
  • alpine-spin-pod — the pod name.
  • /etc/hosts — the absolute path to the hosts source file in the pod.
  • ./alpine-spin-pod-var/hosts-from-alpine-spin-pod — the relative path to the hosts-from-alpine-spin-pod target file on the developer’s machine.

Successful execution of the kubectl cp command should look something like what we have below:

Output from running the kubectl cp command to copy a file from a pod to a local directory with pointers as follows: Red points to the kubectl cp command, Orange: points to the "tar: removing leading '/' from member names" notice, Yellow points to the cat command applied to the hosts-from-alpine-spin-pod file, and Green points to the contents of the hosts-from-alpine-spin-pod file.
Step Two: Output for the kubectl command to copy a file from a pod to a local directory.

The arrows in the above image are as follows:

  • Red points to the kubectl cp command.
  • Orange points to the “tar: removing leading ‘/’ from member names” notice.
  • Yellow points to the cat command applied to the hosts-from-alpine-spin-pod file.
  • Green points to the contents of the hosts-from-alpine-spin-pod file.

This concludes step two, the tutorial conclusion follows.

Tutorial Conclusion

In conclusion, using the kubectl cp command is an efficient way to transfer files between your Kubernetes pods and your local filesystem.

Mastering these file transfer techniques is crucial for Kubernetes administrators and developers who often need to interact with pods for debugging or managing containers.

See Also

Other tutorials focusing on aspects of Kubernetes can be found below and also in the k8s categories page.

  1. Learn how to use kubectl to get the cluster name in this guide.
  2. Tutorial: Learn how to quickly install the Metrics Server in minikube in five easy steps!
  3. Learn how to mount a local directory in minikube by exploring three possible solutions.
  4. Example One: Learn how to create a Kubernetes pod in a namespace in this tutorial.
  5. Example Two: Learn how to create a ConfigMap in Kubernetes in this tutorial.
  6. Example Three: Set user IDs and file system group IDs in containers using the Kubernetes SecurityContext.
  7. Example Four: Learn how to configure configure Kubernetes liveness and readiness probes.
  8. Example Five: Learn How To Create A Deployment With Kubernetes Replicas Now!

Frequently Asked Questions (FAQ)

What does the "tar: removing leading '/' from member names" message mean?

The “tar: removing leading ‘/’ from member names” notification appears during the execution of the kubectl cp command because tar is used internally to transfer files, and tar will remove absolute paths to avoid overwriting files in the root directory.

This notice cannot be suppressed however the appearance of it in the output does not indicate that the execution of the copy operation has encountered any problems and hence can be ignored.

How to copy a file from Kubernetes pod to local?

To copy a file from a Kubernetes pod to your local machine, use the kubectl cp command as follows:

  1. Identify the pod name using kubectl get pods.
  2. Run the command: kubectl cp <pod-name>:/path/in/pod /path/on/local

Ensure that you have the necessary permissions and that the pod is running.

This method allows you to transfer files efficiently for debugging or data extraction.

Dive into the detailed Kubernetes file transfer tutorial: How to Use kubectl to Copy Files from a Pod to Your Local Environment.
author avatar
ThosPFuller
I am a software engineer based in Northern Virginia (USA) and this website focuses on content engineering, web development, Technical SEO, Search Engine Optimization (SEO).

Leave a Reply