Home > database >  Run Shell script in docker using Environment variable
Run Shell script in docker using Environment variable

Time:06-16

I am running a docker file to execute a debezium service as entrypoint.

It works fine and executes debezium service when I use the exact file path as parameter to Entrypoint command in my docker file

command in docker file

ENTRYPOINT ["kafka_2.12-2.6.2/bin/connect-standalone.sh","kafka_2.12-2.6.2/config/connect-standalone.properties","kafka_2.12-2.6.2/worker.properties"]

But it fails when I pass the file names as environment variable

command in docker file

ENTRYPOINT [${connect_standalone},${connect_standalone_properties},${worker_properties}]

docker run command

sudo docker run -it -e connect_standalone=kafka_2.12-2.6.2/bin/connect-standalone.sh -e connect_standalone_properties=kafka_2.12-2.6.2/config/connect-standalone.properties -e worker_properties=kafka_2.12-2.6.2/worker.properties --name cloud1 cloud9-image:latest

output of the docker run command

/bin/sh: [kafka_2.12-2.6.2/bin/connect-standalone.sh,kafka_2.12-2.6.2/config/connect-standalone.properties,kafka_2.12-2.6.2/worker.properties]: No such file or directory

Somehow it always goes to /bin/sh even though I have set working directory as root of my container where the kafka_2.12-2.6.2 directory is present

CodePudding user response:

Replacing environment variables in a command is the job of the shell. When you use the exec form of the ENTRYPOINT statement, there is no shell to replace them, so it doesn't work.

You need to use the shell form which starts a shell to run the command. Then then shell will replace the variables and it should work.

So instead of

ENTRYPOINT [${connect_standalone},${connect_standalone_properties},${worker_properties}]

do

ENTRYPOINT ${connect_standalone} ${connect_standalone_properties} ${worker_properties}

More info here: https://docs.docker.com/engine/reference/builder/#entrypoint

CodePudding user response:

May I suggest an alternative?

Your executable and the worker properties don't really need to change, so there's no reason to use environment variables here.

If you want to change worker properties using environment variables, then that's what existing Connect containers can do...

WORKDIR "kafka_2.12-2.6.2" 
ENTRYPOINT ["bin/connect-standalone.sh", "config/connect-standalone.properties"]

Now, if you want the option to run any number of connectors at runtime, but default to a specific one, then that's a CMD

CMD ["connector.properties"] 

Build the image

If you want to use a different file, mount it as a Docker volume over kafka_2.12-2.6.2/connector.properties

If you want to run multiple connectors, mount a directory of property files

docker run --rm -v $HOME/connectors:/work:ro your_image /work/connector-1.properties /work/connector-2.properties

Remove the CMD if you don't want a default connector

  • Related