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?
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 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:
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.
- Learn how to use kubectl to get the cluster name in this guide.
- Tutorial: Learn how to quickly install the Metrics Server in minikube in five easy steps!
- Learn how to mount a local directory in minikube by exploring three possible solutions.
- Example One: Learn how to create a Kubernetes pod in a namespace in this tutorial.
- Example Two: Learn how to create a ConfigMap in Kubernetes in this tutorial.
- Example Three: Set user IDs and file system group IDs in containers using the Kubernetes SecurityContext.
- Example Four: Learn how to configure configure Kubernetes liveness and readiness probes.
- Example Five: Learn How To Create A Deployment With Kubernetes Replicas Now!
Frequently Asked Questions (FAQ)
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.
To copy a file from a Kubernetes pod to your local machine, use the kubectl cp command as follows:
- Identify the pod name using kubectl get pods.
- 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.











