# docker exec

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

**Field:** Infrastructure  
**Category:** Containers  
**Also known as:** docker exec Command, docker container exec, docker exec -it

## Think of it like

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

## 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

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

## 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.

## Why it matters

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

## Common mistake

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

## Three principles

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

## Related terms

- [Docker](https://learn.benibix.com/term/infrastructure/docker)
- [docker run](https://learn.benibix.com/term/infrastructure/docker-run)
- [Container](https://learn.benibix.com/term/infrastructure/container)
- [Shell](https://learn.benibix.com/term/infrastructure/shell)
- [Terminal](https://learn.benibix.com/term/infrastructure/terminal)

## Told apart from

- [docker run vs docker exec](https://learn.benibix.com/compare/docker-run-vs-docker-exec): People think docker run takes you back into a container, and it does not. docker run always makes a new container from an image. docker exec runs a command in one that is already running. Type docker run twice and you have two containers, each with its own files.

![An image on the left. An arrow labelled docker run leads to a running container. A side arrow labelled docker exec opens a shell inside it. Another arrow leads to a stopped container, and a curved arrow labelled restart policy leads back to running.](https://learn.benibix.com/diagrams/inf4-container-life.svg)

---

Source: https://learn.benibix.com/term/infrastructure/docker-exec
Author: Behnia Zarei (a developer in Vienna, Austria, who builds and teaches data, AI and business intelligence at BENIBIX)
Publisher: BENIBIX Learn (https://learn.benibix.com/)
Last updated: 2026-11-17
Licence: CC BY 4.0 (https://creativecommons.org/licenses/by/4.0/). Quote freely with attribution.
