How to pass in kubernetes config into a docker run command?

5/12/2021

I want to run a command on a docker container that has kubectl installed but not yet configured.

On my local machine, I have my kubectl authenticated and configured to communicate with a kubernetes cluster on Google Cloud.

If I run the command:

docker run mycontainerimage kubectl get pods

I'll get the response:

invalid configuration: no configuration has been provided

How can I pass my kubernetes credentials/configuration into the docker image running on my local machine? Thanks!

-- aztrorisk
configuration
docker
kubernetes

2 Answers

5/13/2021

Actually, the Kubectl Configuration/authentification depends on many resources and symlinks in the environment to connect to the cluster.

I think the best and the most secured solution is to configure a VPN that provides secure remote SSH access in the cluster and connect to the API-Server Remotely.

Kubernetes support SocketXP, here is a step-by-step guide about how to Configure local kubectl to access remote Kubernetes cluster.

-- Hajed.Kh
Source: StackOverflow

5/12/2021

You can mount your config file with -v into the container

docker run -v ~/.kube/config:/kube/config ...

and set the environment variable KUBECONFIG

docker run -v ~/.kube/config:/kube/config --env KUBECONFIG=/kube/config ...

now every kubectl call should use the mounted config file.

-- Andreas Billmann
Source: StackOverflow