Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Commands in Docker ENTRYPOINT

Tags:

docker

mpi

Is there a way to execute a command as an argument in a Dockerfile ENTRYPOINT? I am creating an image that should automatically run mpirun for the number of processors, i.e., mpirun -np $(nproc) or mpirun -np $(getconf _NPROCESSORS_ONLN).

The following line works:

ENTRYPOINT ["/tini", "--", "mpirun", "-np", "4"] # works

But I cannot get an adaptive form to work:

ENTRYPOINT ["/tini", "--", "mpirun", "-np", "$(nproc)"] # doesn't work
ENTRYPOINT ["/tini", "--", "mpirun", "-np", "$(getconf _NPROCESSORS_ONLN)"] # doesn't work

Using the backtick `nproc` notation does not work either. Nor can I pass an environment variable to the command.

ENV processors 4
ENTRYPOINT ["/tini", "--", "mpirun", "-np", "$processors"] # doesn't work

Has anyone managed to get this kind of workflow?

like image 699
Gilly Avatar asked May 31 '26 18:05

Gilly


2 Answers

Those likely won't work: see issue 4783

ENTRYPOINT and CMD are special, as they get started without a shell (so you can choose your own) and iirc they are escaped too.

Unlike the shell form, the exec form does not invoke a command shell.
This means that normal shell processing does not happen.

For example, ENTRYPOINT [ "echo", "$HOME" ] will not do variable substitution on $HOME.
If you want shell processing then either use the shell form or execute a shell directly, for example: ENTRYPOINT [ "sh", "-c", "echo", "$HOME" ].

A workaround would be to use a script.

COPY docker-entrypoint.sh /
ENTRYPOINT ["/docker-entrypoint.sh"]

That script, when docker run triggers it, should at least benefit from the environment variable.

See for example the Dockerfile of vromero/activemq-artemis-docker, which runs the script docker-entrypoint.sh.
In order to allow CMD to run as well, the scripts end with:

exec "$@"

(It will execute whatever parameter comes after, either from the CMD directive, or from docker run parameters)


The OP Gilly adds in the comments:

I use in the Dockerfile:

COPY docker-entrypoint.sh
ENTRYPOINT ["/tini", "--", "/docker-entrypoint.sh"] 

And in the entrypoint script:

#!/bin/bash
exec mpirun -np $(nproc) "$@"
like image 81
VonC Avatar answered Jun 02 '26 21:06

VonC


It is because you are using the exec form for your entry point and variable substitution will not happen in the exec form.

This is the exec form:

ENTRYPOINT ["executable", "param1", "param2"]

this is the shell form:

ENTRYPOINT command param1 param2

From the official documentation:

Unlike the shell form, the exec form does not invoke a command shell. This means that normal shell processing does not happen. For example, ENTRYPOINT [ "echo", "$HOME" ] will not do variable substitution on $HOME

like image 21
Aurélien Bottazini Avatar answered Jun 02 '26 19:06

Aurélien Bottazini



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!