0

I'm creating a Dockerfile that needs to execute a command, let's call it foo

In order to execute foo, I need to create a .cfc in current directory with token information to call this foo service.

So basically I should do something like

ENV FOO_TOKEN token
ENV FOO_HOST host
ENV FOO_SHARED_DIRECTORY directory
ENV LIBS_TARGET target

and then put the first three variables in a .cfg file and then launch a command using the last variable as target.

Given that if run more than one CMD in a Dockerfile, only the last one will be considered, how should I do that?

My ideal execution is docker run -e "FOO_TOKEN=aaaaaaa" -e "FOO_HOST=myhost" -e "FOO_SHARED_DIRECTORY=Shared" -e "LIBS_TARGET=target/scala-2.11/*.jar" -it --rm --name my-ci-deploy foo/foo:latest

dierre
  • 7,140
  • 12
  • 75
  • 120
  • You could make your `target` command a shell script that does what you want maybe. That is, that script could create your .cfg (https://stackoverflow.com/questions/11618696/shell-write-variable-contents-to-a-file) file then call your required command. – Adi Fatol Jul 09 '19 at 08:21
  • Hello @AdiFatol, if I create my own script and then push my docker image to a registry, is this script exported in the docker image? I don't mind using a script but I would like to export it inside the image. – dierre Jul 09 '19 at 08:24
  • 1
    If you copy the script to the image, then yes. There are multiple ways to do it: `COPY` in Dockerfile while building the image (https://docs.docker.com/engine/reference/builder/#copy) or `docker cp /src container:/target`(https://stackoverflow.com/questions/22907231/copying-files-from-host-to-docker-container) and then commit the changed container (`docker commit` https://docs.docker.com/engine/reference/commandline/commit/) – Adi Fatol Jul 09 '19 at 08:44
  • I will use copy, thanks! – dierre Jul 09 '19 at 08:46

1 Answers1

0

If you wanted to keep everything in the Dockerfile (something I think is rather desirable), you can do something nasty like:

ENV SCRIPT=IyEvdXNyL2Jpbi9lbnYgYmFzaApwZG9fc3Fsc3J2PTAKc3Vkbz0KdmVuZG9yPSQoIGxzYl9yZWxlYXNlIC1p
RUN echo -n "$SCRIPT" | base64 -d | /usr/bin/env bash                                                      

Where the contents of SCRIPT= are derived by piping your shell script thusly:

cat my_script.sh | base64 --wrap=0

You may have to adjust the /usr/bin/env bash if you have a really minimal (Alpine) setup.

Orwellophile
  • 13,235
  • 3
  • 69
  • 45