🐳 Docker Beginner

What is docker exec?

Why Interviewers Ask This

Foundational questions like this help interviewers calibrate the rest of the interview. A confident, accurate answer signals that you have solid Docker basics — a prerequisite for any developer role.

Answer

docker exec runs a command inside an already-running container. Unlike docker run which creates a new container, docker exec executes within the existing container's environment. Syntax: docker exec [options] container command [args]. Common uses: (1) Interactive shell: docker exec -it mycontainer bash — opens a bash shell; use sh if bash isn't available (Alpine images); (2) Run a one-off command: docker exec mycontainer php artisan migrate; docker exec mydb mysql -u root -p -e "SHOW DATABASES;"; (3) Check a file: docker exec mycontainer cat /app/config.json. Options: -i — keep stdin open (interactive); -t — allocate TTY; -e VAR=value — set env var for this exec; -u user — run as specific user; -w /path — set working directory. Important: docker exec only works on running containers. The command runs in the container's namespace and sees the container's filesystem, environment, and processes. Changes to the container's filesystem are visible to the main container process. This is the primary debugging tool for investigating container issues.

Pro Tip

Demonstrate both theoretical understanding and practical experience. Say what it is, then give an example of how you actually used it in a Docker codebase.