0

I want to execute some code inside a docker container. To do this, I execute this script:

#!/bin/bash
docker start mycontainer
docker exec mycontainer python hello.py
docker exec mycontainer cd modifiedDiffusion
docker exec mycontainer python hello.py
docker exec mycontainer sh executeModifiedDiffusion.sh
docker stop mycontainer

I created a simple print('hello world') type script in the first directory in which the container puts you and a second script in the directory modifiedDiffusion.

The command cd modifiedDiffusion works, because I tried some tests with the ls command.

The first script runs, but the problem is that the second python script doesn't run. How do I solve this?

Alessandro
  • 103
  • 3
  • 11
  • 1
    I wouldn't use `docker exec` like this. This is the analog of starting a Python REPL and then manually calling functions inside of that, rather than just running a program. For actually debugging things like your container's filesystem layout it can be useful, but it shouldn't be the normal way you run your program. Can you use a non-Docker virtual environment for this kind of development instead? – David Maze Jun 22 '22 at 10:32
  • Unfortunately I can't. In the lab we have to use docker in order to queue jobs on the GPU, and it's useful for other stuff too. I have some difficulties debugging it, since it doesn't have a GUI. I'll look into the things you mentioned. – Alessandro Jun 22 '22 at 12:16

1 Answers1

1

Try this:

#!/bin/bash
docker exec 6b bash -c "python hello.py; cd modifiedDiffusion; python hello.py; sh executeModifiedDiffusion.sh"

Ref: How to run 2 commands with docker exec

The above solution is recommended only for a few commands. As the commands increase, we must create a separate bash script, add all those commands inside it and then run the bash script inside the Docker container. This provide more flexibility.

Kapil Khandelwal
  • 1,096
  • 12
  • 19
  • `hello.py` runs! What if instead of `python hello.py` I want to execute `python run_cifar.py train --log_dir_ '/logs' --exp_name 'exp1'`? Because just copying it inside the line in place of `hello.py` doesn't work. – Alessandro Jun 22 '22 at 07:51
  • Please share the complete command that you are trying to run and the error message you are getting. I have updated the answer as well to handle complex use cases. – Kapil Khandelwal Jun 22 '22 at 08:02
  • I'm running that shell script in a queue system inside a remote workstation. I'm not really getting an error, the code just stops executing and then it finishes. The complete command I'm trying to run is `python scripts/run_cifar.py train --log_dir_ '/logs' --exp_name 'exp1'`. It works if I run it from inside the container, from the right folder. – Alessandro Jun 22 '22 at 08:35