gpt4 book ai didi

CMD 中的 Docker ENV

转载 作者:行者123 更新时间:2023-12-01 23:16:10 25 4
gpt4 key购买 nike

我正在尝试执行以下操作:

  • 将构建参数传递给我的 docker 构建命令
  • 将其作为环境变量存储在我的容器中
  • 在我的 CMD 中使用它以在我的容器启动时启动。

  • 下面是我的设置:
    FROM ubuntu:xenial

    ARG EXECUTABLE

    ENV EXECUTABLE ${EXECUTABLE}


    CMD ["/opt/foo/bin/${EXECUTABLE}", "-bar"]

    这是我构建容器的方式
        docker build --build-arg EXECUTABLE=$EXECUTABLE  -t test_image .

    这是我运行图像的方式
        docker run -d test_image

    当我运行容器时它崩溃并告诉我
    docker: Error response from daemon: OCI runtime create failed: 
    container_linux.go:296: starting container process caused
    "exec: \"/opt/foo/bin/${EXECUTABLE}\": stat /opt/foo/bin/${EXECUTABLE}:
    no such file or directory": unknown.

    最佳答案

    要使用环境变量,您需要使用 shell。
    https://docs.docker.com/engine/reference/builder/#cmd

    Note: Unlike the shell form, the exec form does not invoke a command shell. This means that normal shell processing does not happen. For example, CMD [ "echo", "$HOME" ] will not do variable substitution on $HOME. If you want shell processing then either use the shell form or execute a shell directly, for example: CMD [ "sh", "-c", "echo $HOME" ]. When using the exec form and executing a shell directly, as in the case for the shell form, it is the shell that is doing the environment variable expansion, not docker.



    基于此,我认为您可以通过以下 Dockerfile 正常工作.
    FROM ubuntu:xenial

    ARG EXECUTABLE

    ENV EXECUTABLE ${EXECUTABLE}

    CMD [ "sh", "-c", "/opt/foo/bin/${EXECUTABLE}", "-bar"]

    关于CMD 中的 Docker ENV,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52789177/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com