Docker容器卷未从镜像中填充指定路径

huangapple go评论76阅读模式
英文:

Docker Volume with specified path not populated from image

问题

我的 Docker 镜像包含位于目录 /data 中的文件。

当我运行带有命名或未命名卷的 Docker 容器,并将其关联到该目录时,卷会被从镜像中填充文件(这是应该的)。

但是,当我尝试将卷放置到特定路径时,如下所示:

volumes:
  - ./data:/data

主机目录 ./data 不会从镜像中填充文件,它是空的(但从容器中可以访问)。是否有一些关于创建命名、未命名卷和特定主机路径卷的差异可以解释这种行为?

我的 Dockerfile:

FROM hypriot/rpi-alpine

RUN apk update && \
    apk upgrade && \
    apk add --no-cache bash minidlna && \
    rm -rf /var/cache/apk/*

RUN mkdir /data && \
    mkdir /data/log && \
    mkdir /data/db

COPY minidlna.conf /data/minidlna.conf
COPY minidlna.conf /minidlna.conf
COPY entrypoint.sh /entrypoint.sh

VOLUME ["/data"]

RUN chmod +x /entrypoint.sh
ENTRYPOINT ["/entrypoint.sh"]

和 docker-compose.yml:

version: "3"

services:
  minidlna:
    image: silvan85/rpi-minidlna
    volumes:
      - ./data:/data
      - /media/ExternalHDD/media:/media
    network_mode: "host"
    restart: unless-stopped
英文:

My docker image contain files in directory /data

When I run docker container with named or unnamed volume associated with this directory volume is populated with files from image (as it sould be).

But when I try to put volume to specific path like:

volumes:
  - ./data:/data

Host directory ./data is not populated witch files from image it is empty. (but accessible from container).

Is there some difference between creation of named, unnamed volumes and volumes in specific host path that explains this behavior?

My dockrefile:

FROM hypriot/rpi-alpine 

RUN apk update && \
    apk upgrade && \
    apk add --no-cache bash minidlna && \
    rm -rf /var/cache/apk/*

RUN mkdir /data %% \
    mkdir /data/log && \
    mkdir /data/db

COPY minidlna.conf /data/minidlna.conf
COPY minidlna.conf /minidlna.conf
COPY entrypoint.sh /entrypoint.sh

VOLUME ["/data"]

RUN chmod +x /entrypoint.sh
ENTRYPOINT ["/entrypoint.sh"]

and docker-compose.yml

version: "3" 

services:
  minidlna:
    image: silvan85/rpi-minidlna
    volumes:
      - ./data:/data
      - /media/ExternalHDD/media:/media
    network_mode: "host"
    restart: unless-stopped

答案1

得分: 0

当你将主机目录挂载到容器中时,主机目录的内容会覆盖容器的内容。

请参阅 https://stackoverflow.com/questions/59598564/mount-files-present-in-a-directory-in-docker-image-with-the-directory-in-the-hos/59598831#59598831

英文:

When you mount a host directory into container, the contents of host directory shadow the contents of container.

Please see https://stackoverflow.com/questions/59598564/mount-files-present-in-a-directory-in-docker-image-with-the-directory-in-the-hos/59598831#59598831

huangapple
  • 本文由 发表于 2020年1月7日 01:30:17
  • 转载请务必保留本文链接:https://go.coder-hub.com/59616467.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定