This quite possible. The problem is that your trying to execute the command directly. Instead what you need to do is, create a entrypoint.sh
and let it execute. That script can determine what it needs to do based on environment variables.
Consider the below Dockerfile
FROM centos:7.2.1511
COPY entrypoint.sh /entrypoint.sh
ENTRYPOINT ["/entrypoint.sh"]
CMD ["this","is", "tarun"]
and content of the entrypoint.sh
are as below
#!/usr/bin/env sh
_load() {
CMD=""
if [ ! -z "$ENTRYPOINT" ]; then
CMD=$ENTRYPOINT
fi
if [ "$#" -gt 0 ]; then
CMD="$CMD $@"
elif [ ! -z $COMMAND ]; then
CMD="$CMD $COMMAND"
fi
echo "Executing - $CMD"
exec $CMD
}
_load $@
The script does nothing but check if a environment variable $ENTRYPOINT
or $COMMAND
was passed, or both were passed, or command from docker run was passed. Based on below priority order it runs the command
- ENTRYPOINT + RUN PARAMETERS - Combine both and execute
- ENTRYPOINT + COMMAND - Combine both and execute
- MISSING $COMMAND - Run $ENTRYPOINT
- MISSING $ENTRYPOINT - Run $COMMAND
Now to test it
$ docker run -e ENTRYPOINT=/bin/echo test my name "is XYZ"
Executing - /bin/echo my name is XYZ
my name is XYZ
The script may not be 100% perfect, but I just created it to demonstrate the idea
If you need a simple entrypoint.sh
then you can even replace it with below content
exec $ENTRYPOINT $@
This also would work.