Menu
Coddy logo textTech

Executing Commands

Part of the Introduction to Kubernetes section of Coddy's Terminal journey. Lesson 22 of 45.

To run a command inside a running container, use kubectl exec. Everything after -- is the command to run:

kubectl exec web -- env

This prints the environment variables inside the web Pod's container, including HOSTNAME=web: a container's hostname is its Pod's name.

Any command that exists in the image works, for example listing a directory:

kubectl exec web -- ls /

This is the everyday way to look around inside a container without stopping it, like docker exec.

On a real cluster, kubectl exec -it web -- sh opens an interactive shell. This practice terminal has no terminal to attach, so run one command at a time instead.

challenge icon

Challenge

Beginner

Start a Pod named web from the nginx image, then run env inside it with kubectl exec to see its HOSTNAME.

Try it yourself

Terminal
quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

All lessons in Introduction to Kubernetes

Practice on your own: Terminal playground