Home > Software design >  How to use env variables set from build phase in run. (Docker)
How to use env variables set from build phase in run. (Docker)

Time:11-18

I want to preface this in saying that I am very new to docker and have just got my feet wet with using it. In my Docker file that I run to build the container I install a program that sets some env variables. Here is my Docker file for context.

FROM python:3.8-slim-buster
COPY . /app
RUN apt-get update
RUN apt-get install wget -y
RUN wget http://static.matrix-vision.com/mvIMPACT_Acquire/2.40.0/install_mvGenTL_Acquire.sh
RUN wget http://static.matrix-vision.com/mvIMPACT_Acquire/2.40.0/mvGenTL_Acquire-x86_64_ABI2-2.40.0.tgz
RUN chmod  x ./install_mvGenTL_Acquire.sh
RUN ./install_mvGenTL_Acquire.sh -u
RUN apt-get install -y python3-opencv
RUN pip3 install USSCameraTools 
WORKDIR /app
CMD python3 main.py

After executing the build docker command the program "mvGenTL_Acquire.sh" sets env inside the container. I need these variables to be set when executing the run docker command. But when checking the env variables after running the image it is not set. I know I can pass them in directly but would like to use the ones that are set from the install in the build.

Any help would be greatly appreciated, thanks!

CodePudding user response:

For running a bash script when your container is creating:

make an script.sh file:

#!/bin/bash
your commands here

If you are using an alpine image, you must use #!/bin/sh instead of #!/bin/bash on the first line of your bash file.

Now in your Dockerfile copy your bash file in the container and use the ENTRYPOINT instruction for running this file when the container is creating:

.
.
.

COPY script.sh /
RUN chmod  x /script.sh
.
.
.
ENTRYPOINT ["/script.sh"]

Notice that in the ENTRYPOINT instruction use your bash file address in your image.

Now when you create a container, the script.sh file will be executed.

  • Related