Docker exec lets you launch a shell or another command inside a running container without creating a new one. Learn how interactive flags such as -it work, and how exec differs from attaching to the main process or starting a separate container.

Multiple Choice

Which command is used to execute another process inside a running Docker container?

To run another process inside a running container, you need a command that injects a new process into that container’s existing namespace and filesystem. docker exec does exactly that: it starts the specified command as a new process inside the running container, using the container’s environment. You can run interactively with -it, for example: docker exec -it my-container bash. This differs from starting a new container with docker run, which would create a separate container; it also differs from docker attach, which connects you to the container’s main process rather than starting a new one. That’s why docker exec is the right choice for executing an additional process inside a running container.

When a container is already running, you don’t need to start a second one just to check a file or run a command. Docker’s exec command starts a new process inside the existing container. It’s a small command with a useful role: it lets you inspect or interact with a running container without changing its main process.

For example, this command opens a shell inside a container named my-container:


docker exec -it my-container bash

The flags make the session interactive and connect it to your terminal. Once inside, you can inspect files, check environment variables, or run a diagnostic tool—much as you would on a regular Linux system. There’s one catch: the container needs to be running, and it must include the shell or program you’re trying to start.

What docker exec does

A container runs one main process, often called its entrypoint or main process. That process might start a web server, a database, or a small service. With docker exec, Docker launches an additional process inside that running container.

Think of the container as a small, separate workspace. The main process is already doing its job, but you can ask Docker to start another task in that same workspace. The new process can use the container’s environment and access its files, subject to the container’s configuration and permissions.

A basic command looks like this:


docker exec CONTAINER COMMAND

For instance, to list files in a container’s /app directory:


docker exec my-container ls /app

This runs ls /app inside my-container and then returns the result to your terminal. It doesn’t open a shell, and it doesn’t replace the application that’s already running.

That distinction makes exec handy for short, focused tasks. You can check a setting, inspect a log file, or confirm that a program is present. When the command finishes, its process exits; the container’s main process keeps running.

The flags that make a difference

The two flags in the common shell example, -i and -t, serve different purposes:

  • -i keeps standard input open, so you can type into the process.

  • -t allocates a pseudo-terminal, which gives an interactive shell a familiar terminal-like interface.

Together, -it are useful when you want to work in a shell:


docker exec -it my-container sh

Many small container images include sh but not bash, so sh is often a safer first try. Minimal images may contain neither. That’s not a Docker error; the shell simply isn’t part of the image.

For a command that doesn’t need interaction, you can leave off -it:


docker exec my-container printenv

You can also run a command as a particular user with --user:


docker exec --user 1000 my-container whoami

This can help when a process needs permissions different from the container’s default user. Still, be thoughtful with elevated access. A quick command can change files or settings just as easily as it can inspect them.

exec isn’t the same as run or attach

Docker command names can sound similar, but they do different jobs.

docker run creates and starts a new container from an image. If you run:


docker run alpine echo hello

Docker starts a new container using the Alpine image, runs echo hello, and then the container exits. That’s useful when you want a fresh container, not when you want another process inside one that’s already running.

docker attach, meanwhile, connects your terminal to the main process of an existing container. It doesn’t create a new process. If the main process is a shell, attaching may feel like entering that shell. If it’s a web server, you’ll connect to that process’s input and output instead. Depending on how the container was started, keyboard input can affect the main process, so attach isn’t a general-purpose way to open a separate shell.

docker kill has a different purpose again: it sends a signal to stop a container’s main process. It doesn’t run commands inside the container. Use it with care, particularly for services that need time to shut down cleanly.

In short, exec starts another process in a running container; run starts a new container; attach connects to the main process; and kill sends a stop signal.

A few useful examples

Suppose an application container is running and you want to see whether a configuration file exists. You can ask for that one result directly:


docker exec my-container ls -l /etc/my-app

If you need several commands, an interactive shell may be easier:


docker exec -it my-container sh

From there, you can inspect files or run available tools. Type exit to leave the shell. This ends the shell process, not the container’s main process.

You can also run a command in the background with -d:


docker exec -d my-container /app/refresh-cache

Docker starts the process and returns control to your terminal without keeping an interactive session open. The command still runs in the container, and its output may not appear in your terminal. For ongoing services or scheduled work, it’s usually better to manage the process through the application’s normal design rather than relying on a one-off background command.

A container isn’t a full virtual machine

It’s tempting to treat a container like a small computer that you can log into whenever you want. That image is convenient, but not quite right. A container shares the host system’s kernel and uses isolation features to separate its processes and resources. It may contain only the files and tools its application needs.

That’s why a command such as bash may fail even when docker exec itself is working. The container could be based on a minimal image built to run one service, with no interactive shell at all. In that case, try a tool that is actually present, such as sh, or run a specific available command.

There’s another important point: changes made inside a running container may not last. If a container is removed and recreated from its image, edits to its writable layer can disappear. For lasting changes, update the image or store important data in a volume, depending on what you’re changing.

Where Docker fits in a Kubernetes setting

Docker remains a familiar tool for building images and working with containers on a local machine. Kubernetes, however, manages containers through a container runtime that follows the Container Runtime Interface (CRI). Docker Engine is no longer Kubernetes’ built-in runtime through the old dockershim component, though tools such as containerd and CRI-O are commonly used.

So, docker exec is useful in Docker workflows, but it isn’t a universal command for entering every container managed by Kubernetes. In a Kubernetes environment, kubectl exec serves a similar purpose for a container in a pod:


kubectl exec -it my-pod -- sh

The -- separates kubectl’s own arguments from the command that should run in the container. If a pod has more than one container, you may also need to name the target container with -c.

The idea is much the same: start a new process in a running container, rather than attach to its main process or create a fresh container. The command-line tool changes because the system managing the container changes.

Use it thoughtfully

docker exec is a practical way to inspect a live container, but it’s best used with a clear purpose. A quick read-only command is usually low-risk. Changing files, installing software, or starting a second copy of an application can produce confusing results—especially if those changes vanish when the container is replaced.

For routine operations, put the needed tools and configuration into the image, and send logs to a place where they can be collected. That keeps containers easier to reproduce and easier to understand. Still, when you need to check what’s happening right now, docker exec gives you a direct window into a running container: one new process, in the environment where the application lives.