How to run commands after login to shell command of a container in a VM?

6/18/2019

when I ran this command

kubectl exec -it $(kubectl get pods | grep container-name | awk '{print $1}') /bin/sh

I ssh into my container,

/src #

Now - I want to add 2 more commands so I did

kubectl exec -it $(kubectl get pods | grep container-name | awk '{print $1}') /bin/sh ; cd app/Http/Controllers; ls -lrt; 

but I still see this

/src #

I am supposed to see this

/src # cd app/Http/Controllers; ls -lrt;                                                
total 44                                                                                
-rw-r--r--    1 root     root           361 Jun 11 11:27 Controller.php                   
-rw-r--r--    1 root     root          1734 Jun 11 11:27 ClientController.php             
-rw-r--r--    1 root     root          1702 Jun 11 11:27 BroadcastController.php        
-rw-r--r--    1 root     root          5257 Jun 17 15:24 NodeController.php              
-rw-r--r--    1 root     root          2844 Jun 17 20:21 AccountController.php          
/src/app/Http/Controllers #
-- cyber8200
bash
containers
docker
kubernetes-pod
shell

1 Answer

6/18/2019

There is no need to go inside docker container do as below

kubectl exec -it $(kubectl get pods | grep container-name | awk '{print $1}') -- ls -lrt /app/Http/Controllers;

kubectl will execute every command inside container which comes after --. also you can mount your target directory and perform ls on localhost. create a yaml file like the one here.

apiVersion: v1
kind: Pod
metadata:
  name: test-container
spec:
  containers:
  - name: my-container
    image: my-container
    volumeMounts:
    - name: my-storage
      mountPath: /data/
  volumes:
  - name: my-storage
    hostPath:
        # directory location on host
        path: /opt/data
        # this field is optional
        type: Directory

then run this command kubectl apply -f your-file.yaml

-- Siyavash vaez afshar
Source: StackOverflow