TL;DR
There are two ways that could work in your case.
- You can run one-liner-script using
docker exec sh/bash
with -c
argument:
docker exec -i <your_container_id> sh -c 'sh-command-1 && sh-command-2 && sh-command-n'
- You can copy shell script into container using
docker cp
and then run it in docker context:
docker cp ~/your-shell-script.sh <your_container_id>:/tmp
docker exec -i <your_container_id> /tmp/your-shell-script.sh
Precaution
Not all containers allow to run shell scripts in their context. You can check it executing any shell command in docker:
docker exec -i <your_container_id> echo "Shell works"
For future reference check section Understand how CMD and ENTRYPOINT interact

Docker Exec One-liner
docker exec -i <your_container_id> sh -c 'sh-command-1 && sh-command-2 && sh-command-n'
If your container has sh
or bash
or BusyBox shell wrapper (such as alpine, you can send one-line shell script to container's shell.
Limitations:
- only short scripts;
- hard to pass command-line arguments;
- only if your container has shell.
Docker Copy and Execute Script
docker cp ~/your-shell-script.sh <your_container_id>:/tmp
docker exec -i <your_container_id> /tmp/your-shell-script.sh -arg1 -arg2
You can copy script from host to container and then execute it.
You can pass arguments to the script.
You can run script with root credentials with -u root
: docker exec -i -u root <your_container_id> /tmp/your-shell-script.sh -arg1 -arg2
You can run script interactively with -t
: docker exec -it <your_container_id> /tmp/your-shell-script.sh -arg1 -arg2
Limitations:
- one more command to execute;
- only if your container has shell.