【问题标题】:How to map the host OS file to the container at the time of container build process如何在容器构建过程中将主机 OS 文件映射到容器
【发布时间】:2017-10-31 07:53:47
【问题描述】:

docker-compose.yml:

    version: '3'
    services:
      ezmove:
        volumes:
          - /host-dir:/home/container-dir    
        build:
          context: .
          args:
            BRANCH: develop

Dockerfile:

FROM appcontainers/ubuntu:xenial

MAINTAINER user <user>

RUN apt-get update -y --no-install-recommends \
    && apt-get install -y --no-install-recommends python3.5-minimal python3.5-venv \    
    && apt-get install -y --no-install-recommends git \
    && apt-get install -y --no-install-recommends python-pip \
    && pip install --upgrade pip \  
    && rm -rf /var/lib/apt/lists/* \
    && mkdir -p /home/container-dir 

WORKDIR /home/container-dir 

RUN /bin/bash - c "sh ./script.sh"

在构建时 docker容器如何将本地目录映射到容器

当 $ docker-compose up 时,它将开始构建容器,但在安装包依赖项后,它会尝试执行 script.sh 文件但出现错误“未找到文件!”

试过了:

  1. 不想在 docker continer 中执行 git clone
  2. 不想将源代码存储在容器内

那么,如何在构建时将宿主 OS 文件映射到容器中

【问题讨论】:

    标签: docker docker-compose dockerfile


    【解决方案1】:

    您的 Dockerfile 中缺少一些 COPY 或 ADD 以便将您的 script.sh 复制到您的映像中。

    查看文档

    https://docs.docker.com/engine/reference/builder/#add

    https://docs.docker.com/engine/reference/builder/#copy

    顺便说一句,Docker是关于隔离的,所以一个运行中的容器应该和宿主机隔离,当然不能访问宿主机操作系统。

    【讨论】:

    • 感谢 #user2915097,使用 ADD 和 COPY 会起作用,但这些都有限制,例如 ADD obeys the following rules: The &lt;src&gt; path must be inside the context of the build; you cannot ADD ../something /something, because the first step of a docker build is to send the context directory (and subdirectories) to the docker daemon.
    • 嗨,我发布了与此stackoverflow.com/questions/47067944/…相关的新问题
    • 著名的 Vonc 回答
    猜你喜欢
    • 2015-01-22
    • 1970-01-01
    • 2019-05-21
    • 2017-09-11
    • 2020-02-26
    • 1970-01-01
    • 2018-11-28
    • 2021-04-03
    • 1970-01-01
    相关资源
    最近更新 更多