How to unset “ENV” in dockerfile?

后端 未结 5 431
佛祖请我去吃肉
佛祖请我去吃肉 2021-01-07 22:03

For some certain reasons, I have to set \"http_proxy\" and \"https_proxy\" ENV in my dockerfile. I would like to now unset them because there are also some buil

5条回答
  •  温柔的废话
    2021-01-07 22:45

    If one needs env vars during the image build but they should not persist, just clear them. In the following example, the running container shows empty env vars.

    Dockerfile

    # set proxy
    ARG http_proxy
    ARG https_proxy
    ARG no_proxy
    ENV http_proxy=$http_proxy
    ENV https_proxy=$http_proxy
    ENV no_proxy=$no_proxy
    
    # ... do stuff that needs the proxy during the build, like apt-get, curl, et al.
    
    # unset proxy
    ENV http_proxy=
    ENV https_proxy=
    ENV no_proxy=
    

    build.sh

    docker build -t the-image \
        --build-arg http_proxy="$http_proxy" \
        --build-arg https_proxy="$http_proxy" \
        --build-arg no_proxy="$no_proxy" \
        --no-cache \
        .
    

    run.sh

    docker run --rm -i \
        the-image \
        sh << COMMANDS
            env
    COMMANDS
    

    Output

    no_proxy=
    https_proxy=
    http_proxy=
    ...
    

提交回复
热议问题