Bind path in singularity using container environment variable - singularity-container

I have a singularity container built with a scientific filesystem app.
I run the container with singularity run --app myapp image.sif and, accordingly, I have the environment variable SCIF_APPDATA=/scif/data/myapp set inside the container.
I'd like to bind a path using this environment variable. Something like:
singularity run -B /my/path/on/host/:$SCIF_APPDATA/input/
Unfortunately this does not work. I didn't manage to make singularity use as a mount path the environment variable with its "internal" value.
I have to explicitly pass the value of the environment variable:
singularity run -B /my/path/on/host/:/scif/data/myapp/input
Does anybody know how to use container environment variables in bind paths?

I don't think it is possible to directly use environment variables from inside the container in the bind statement. However, you can do it in two steps, first a call to singularity exec to get the value of the variable, then use it with singularity run:
SCIF_APPDATA=$(singularity exec ascisoftApp.sif bash -c "echo \$SCIF_APPDATA")
singularity run -B /my/path/on/host/:$SCIF_APPDATA/input/ ...
Don't forget the backslash to escape the $ in the echo command.
Note: Not sure why but it doesn't work for me when I directly use echo, hence the wrapping with bash -c.

Related

Variable substitution is not happening inside Containerfile

Building a docker container
Using alpine as build image
The Containerfile receives VERSION as argument
It is set as image version
The same version is passed to dotnet publish command.
The passed version (which is a variable) in current context never gets substituted. Instead of substituted variable ${VARIABLE} is passed to dotnet publish command.
ARG VERSION
FROM mcr.microsoft.com/dotnet/sdk:6.0-alpine as build
ARG VERSION
RUN echo ${VERSION}
RUN echo ${VERSION} > image_version
WORKDIR /app
COPY . .
RUN dotnet restore
# TODO Pass the version as build command below.
RUN dotnet publish --version-suffix ${VERSION} --output /app/published-app
I tried different ways to do the variable substitution like below:
"$VERSION"
"$(VERSION)"
$VERSION ..... and many other ways I found in the internet. None of them worked. The argument passed is exactly what it is before the variable is substituted.
Thoughts on what may be causing this?

Variable Substitution with Bamboo in Dockerfile

My Dockerfile looks like the following:
from httpd:${bamboo.test.tag}
COPY index.html /usr/local/apache2/htdocs/
In Bamboo I have a task with the following script:
docker build --no-cache -t myproj/my .
When running the job, I get the following error:
build 26-Sep-2022 10:42:26 Step 1/2 : from httpd:${bamboo.test.tag}
error 26-Sep-2022 10:42:26 failed to process "httpd:${bamboo.test.tag}": missing ':' in substitution
How can I substitute the tag?
This is actually a problem with how you are using the dockerfile.
Docker will not expand environment variables inside your Dockerfile. You need to pass the environment value as a build argument in the docker build command then use the ARG keyword inside the Dockerfile.
Your Dockerfile would look like this:
ARG IMAGE_TAG
from httpd:${IMAGE_TAG}
COPY index.html /usr/local/apache2/htdocs/
And you would need to change you docker build command to:
docker build --no-cache --build-arg IMAGE_TAG=${bamboo.test.tag} -t myproj/my .
Check a more detailed explanation here

Access environment variable in a korn shell script

my dev/qa/uat/prod servers each have an environmental variable "DATADIR".
For example in a dev linux server:
echo $DATADIR
devl
Can I access that environment variable inside
a korn shell script?
I figured it out after reading more about variables.
System variables are available inside the script.
echo $SOMEPATH
/usr/bin/
path=$SOMEPATH
echo The path you want is $path
The path you want is /usr/bin

Environment variables for build not visible in Dockerfile

I've set an environment variable (NPM_TOKEN) for my repo in Docker Cloud to use when building my Dockerfile. However, the variable is always empty...
Tried both of these in Dockerfile:
RUN echo ${NPM_TOKEN}
and:
ARG NPM_TOKEN
RUN echo ${NPM_TOKEN}
Am I wrong in assuming that Docker Clouds environment variables for build does the same thing as --build-arg?
It took me along time, but you can use build hooks to set variables for automated builds!
https://docs.docker.com/docker-cloud/builds/advanced/#build-hook-examples

How can i set a local variable in ssh?

I would like to set a local variable in a ssh command-chain that is only used in this environment:
#!/bin/sh
my_var='/tmp/wrong_file'
ssh user#server "my_var='/tmp/a_file'; cat $my_var;my_var=123;echo $my_var"
echo $my_var
This example the "outer" $my_var is used. How to fix this and use variables "in" the current ssh connection as locally defined? There is no need to change or access the external value '/tmp/wrong_file' in $my_var, as asked in Assign directory listing to variable in bash script over ssh.
You're using the wrong quotes. Parameter expansion is performed inside double quotes, but not inside single quotes.
#!/bin/sh
my_var=/tmp/wrong_file
ssh user#server 'my_var=/tmp/a_file; cat $my_var;my_var=123;echo $my_var'
First of all: The SSH shell and your local shell are completely different and do not exchange any environment variables. This is a good thing - consider environment variables such as LD_LIBRARY_PATH when using SSH between machines of different OS architecture.
IMHO the best solution for your problem is to encapsulate your commands into a shell script on the remote side, then maybe start it with parameters. E.g.:
Remote:
myscript.sh contains:
#!/bin/sh
MY_FILE="$1";
echo "Contents of §MY_FILE:"
cat $MY_FILE
Local:
RUn something like
export REMOTE_FILE='/path/to/it'
ssh user#server "/path/to/myscript.sh '$REMOTE_FILE'"