简体   繁体   中英

Add fixed argument to command arguments in docker entrypoint

I want to add a fixed parameter to a command in a docker entrypoint st the command is executed like so:

command user_arg1 fixed_arg user_arg2

Using a linux terminal as an example, instead of running:

echo arg1 arg2

I want to run:

<some set of commands> arg1 arg2

such that the final command that's run is:

echo arg1 fixed_arg arg2

Is there a way to use basic command line tools to achieve this instead of writing a bash script?

If you want to include the fixed argument in the middle of the command, you have to use a script. You don't need bash extensions, though, and you can make this work even with the basic POSIX shell in BusyBox or Alpine Linux.

#!/bin/sh
first="$1" # save the first option
shift      # remove it from the options list
           # now $@ is all the options except the first one
exec the_command "$first" some_arg "$@"

In the context of a Docker entrypoint, it will get pass the entire CMD (or docker run command part or Compose command: ) as options, so you could make this conditional on what the command actually is.

#!/bin/sh
case "$1" in
  the_command)
    first="$2"
    shift 2
    exec the_command "$first" some_arg "$@"
    ;;
  *)
    exec "$@"
    ;;
esac

The only thing Docker will do natively is combine the ENTRYPOINT and CMD into a single command ; there's no way to rearrange the options or otherwise manipulate them. A pattern I find useful is for the CMD to be the actual command you want to run, and the ENTRYPOINT to be a script that eventually runs it. That can manipulate the command string however you want, including choosing to completely ignore it or change the actual command that gets run.

The RUN command in a dockerfile should work, an example is,

RUN /bin/bash -c 'source $HOME/.bashrc; \
echo $HOME'

More information in the docker documentation, https://docs.docker.com/engine/reference/builder/#run

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM