-1

I am trying to run a few different processes in a bash script. I am using a Mac. Basically, I am running a CFD software called OpenFOAM that runs in a docker container, which needs to be launched before I can start running the simulation. I am following their instructions at https://openfoam.org/download/7-macos/. I have X11 installed.

So far, my script works up to the part where the container is launched, but it does not execute the command 'simpleFoam' inside the container, which runs the simulation. Basically, it launches the container and stops.

How can run a command inside of the OpenFoam container after it is launched? This is my code so far:

#!/bin/bash 
cd ~/openfoam/run/airFoil2D
openfoam7-macos 
simpleFoam 

It basically works up to openfoam7-macos (which launches the container) but nothing is executed after that.

The openfoam7-macos script appears to be running docker under the hood. How can I pass a command to execute into that?

user3611
  • 151
  • 2
  • 9
  • It's not clear where the container fits into the picture or where `xterm` runs. Do you have a container which runs X11? Is `simpleFoam` a valid installed command in that container? Do you mean a Docker container, or something else? – tripleee Jun 29 '21 at 04:43
  • As an aside, `cd; cd somewhere` can be simplified to `cd ~/somewhere` – tripleee Jun 29 '21 at 04:43
  • You're right - I realized I don't need to use xterm specifically, but any terminal. I am running docker on my desktop, and when I cd into the openfoam/run/airFoil2D folder, I launch the OpenFOAM docker image by using 'openfoam7-macos'. It's better explained here, in "Step 6" https://openfoam.org/download/7-macos/ – user3611 Jun 29 '21 at 04:51
  • running 'openfoam7-macos' provides a bash terminal with OpenFOAM – user3611 Jun 29 '21 at 04:53
  • Running multiple commands with `&&` in a terminal will execute one command until it finishes before moving on to the next. In this case finishes means once you have exited from the bash terminal that opens up. You need a different approach here. `docker exec` can execute commands inside a running container for example. – super Jun 29 '21 at 04:59
  • I updated your question to the best of my ability to include information from the comments here, but I had to guess a few things -- please review. In particular, is it true that you have Xquartz up and running? It would still be helpful to indicate what _exactly_ you hope to accomplish in terms of whether the container should continue to run after your command finishes etc, and perhaps explain why you `cd` (if the container is self-contained, that should not be necessary; or do you hope to change directories inside the container?) – tripleee Jun 29 '21 at 05:10
  • I made some modifications to my script after realizing xterm isn't really relevant to this. And yes, XQuartz is up and running. The container can exit after my command is run. What's important is that I launch it, run simpleFoam (the simulation), then exit it – user3611 Jun 29 '21 at 05:33
  • By the looks of it, the container script doesn't let you pass commands into it. What would seem to make sense is start the container, then connect to it separately with `xterm` or perhaps `ssh` and run the command. Then maybe run a separate command to shut it down if that's what you want. – tripleee Jun 29 '21 at 05:38
  • Tangentially perhaps see also https://stackoverflow.com/questions/37586811/pass-commands-as-input-to-another-command-su-ssh-sh-etc which explains why your current script doesn't work. – tripleee Jun 29 '21 at 05:39
  • At a distant second, maybe hack the script to allow you to pass in a command after the container name. Where you have `docker run --parameters --more parameters image` you want `docker run --parameters --more parameters image command` – tripleee Jun 29 '21 at 05:40
  • unfortunately none of these are working. I guess it's not possible to enter into a docker container and run commands inside from an outside bash script. How strange. – user3611 Jun 29 '21 at 15:01
  • Dunno exactly what you tried. I can add an answer just to show the code, and then delete it if it doesn't work for you. – tripleee Jun 29 '21 at 15:12
  • sounds good! I tried adding the command to the openfoam7-macos script as your comment suggested, and I tried a bunch of other things with docker run, no luck so far, it doesn't seem to execute anything after entering the container – user3611 Jun 29 '21 at 15:34

1 Answers1

1

Try copying the script to a private location - say, openfoam7-exec, and replacing the Docker command near the end of the script with

docker run -it \
    --rm \
    -e DISPLAY=$IP:0 \
    -u $USER_ID:$GROUP_ID \
    -v /tmp/.X11-unix:/tmp/.X11-unix \
    -v $MOUNT_DIR:$HOME_DIR \
    $DOCKER_OPTIONS \
    $DOCKER_IMAGE "$@"

You'll also need to replace the usage command in the case statement near the beginning of the script with a break statement, to allow arguments to be passed through to Docker. (This probably breaks the script's option parsing, so if you need the options to work, this will perhaps require further refinement.)

With this change, adding an argument to the script should cause Docker to not just start the container in interactive mode, but run that argument as a command in the container and then shut down the container when that command terminates.

So then, if you saved the modified script in you PATH as openfoam7-exec and made it executable, your script should hopefolly work with a simple modification:

#!/bin/bash
openfoam7-exec simpleFoam

(I'm still not convinced the cd is useful, let alone necessary; obviously, if it is, put it back.)

tripleee
  • 175,061
  • 34
  • 275
  • 318
  • Thank you so much for this answer. Like you predicted, I think replacing usage with case in this line: while [ "$#" -gt 0 ] do case "$1" in -d | -dir) [ "$#" -ge 2 ] || break "'$1' option requires an argument" caused an issue with options, as it gave me this error: Invalid option 'simpleFoam' Usage: openfoam7-macos [OPTIONS] – user3611 Jun 29 '21 at 23:51
  • Sounds like my instructions were not clear enough. Here is a Gist with the required changes (slightly expanded to still reject invalid _options_ but permit arguments): https://gist.github.com/tripleee/9200f7274905f63b6748c0d7dbcf7daa – tripleee Jun 30 '21 at 03:53