docker exec -it opens an interactive shell inside a container that is already running: -i keeps standard input open and -t allocates a terminal. In production, start with sh if you do not know what the image ships, set the user with -u and the directory with --workdir, and change nothing that would not survive a recreate.

docker exec -it: minimal syntax and what each flag does

The basic form is docker exec [options] CONTAINER COMMAND, and the command runs in the existing container rather than creating a new one. Find the exact name first, then step in:

docker ps
docker exec -it api sh

# With Docker Compose, by service name
docker compose exec api sh

-i wires your standard input to the process and -t gives it a pseudo-terminal; without -t you get no prompt and no line editing. docker compose exec allocates a terminal by default and -T turns that off, which is what scripts want. If the container is stuck in a restart loop, exec will fail: check docker logs --tail 100 api first.

sh vs bash in docker exec: how to tell which shell the image ships

Many minimal images, such as Alpine-based ones, ship sh but not bash, and distroless images ship no shell at all. Ask for bash where it does not exist and Docker returns an executable-not-found-in-$PATH error. Check instead of guessing:

docker exec api sh -c 'command -v bash || echo no-bash'

# Use bash if present, otherwise stay in sh
docker exec -it api bash
docker exec -it api sh

sh is the safe default: it almost always works, and reading logs or checking configuration does not need fancy completion. Do not install bash with the package manager inside the container for comfort; that install is gone after the next deploy. If the image has no shell, docker cp still works, so you can pull files out and read them on the host.

User in docker exec: -u, root and files with the wrong owner

docker exec runs as the same user as the container's main process by default, meaning the image's USER or the --user it was created with. Confirm who you are before touching anything:

docker inspect --format '{{.Config.User}}' api
docker exec api id

# Root only for what strictly needs it
docker exec -it -u 0 api sh

# Same UID:GID as the application
docker exec -it -u 1000:1000 api sh

An empty string from docker inspect means the container runs as root. The real risk of -u 0 is not only security: a file created as root inside a shared volume can block the application, running under another UID, from writing it later. Working rule: read as the application user, switch to root for one specific action, and leave right after.

--workdir in docker exec: open the shell in the directory that matters

-w or --workdir sets the command's working directory; without it, docker exec starts in the image's WORKDIR. Paired with -u, it removes the classic wrong-cd mistake:

docker exec -it -w /var/log/nginx web sh

# One-off command, no interactive shell
docker exec -w /app api ls -la

# User and directory together
docker exec -it -u www-data -w /var/www/html web sh

One-line checks do not need -it: a docker exec without a terminal prints the output and exits, which makes it easy to rerun and to paste into an incident report.

Changes made with docker exec that do not survive a container recreate

Anything written outside a volume lands in the container's writable layer, and docker compose up -d --force-recreate or any new deploy throws that layer away. A plain docker restart keeps it, which makes a hotfix look permanent when it is not. Before leaving, review what you touched:

# A = added, C = changed, D = deleted
docker diff api

# Paths that do persist (volumes and bind mounts)
docker inspect --format '{{range .Mounts}}{{.Destination}} {{end}}' api

# Take a copy before you start poking
docker cp api:/app/config/settings.yml ./settings.yml
  1. Do not install packages to debug; they vanish and they change the image you think you are running.
  2. Do not edit configuration inside the container; fix it in the image, the compose file or the volume.
  3. Do not run chmod or chown outside a volume; the next recreate reverts them silently.
  4. Do not kill PID 1; when the main process dies, the whole container stops.

docker exec -it from a phone vs a desktop: what changes

Getting into a container from a phone needs two things a desktop takes for granted: keys like Ctrl and Tab, and a session that survives moving around. Here is the comparison:

Need during docker execDesktopPhone with termi1
Ctrl+C, Ctrl+D, Tab and arrowsPhysical keyboardOwn keyboard with Esc, Ctrl, Tab and arrows
Network changeOver SSH the open shell is lostMosh keeps the session when switching from wifi to mobile data
Long tasks on the hosttmux, set up by handtmux: whatever you leave running is still there
Downloading a file pulled with docker cpscp or sftp in another windowBuilt-in SFTP without leaving the session
Typing the commandBy hand or from historyAI types it at the prompt without pressing Enter

On a connection tagged as production, the Production Guard in termi1, the mobile SSH terminal sends Enter to nothing automatically and holds back anything that deletes, stops or changes something before it reaches the prompt, so a docker exec with -u 0 never fires on its own. The AI does not open the session or receive keys, and the SSH private key never leaves the phone. You can get it from the termi1 app on Google Play; more server guides live on the termi1 technical blog.

Frequently asked questions

Why can't docker exec -it find bash in the container?

docker exec -it cannot find bash because many minimal images, such as Alpine-based ones, only include sh, and distroless images include no shell at all. Run docker exec -it container sh instead, and when there is no shell, pull files out with docker cp to inspect them on the host.

How do I get a root shell in a container with docker exec?

A root shell with docker exec uses -u 0, for example docker exec -it -u 0 api sh. Keep it to one specific action: files you create as root inside a volume can become unwritable for the application, which usually runs under a different UID.

Are changes made with docker exec lost when the container restarts?

Changes made with docker exec outside a volume survive docker restart, but they are lost when the container is recreated with docker compose up -d --force-recreate or by a new deploy. docker diff lists the files you added, changed or deleted in the writable layer.

What is the difference between docker exec and docker attach?

docker exec starts a new process inside the container, while docker attach connects to the input and output of the main process. With attach, a Ctrl+C can reach the main process and stop the container, so docker exec is the cautious choice for inspecting production.