0

I am using next commands to open a docker container in interactive mode and using following commans inside bash session with this container.

  docker run -v /scriptsIA:/scriptsIA -v /opt/tomcat/webapps/PokeTrainer/imgIA:/imgsIA -it dbmobilelife/docker-python-opencv-tesseract bash

cd /scriptsIA/

python

from SegmentarImagen import *

extraerNombreUsuarioNiveldeUnaFoto("/imgsIA/andres.jpg")

exit()

exit

I have tried to create a bash script as follows:

#!/bin/bash
docker run -v /scriptsIA:/scriptsIA -v /opt/tomcat/webapps/PokeTrainer/imgIA:/imgsIA -it dbmobilelife/docker-python-opencv-tesseract bash
cd /scriptsIA/
python
from SegmentarImagen import *
extraerNombreUsuarioNiveldeUnaFoto("/imgsIA/andres.jpg")
exit()
exit

However, when i execute this bash script all i get is the following error:

[root@poketrainer /]# sh scriptIA.sh docker: Error response from daemon: OCI runtime create failed: container_linux.go:344: starting container process caused "exec: \"bash\r\": executable file not found in $PATH": unknown. : No existe el fichero o el directorio scriptIA.sh: línea 4: $'python\r': no se encontró la orden scriptIA.sh: línea 5: from: no se encontró la orden scriptIA.sh: línea 6: error sintáctico cerca del elemento inesperado "/imgsIA/andres.jpg"' 'criptIA.sh: línea 6: extraerNombreUsuarioNiveldeUnaFoto("/imgsIA/andres.jpg")

How can I do the explained bash script above withouth getting errors?

WaterKnight
  • 197
  • 7
  • I would make the script part of the container. Then you don't need interactive mode and you can execute it by default when someone starts the container. – dmadic Mar 24 '19 at 20:57

1 Answers1

2

There are multiple issues with your script here:

The \r errors such as:

starting container process caused "exec: \"bash\r\": executable file not found in $PATH": unknown

And other similar errors are related: the \r indicates there is a Windows carriage return in your script - it was probably written on Windows and mounted in a VM, or your editor somehow added these characters (see this post). Linux only expects a \n and treats \ras part of your command. Try running dos2unix on your file or make sure there are no special characters.


Also, the script has several issues:

  • You are trying to run docker exec which runs a bash command which cd and runs a python script. This can be simplified a bit (see below)
  • You want to run Python, there is probably no need to run bash first, you can run python command directly
  • Given that you want to run 2 Python commands you'll require line breaks, that's possible but not very handy. It would be better to create a Python script and mount it in the image before running a simple python command.
  • It would also be better to use docker exec -w flag instead of using cd command to set working directory
  • There is no need for exit nor exit() as it will be done implicitly once there are no more instructions to be executed

Given all that, you can:

  • run a single command such as

    docker exec [...] -it -w /scriptsIA dbmobilelife/docker-python-opencv-tesseract \
         echo -e "from SegmentarImagen import *\nextraerNombreUsuarioNiveldeUnaFoto("/imgsIA/andres.jpg")" | python
    

    in which you set working directory with -w and run a Python command by passing its content via echo and a pipe (mind the \n with no space to have a proper Python syntax)

  • creating a myscript.py script such as:

    from SegmentarImagen import *
    extraerNombreUsuarioNiveldeUnaFoto("/imgsIA/andres.jpg")
    

    then mount that script into the container and run a simple python command:

    docker exec [...] -it -w /scriptsIA -v /path/to/myscript.py:/myscript.py \
       dbmobilelife/docker-python-opencv-tesseract \
       python /myscript.py
    

Note: the [...] are for the -v /scriptsIA:/scriptsIA -v /opt/tomcat/webapps/PokeTrainer/imgIA:/imgsIA volume mount I cut out for simplification

Pierre B.
  • 11,612
  • 1
  • 37
  • 58