How to source a script with environment variables in a docker build process?

前端 未结 4 851
孤独总比滥情好
孤独总比滥情好 2020-12-06 07:04

I\'m creating an image that has a similar problem like the following docker project:

Dockerfile

FROM alpine:3.9.3

COPY ./env.sh /env.sh
RUN source         


        
4条回答
  •  孤街浪徒
    2020-12-06 07:42

    I ended up do a multistep build of the dockerfile in a bash script:

    1. Setup your Dockerfile to include everything up to the point where you need to source a file for environment variables.
    2. In the docker file, source the environment variables and echo the environment to a file.
    RUN  source $(pwd)/buildstepenv_rhel72_64.sh && source /opt/rh/devtoolset-8/enable && env | sort -u  > /tmp.env"  
    
    1. Build the image with a tag: docker build -t ${image}_dev .
    2. Run the image using the tag to get the environment variables and add them to the end of the docker file
    docker run --rm  ${image}_dev cat /tmp.env | sed 's/$/"/;s/=/="/;s/^/ENV /'  >>  logs/docker/Dockerfile.${step}
    
    1. Construct the remainder of your dockerfile.

提交回复
热议问题