-1

I have a deadly doubt, I would like to replace an environment variable that is declared in a .sh file inside a docker image.

Any way to do this without having to mount a volume to change it?

NB. I already tried to do this through compose, and I have already guaranteed that my set variable is there, but it is overwritten by the original declaration inside the sh file.

Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
  • Does this answer your question? [How to include modified configuration files in a docker image?](https://stackoverflow.com/questions/38719427/how-to-include-modified-configuration-files-in-a-docker-image) – Marc Sances Aug 20 '20 at 17:34
  • Unfortunately, the first answer recommends using environments and the second recommends copying the file, changing it and then going back inside. I was looking for a solution where it was possible to perform the task directly through compose or through some Dockerfile. – Gabriel Cavalcante Aug 20 '20 at 17:39
  • So creating a new image with the replaced file? – Marc Sances Aug 20 '20 at 17:40
  • That's the problem, it's an application, we can't change the image, and we have to make it work locally so that we can make changes. – Gabriel Cavalcante Aug 20 '20 at 17:45
  • Is the .sh file run in the `ENTRYPOINT` or `CMD` statement of the docker file? – Shmuel Aug 20 '20 at 18:36
  • Yes it runs on the entrypoint. – Gabriel Cavalcante Aug 20 '20 at 18:41
  • Then you have to overwrite the ENTRYPOINT since anything you do will get override by the script, ENTRYPOINT is running when the container start not at build time which means that anything to you in the dockerfile will be override by the script that runs after it. – Shmuel Aug 20 '20 at 19:03
  • Can you include a [mcve] that demonstrates the specific problem you're having? Where is the environment variable defined, and what happens if you pass it with _e.g._ `docker run -e`? – David Maze Aug 21 '20 at 01:05

2 Answers2

0

I don't understand reason for such thing. ENV is gets exact value on a stage of image run. If you are trying to run multiple application with different profile pass different env file during docker run. In Dockerfile you could pass argument to you shell script:

CMD my_app.sh ${APP_ADDR} ${APP_PORT}

in my_app.sh

get variables like

APP_ADDR=$1
APP_PORT=$2

and when you running you docker image store all you variables in env file and pass it like this:

docker run --env-file=app_local.env my_app:0.1

in env file you could define you variables:

APP_ADDR=192.168.200.200
APP_PORT=5678
....
Michael Ushakov
  • 1,639
  • 1
  • 10
  • 18
  • But that would work for the scenario where: Inside the image I have a sh that already defines the variable, ex: URI=192.168.10.1:8080 And I replace it with the dockerfile? Knowing that when you start the sh script it will take the original value for which it was created? – Gabriel Cavalcante Aug 20 '20 at 17:51
  • i don't fully understand you last commentary. I think that generally you have to modify all you sh script and gets all params from cmd arguments and specify one point (env file) where you define all variables. In dockerfile you pass all arguments to you scripts. I think it will be easy to maintain you application inside image. – Michael Ushakov Aug 20 '20 at 18:03
  • I will explain better. A .sh file already exists in images, and it runs in an entrypoint statement, this file has already defined a variable value within it. I just want to override this variable. – Gabriel Cavalcante Aug 20 '20 at 18:44
  • I understand what you want. I could only suggest you to override variable via export URI=192.168.10.1:8080 from another sh script. – Michael Ushakov Aug 20 '20 at 18:50
0

You can do as suggested here

ENTRYPOINT ./base_image_entrypoint_script.sh && export URI=http://localhost

This way you override the base image ENTRYPOINT with the same script but will add the env car you wanted that will override the script variable.

Shmuel
  • 1,568
  • 10
  • 20