docker
shell-script
containerization
devops
automation

How to run a shell script on the host from a Docker container

Codemia Knowledge Hub ·

A container cannot run a script on its host directly, because Docker isolates containers from the host by design. To trigger a host script you need a controlled channel: a bind-mounted directory that a host-side watcher monitors, an SSH connection from the container to the host, or the Docker socket mounted into the container. Each one weakens isolation, so use them deliberately.

You might need this to trigger host-level operations, manage host files, or reach services that exist only on the host. You need Docker on the host, basic familiarity with containers, and the script you want to run.

Method 1: a bind mount and a watcher on the host

Mount a host directory into the container. The container writes a trigger file there, and a script on the host watches the directory and runs your script when the file appears.

Set up the bind mount, mapping a host directory to a container directory:

bash
   docker run -v /host/directory:/container/directory myimage

On the host, run a watcher on that directory. This example assumes inotifywait is available on the host:

bash
1   # Host Script monitoring /host/directory
2   inotifywait -m /host/directory -e create |
3   while read path action file; do
4     if [ "$file" == "run-script.indicator" ]; then
5       ./my-script.sh
6     fi
7   done

From the container, trigger it by writing the file to the shared directory:

bash
   touch /container/directory/run-script.indicator

Method 2: SSH from the container to the host

The host must run an SSH server and the container needs an SSH client. Set up keys for passwordless login by generating them inside the container and copying them to the host:

bash
      ssh-keygen -t rsa -b 2048
      ssh-copy-id user@host

Then run the script over SSH:

bash
    ssh user@host './path/to/my-host-script.sh'

Method 3: the Docker socket

Mounting the host's Docker socket lets the container talk to the Docker daemon on the host:

bash
    docker run -v /var/run/docker.sock:/var/run/docker.sock myimage

The container can then use the Docker API to run commands, for example by creating temporary containers that execute scripts. This requires understanding what the Docker API can do.

Choosing a method

MethodDescriptionUse Case
Bind MountsShares directories between host & container for indirect executionFiles available on both sides
SSHEnables command execution over secure shellHost has SSH and passwordless login enabled
Docker APIUtilizes Docker's exposed API to control host operationsDocker-level command execution via API

Security risks

  • Security: running scripts on the host from a container can open vulnerabilities. Use proper authentication and limit access.
  • Broken isolation: all of these methods weaken the container's isolation, which is why they are often not recommended in production.

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