Created
February 6, 2020 23:58
-
-
Save wcarhart/6271dec3eb081a74314699e2eb304b5d to your computer and use it in GitHub Desktop.
Run a Matlab script in a Docker container without rebuilding the image when the script updates (could be extended to any script)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
FROM ubuntu:18.04 | |
RUN apt-get update | |
RUN apt-get install -y octave | |
WORKDIR / | |
ENV SCRIPT="" | |
COPY octave_runner / | |
RUN chmod +x /octave_runner | |
CMD /octave_runner -d $SCRIPT |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
set -o errexit -o nounset -o pipefail | |
function main { | |
parse_args "$@" | |
if [[ $docker -eq 1 ]] ; then | |
run_octave | |
else | |
setup_container | |
fi | |
} | |
# setup Octave container for specified Matlab script | |
function setup_container { | |
docker build -t octave . | |
docker run -it -v `greadlink -m "$script"`:/"$script":ro -e SCRIPT="$script" octave | |
} | |
# run Octave within the Docker container | |
function run_octave { | |
octave "$script" --traditional | |
} | |
# print program usage | |
function usage { | |
cat << EndOfUsage | |
Run a Matlab script in a Docker container via Octave | |
Usage: | |
octave_runner [-h] [-d] SCRIPT | |
Required arguments: | |
SCRIPT path to Matlab script to run | |
Optional arguments: | |
-h, --help show this menu and exit | |
-d, --docker spin up an Octave container and execute SCRIPT in it | |
EndOfUsage | |
} | |
# parse command line arguments | |
function parse_args { | |
if [[ $# -eq 0 ]] ; then | |
usage | |
exit 1 | |
fi | |
script= | |
docker=0 | |
while [[ $# -gt 0 ]] ; do | |
key="$1" | |
case "$key" in | |
-d|--docker ) docker=1 ; shift ;; | |
-h|--help ) usage ; exit 0 ;; | |
* ) | |
if [[ "$key" == "-"* ]] ; then | |
>&2 echo "-err: unrecognized option '$key'" | |
exit 1 | |
fi | |
script="$1" ; shift | |
;; | |
esac | |
done | |
if [[ "$script" == "" ]] ; then | |
>&2 echo "-err: no script provided" | |
exit 1 | |
fi | |
if [[ ! -f "$script" ]] ; then | |
>&2 echo "-err: no such file '$script'" | |
exit 1 | |
fi | |
} | |
# ======================================= # | |
if declare -F -- "${1:-}" >/dev/null ; then | |
"$@" | |
else | |
main "$@" | |
fi |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
my_var = 3; disp(my_var) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment