【问题标题】:context or workdir for docker-composedocker-compose 的上下文或工作目录
【发布时间】:2016-10-25 20:12:57
【问题描述】:

我正在学习 docker

我需要为一个 docker 镜像指定工作目录,我想应该是这样的:

version: '2'
services:
  test:
    build:
      context: ./dir

现在我想让图像python:onbuild./dir 上运行,但我不想在./dir 内创建任何Dockerfile

docker-compose 手册对此只字未提。

有可能吗?该怎么做?

【问题讨论】:

    标签: docker docker-compose


    【解决方案1】:

    我认为您正在寻找working_dir。在docker-compose reference 中搜索“working_dir”。

    【讨论】:

    • 该链接不再指向特定部分,一旦我找到特定部分,它就无法清楚地显示 working_dir 在 YAML 层次结构中的位置。这篇博文很有帮助:medium.com/gemma-black/…
    【解决方案2】:

    您可以按如下方式指定工作目录。

    version: '2'
    services:
      test:
        build:
          context: ./dir
        working_dir: /dir
    

    【讨论】:

      【解决方案3】:

      可能不完全是您要查找的内容,但您可以将 docker 构建的“上下文”指定为与实际 Dockerfile 所在的目录不同的目录。

      build:
        context: ./folder/containing/files
        dockerfile: path/to/dockerfile/relative/to/context/Dockerfile
      

      无论 Dockerfile 实际位于何处,您的 Dockerfile 中的任何 ADD/COPY 命令都如同与 context 相关。

      【讨论】:

        【解决方案4】:

        build configuration in Docker Compose 最终会调用docker build,因此您需要有一个 Dockerfile 才能使用该工作流程。

        正如docs for python:onbuild 所说,您可以从仅包含FROM python:onbuild 的最小Dockerfile 开始。但正如他们所说,:onbuild 不是一个好选择,您将拥有更多的控制权来构建自己的 Dockerfile FROM python

        【讨论】:

          猜你喜欢
          • 2020-03-26
          • 2020-04-18
          • 1970-01-01
          • 1970-01-01
          • 2018-03-25
          • 1970-01-01
          • 2021-01-24
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多