docker
shell-script
containerization
devops
automation

How to run shell script on host from docker 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

Understanding Docker: An Overview

Docker is an open-source platform that automates the deployment of applications inside lightweight, portable containers. Containers have become an essential tool in today's software development landscape due to their ability to isolate applications, ensuring consistency across different environments.

When working with Docker, you might encounter scenarios where running a shell script on the Docker host from within a container is necessary. While Docker encourages isolation, there are instances where interacting with the host system is needed, such as triggering host-level operations, managing files, or accessing services only available on the host.

This guide walks you through the process of executing a shell script on a host machine from a Docker container.

Prerequisites

Before diving into the execution process, ensure you have:

  • Docker installed on your host machine.
  • Basic knowledge of Docker and container usage.
  • A shell script you intend to execute on the host.

Conceptual Approach

By design, Docker containers are isolated from the host machine. To run a script on the host from a container, you need to parlay a method that allows for controlled interaction with the host environment. Here are some common approaches:

  • Bind Mounts or Volumes: Share files between the host and container.
  • SSH Setup: Use SSH to execute commands on the host from within the container.
  • Docker API: Leverage Docker's API or Docker sockets to communicate with the host.

Method 1: Using Bind Mounts or Volumes

In this approach, you utilize Docker's ability to mount directories from the host onto the container so that you can execute scripts on the host.

  1. Set Up a Bind Mount:
    Create a bind mount specifying a directory on your host and the directory it should map to in the container.
bash
   docker run -v /host/directory:/container/directory myimage
  1. Host Interaction Script:
    Within your containerized application, write to the bind-mounted directory. The host machine can have a monitoring script that executes scripts based on file-system changes within this directory.
    Example (assuming 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
  1. Trigger the Action from Container:
    Your container writes a file to the shared directory:
bash
   touch /container/directory/run-script.indicator

Method 2: Using SSH

SSH can also be utilized to execute scripts on the host machine from a Docker container:

  1. SSH Server on Host:
    Ensure the host machine is running an SSH server and your container has an SSH client installed.
  2. Authentication Setup:
    Establish SSH keys for passwordless login and copy them to the host:
    • Generate keys inside the container:
bash
      ssh-keygen -t rsa -b 2048
      ssh-copy-id user@host
  1. Script Execution:
    Use SSH to run the script:
bash
    ssh user@host './path/to/my-host-script.sh'

Method 3: Using Docker API

By leveraging Docker's API, especially the Docker socket, you can also communicate with the Docker host:

  1. Access the Docker Socket:
    Bind the Docker socket of the host into the container:
bash
    docker run -v /var/run/docker.sock:/var/run/docker.sock myimage
  1. Command Execution via API:
    Use Docker's API to execute commands. This method requires understanding Docker API's capabilities and performing operations such as creating temporary containers that execute scripts.

Risks and Precautions

  • Security: Running scripts on the host can open vulnerabilities. Ensure proper authentication and limit access as needed.
  • Isolation Violation: These methods can break the container’s isolation, so they're often not recommended for production environments.

Summary Table

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

Conclusion

Executing a host-based script from within a Docker container involves breaching traditional container isolation. While several methods exist, each comes with its own set of trade-offs regarding complexity, security, and maintainability. Ensuring clear boundaries and safeguarding each method with proper security practices is paramount to prevent potential breaches. Understanding these interactions deepens your overall grasp of Docker's architecture and prepares you for complex real-world scenarios.


Related reading
Course
Beginner
27 lessons
10 hours
System Design Fundamentals

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

View the course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

System Design practice on Codemia

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

Practice system design

All Rights Reserved.