Openfoam Docker
Openfoam Docker
Openfoam Docker
/bin/sh
openfoamVersion="latest"
imageBasename="opencfd/openfoam"
imageFlavour="-run"
scriptVersion="2022-01-10"
#------------------------------------------------------------------------------
# ========= |
# \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
# \\ / O peration |
# \\ / A nd | www.openfoam.com
# \\/ M anipulation |
#------------------------------------------------------------------------------
# Copyright (C) 2016-2022 OpenCFD Ltd.
#------------------------------------------------------------------------------
# SPDX-License-Identifier: (GPL-3.0+)
#
# Script
# openfoam-docker
#
# Description
# Run script for openfoam container images
# (https://hub.docker.com/u/opencfd)
#
# Copy/link to automatically specify OpenFOAM version and image 'flavour'
# For example,
#
# ln -s openfoam-docker openfoam2112-run
#
#------------------------------------------------------------------------------
printHelp()
{
defaultImage="${imageBasename}${imageFlavour}:${openfoamVersion}"
cat<<HELP_HEAD
options:
-c Shell commands read from the first non-option argument
-data=DIR Specify mount dir for container '/data'
-dir=DIR Specify mount dir for container '~/' (default: pwd)
-<digits> Specify OpenFOAM version (eg, -2112)
-base | -dev | -run | -tut | -default
Image flavour (default: -run).
Not all image flavours are necessarily available.
-X | -x X11 forwarding: enable (-X) or disable (-x)
-update Update (pull) the image, do not run
-dry-run Report the start command, without running
-verbose Additional verbosity when starting (FOAM_VERBOSE)
HELP_HEAD
if [ -n "$1" ]
then
cat<<'HELP_FULL'
-entry=PATH Alternative entry point
-image=NAME | -i=NAME
Specify image to run
-docker Use docker (default)
-podman Use podman instead of docker
-sudo Prefix container calls with 'sudo'
-xhost Allow container access to host network (implies -X)
--shm-size=BYTES Size of /dev/shm (eg, --shm-size=4G)
HELP_FULL
fi
cat<<TAIL_OPTIONS
-- The end of option processing.
An argument of - or / is equivalent to --.
-h | -help Display short help and exit
-help-full Display full help and exit
Note:
The user name within the container is 'openfoam',
but matches the user/group id from the host.
Requires docker or podman.
TAIL_OPTIONS
if [ -n "$1" ]
then
cat<<HELP_FULL
Equivalent options:
| -dir=DIR | -case=DIR | -case DIR | -home=DIR
Example:
${0##*/} -dir=/path/to/simulation
or cd /path/to/simulation && ${0##*/}
Note:
The '-xhost' option can be useful in combination with 'ssh -X',
but should be used sparingly since it permits the container full
access to network communication (potentially insecure).
HELP_FULL
fi
cat<<'FOOTER'
Note:
Different OpenFOAM components and modules may be present (or missing)
on any particular container image.
For example, source code, tutorials, in-situ visualization,
paraview plugins, external linear-solver interfaces etc.
FOOTER
#------------------------------------------------------------------------------
# Constants - user name/locations MUST correspond to the image assets
toolChain=docker
container_home='/home/openfoam' # Home for container user
container_tmphome='/tmp/.home.openfoam' # Fake home for container user
#------------------------------------------------------------------------------
# Version:
if [ -n "$imageTag" ]
then
openfoamVersion="$imageTag"
fi
# Flavour:
imageTag="-${0##*-}"
case "$imageTag" in
(-base)
unset imageFlavour
;;
(-run | -default)
imageFlavour="$imageTag"
;;
(-dev*)
imageFlavour="-dev"
;;
(-tut*)
imageFlavour="-tutorials"
;;
esac
#------------------------------------------------------------------------------
# Parse options
# Image flavours
(-base) unset imageFlavour ;;
(-run) imageFlavour="-run" ;;
(-def*) imageFlavour="-default" ;;
(-dev*) imageFlavour="-dev" ;;
(-tut*) imageFlavour="-tutorials" ;;
(-docker | -podman)
toolChain="${1#*-}"
;;
(-sudo) # Use sudo
sudo="sudo"
;;
(--shm-size=*)
optShmSize="${1#*=}"
;;
(-case)
# OpenFOAM-style '-case' for specfying the HOME mount
[ "$#" -ge 2 ] || die "'$1' option requires an argument"
shift
mount1Dir="$1"
;;
(-*)
die "Invalid option '$1'"
;;
(*)
if [ -n "$optShellCommand" ]
then
break
else
die "Unexpected argument '$1'"
fi
;;
esac
shift
done
if [ -z "$image" ]
then
image="${imageBasename}${imageFlavour}:${openfoamVersion}"
fi
if [ -n "$optUpdate" ]
then
if [ -n "$optDryrun" ]
then
runPrefix="echo"
echo "(dry-run)" 1>&2
echo 1>&2
else
runPrefix="$sudo"
echo "Update image: $image" 1>&2
fi
$runPrefix ${toolChain:?} pull "$image"
exitCode="$?"
echo 1>&2
echo "Done" 1>&2
exit "$exitCode"
fi
#------------------------------------------------------------------------------
guest_uid="$(id -u 2>/dev/null)"
guest_gid="$(id -g 2>/dev/null)"
[ -n "$guest_uid" ] || die "Cannot determine current user id"
[ -n "$guest_gid" ] || die "Cannot determine current group id"
if [ -n "$DISPLAY" ] \
&& [ -n "$optX11Forwarding" ] \
&& [ -d "$mount1Dir" ] \
&& command -v xauth >/dev/null
then
# DISPLAY="host:0" vs DISPLAY=":0"
display_host="${DISPLAY%%:*}"
if [ -n "$optDryrun" ]
then
runPrefix="echo"
echo "(dry-run)" 1>&2
echo 1>&2
else
runPrefix="$sudo"
fi
if [ "$optVerbose" = true ]
then
set -x
fi
# ---------------------------------------------------------------------------