0

I have pulled one docker image and docker container is running successfully as well. But I want to run one shell script in the running docker. The shell script is located in my hard disk. I am unable to find out which command to use and how to give pathname of the shell file so that it can be executed in running docker.

Please guide me.

Regards

  • Possible duplicate of [How to run shell script on host from docker container?](https://stackoverflow.com/questions/32163955/how-to-run-shell-script-on-host-from-docker-container) – David Maze Jul 31 '19 at 17:00
  • As a principal design goal of Docker, you can’t. But that question has several workarounds. – David Maze Jul 31 '19 at 17:00

1 Answers1

0

TL;DR

There are two ways that could work in your case.

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

  1. 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 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.
Community
  • 1
  • 1
Yasen
  • 4,241
  • 1
  • 16
  • 25
  • Thank you very much. Copying the shell script in container and then executing it, is working for me. But my problem is one shell script is referring to another Python script. And there I am getting error as the referred Python is again not found in directory. Please guide how to handle such cases. – Manish Kumar Aug 01 '19 at 02:37
  • Hello @ManishKumar. You can pass Python script to the container too. But this trick may not work due to lack of python on your container. – Yasen Aug 01 '19 at 08:32
  • Thank you @yasen for the help. My container does have the Python. But help me to understand that, in order to run a shell script that refers to various files, do we need to copy all files being referred in the docker.? – Manish Kumar Aug 01 '19 at 17:04
  • Yes, sure. See about [docker security](https://docs.docker.com/engine/security/security/) for reference. – Yasen Aug 01 '19 at 19:44