Bash
Docker
File Redirection
Docker Exec
Container Operations

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.

Practice system design

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:

bash
docker exec my-container echo hello > output.txt

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

bash
docker exec my-container sh -c 'echo hello > /tmp/output.txt'

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:

bash
docker exec -i my-container sh -c 'cat > /tmp/input.txt' < localfile.txt

Here:

  • '< localfile.txt is handled by the host shell'
  • 'docker exec -i keeps stdin open'
  • 'cat > /tmp/input.txt runs 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:

bash
docker exec my-container cat /etc/hostname > host-hostname.txt

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:

bash
docker exec my-container sh -c 'grep error /app/log.txt >> /tmp/errors.txt'

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 > after docker exec writes to a file inside the container.
  • Forgetting -i when piping or redirecting input into a command inside the container.
  • Omitting sh -c when the command needs shell features inside the container.
  • Losing track of which shell expands variables and redirection operators.
  • Using redirection tricks when docker cp would be simpler and clearer.

Summary

  • 'docker exec container command > file writes to a host file, not a container file.'
  • To redirect inside the container, run a shell there with sh -c.
  • Use -i when 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.

Course
Beginner
27 lessons
10 hours
System Design Fundamentals

Build a strong foundation in designing scalable, reliable distributed systems.

View the course
System Design practice on Codemia

Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.

Practice system design