Skip to content
BENIBIX Learn

docker exec

Infrastructure📦 ContainersRuntimes and Tools

Also known as docker exec Command, docker container exec, docker exec -it

Runs one more command in a live box. Often it is a shell.

Think of it like

Think of it like a door into a room. The room is in use, and you walk in.

Example

Lucy's web container shows an error. She types docker exec -it web sh, reads the config file inside, and leaves with exit.

Why it matters

It is the quickest way to look inside a live container. Most fixing starts there.

Definition

docker exec runs a new command inside a container that is already running. The most common use is opening a shell. You type docker exec -it, the name and sh. The -i keeps input open, and -t gives you a proper terminal. The command runs beside the main program and ends when you exit. It only works while the container is running.

Example code

console
$ docker exec -it web sh
/ # cat /etc/hostname
4f1c2a9b7e3d
/ # exit
Lucy is now inside the web container. The prompt stays changed until she types exit.
docker exec is the side door. It opens a shell in a container that is already running.

Common mistake

Fixing things by hand inside a container. The changes are lost when the container is replaced, so fix the image instead.

Key takeaways

  1. 1
    Already running. It needs a live container.
  2. 2
    -it for a shell. Input kept open, with a terminal.
  3. 3
    Not saved. Changes go when the container goes.

Compare