I'm using kubectl cp to copy a jar file from my local file system into a the home directory of a POD in my minikube environment. However, the only way I can confirm that the copy succeeded is to issue a new kubectl cp command to copy the file back to a temp directory and compare the checksums. Is there a way to view the copied files directly?
Asked
Active
Viewed 6.0k times
37
-
It might be a bit too much for what you need but have a look at [ksync](https://vapor-ware.github.io/ksync/) maybe? – Michael Hausenblas Jan 03 '18 at 21:07
3 Answers
55
You can execute commands in a container using kubectl exec
command.
For example:
to check files in any folder:
kubectl exec <pod_name> -- ls -la /
or to calculate md5sum
of any file:
kubectl exec <pod_name> -- md5sum /some_file

nickgryg
- 25,567
- 5
- 77
- 79
-
Thanks! That's what I was missing. It makes sense now. The cp command is really more like rcp. The ls command is a true shell command. – Greg Charles Jan 04 '18 at 19:14
-
@nick318 '--' is used to mark the start of commands. should be good practice to always use it. ls and whatever tool you are calling, needs to exist in your container. Otherwise it will fail. – sigi Feb 28 '19 at 12:03
-
tried the command but getting: error: unable to upgrade connection: container not found ("app1") so, QUESTION: does this command work with Docker Desktop Kubernetes? thx! :-) – sairn Jul 22 '22 at 19:46
-
Note that this will not work if the pod's container doen't have ls command installed, which is the case for example with most pods in the kube-system namespace. – Luis Vazquez Mar 26 '23 at 16:07
3
I think it is really convenient to use shell or bash to navigate the files in the file system of the container as @JRA_TLL has pointed out.
Remember to open shell in the interactive mode though:
kubectl exec -it <podname> -- sh
When you open the shell, by default it opens in the application directory i.e. app folder. You can use shell commands like ls
to view folders , cd ..
to navigate etc. Using cd ..
in the app folder will get you to the root folder of the container.
We can also use:
kubectl exec -it --namespace <namespace> <podname> -- bash

Ayushmati
- 1,455
- 1
- 13
- 15
-
1Thanks @Ayushmati! To add, this is the command to get pod names ```kubectl get pods --namespace
``` – Michael Wegter Jul 05 '23 at 20:40
1
Most practical would be to start a shell in the container if you want to execute multiple commands, ie.
kubectl exec <pod_name> -- sh

JRA_TLL
- 1,186
- 12
- 23
-
When you open the shell, it opens in the application directory i.e. app folder. You can use shell commands like ls to view folders , cd .. to navigate etc. Using cd .. in the app folder will get you to the root folder of the container. – Ayushmati Nov 25 '22 at 13:22