Bash / Docker exec file redirection from inside a container
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.
Introduction
The key thing to understand about docker exec and shell redirection is where the shell is running. If you write docker exec container command > file, the > is usually handled by the host shell, not by a shell inside the container.
Redirection outside the container
This command writes to a file on the host:
Why? Because your host shell processes > before docker exec runs. The container executes echo hello, and the resulting standard output is redirected by the host shell into output.txt on the host filesystem.
That surprises people who expected /output.txt to appear inside the container.
Redirection inside the container
If you want the redirection to happen inside the container, run a shell inside the container and let that shell interpret >.
Now the redirection happens inside the container because sh -c runs there and processes the redirection expression.
This is the most important pattern in the whole topic.
Reading a host file into a command running in the container
Input redirection on the host side works the same way. For example:
Here:
- '
< localfile.txtis handled by the host shell' - '
docker exec -ikeeps stdin open' - '
cat > /tmp/input.txtruns inside the container'
This is a common way to copy content into a file inside a running container without using docker cp.
Capturing command output from the container to the host
If the goal is to save output from a containerized command into a host file, the host-side redirection is exactly what you want:
The file ends up on the host, which is often useful for logs, backups, and extracted data.
When quoting becomes important
Because two shells may be involved, quoting mistakes are common. If the command contains >, >>, pipes, variable expansion, or command substitution that must happen inside the container, wrap it in sh -c '...'.
For example:
Without sh -c, Docker tries to execute grep directly with literal tokens, and the redirection is not interpreted inside the container.
Choose the right tool for the job
For repeated file transfer tasks, docker cp may be simpler than clever stdin or stdout redirection. But for quick one-off piping, docker exec plus shell redirection is often very convenient once you understand which side handles the operator.
That is the real mental model:
- host shell handles unquoted redirection outside the exec command
- container shell handles redirection only when you explicitly run one inside the container
Common Pitfalls
- Assuming
>afterdocker execwrites to a file inside the container. - Forgetting
-iwhen piping or redirecting input into a command inside the container. - Omitting
sh -cwhen the command needs shell features inside the container. - Losing track of which shell expands variables and redirection operators.
- Using redirection tricks when
docker cpwould be simpler and clearer.
Summary
- '
docker exec container command > filewrites to a host file, not a container file.' - To redirect inside the container, run a shell there with
sh -c. - Use
-iwhen piping input into the containerized command. - Be explicit about whether redirection should happen on the host or in the container.
- Once you understand which shell owns
>or<, the behavior becomes predictable.
Related reading
- Brew install docker does not include docker engine?
- Build a multi node Kafka cluster on docker swarm
- Build and run Dockerfile with one command
- build context for docker image very large
- Build with docker and --privileged
- BundlerGemNotFound Could not find mimemagic-0.3.5 in any of the sources on Rails project with Docker
- Calling an external service from within Minikube
- Calling redis-cli in docker-compose setup

System Design Fundamentals
Build a strong foundation in designing scalable, reliable distributed systems.
View the courseSystem Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.