简体   繁体   English

Docker 更改命名卷的位置

[英]Docker change location of named volumes

I have a problem that I just can't understand.我有一个我无法理解的问题。 I am using docker to run certain containers, but I have problems with at least one Volume, where I't like to ask if anybody can give me a hint what I am doing wrong.我正在使用 docker 来运行某些容器,但我至少有一个卷有问题,我不想问是否有人可以提示我做错了什么。 I am using Nifi-Ingestion as example, but it affects even more container volumes.我以 Nifi-Ingestion 为例,但它会影响更多的容器体积。

First, let's talk about the versions I use:首先说一下我使用的版本:

  • Docker version 19.03.8, build afacb8b7f0 Docker 版本 19.03.8,构建 afacb8b7f0
  • docker-compose version 1.27.4, build 40524192 docker-compose 版本 1.27.4,构建 40524192
  • Ubuntu 20.04.1 LTS Ubuntu 20.04.1 LTS

Now, let's show the volume in my working docker-compose-file:现在,让我们在我的工作 docker-compose-file 中显示卷:

In my container, it is configured as followed:在我的容器中,它的配置如下:

volumes:
    - nifi-ingestion-conf:/opt/nifi/nifi-current/conf

Below my docker-compose file it is defined as a normal named volume:在我的 docker-compose 文件下面,它被定义为一个普通的命名卷:

volumes:
    nifi-ingestion-conf:

This is a snippet from the docker-compose that I'd like to get working这是 docker-compose 的一个片段,我想开始工作

In my container, it is configured in this case as followed (having my STORAGE_VOLUME_PATH defined as /mnt/storage/docker_data):在我的容器中,在这种情况下配置如下(将我的 STORAGE_VOLUME_PATH 定义为 /mnt/storage/docker_data):

volumes:
    - ${STORAGE_VOLUME_PATH}/nifi-ingestion-conf:/opt/nifi/nifi-current/conf

On the bottom I guess there is something to do but I don't know what I could need to do here.在底部,我想有一些事情要做,但我不知道我需要在这里做什么。 In this case it is the same as in the working docker-compose:在这种情况下,它与工作中的 docker-compose 相同:

volumes:
    nifi-ingestion-conf:

So, now whats my problem?那么,现在我的问题是什么?

I have two docker-compose files.我有两个 docker-compose 文件。 One uses the normal named volumes, and one uses the volumes in my extra mount path.一个使用普通的命名卷,一个使用我的额外挂载路径中的卷。 When I run the containers, the volumes seem to work different since files are written in the first style, but not in the second.当我运行容器时,卷的工作方式似乎有所不同,因为文件是用第一种风格编写的,而不是第二种风格。 My mount paths are generated in the second version so there is nothing wrong with my environment variables in the.env-file.我的挂载路径是在第二个版本中生成的,因此 .env 文件中的环境变量没有任何问题。

Hint: the /mnt/storage/docker_data is an NFS-mount but my machine has the full privileges on that share.提示:/mnt/storage/docker_data 是一个 NFS 挂载,但我的机器拥有该共享的全部权限。

Here is my fstab-entry to mount that volume (maybe I have to set other options):这是我挂载该卷的 fstab 条目(也许我必须设置其他选项):

10.1.0.2:/docker/data               /mnt/storage/docker_data        nfs     auto,rw

Bigger snippets更大的片段

Here is a bigger snipped if the docker-compose (i need to cut and remove confident data, my problem is not that it does not work, it is only that the volume acts different. Everything for this one volume is in the code.):如果 docker-compose 是一个更大的片段(我需要剪切和删除有信心的数据,我的问题不是它不起作用,只是音量不同。这一卷的所有内容都在代码中。):

version: "3"
services:
    nifi-ingestion:
        image: my image on my personal repo
        container_name: nifi-ingestion
        ports:
            - 0000
        labels:
            - app-specivic
        volumes:
            - ${STORAGE_VOLUME_PATH}/nifi-ingestion-conf:/opt/nifi/nifi-current/conf
            #working: - nifi-ingestion-conf:/opt/nifi/nifi-current/conf
        environment:
            - app-specivic
        networks:
            - cnetwork

volumes:
    nifi-ingestion-conf:

networks:
    cnetwork:
        external: false
        ipam:
            driver: default
            config:
                - subnet: 192.168.1.0/24

And here of the env (only the value we are using)这里的 env (只有我们正在使用的值)

STORAGE_VOLUME_PATH=/mnt/storage/docker_data

if i understand your question correctly, you wonder why the following docker-compose snippet works for you如果我正确理解了您的问题,您想知道为什么以下 docker-compose 片段对您有用

version: "3"
services:
  nifi-ingestion:
    volumes:
       - nifi-ingestion-conf:/opt/nifi/nifi-current/conf
volumes:
  nifi-ingestion-conf:

and the following docker-compose snippet does not work for you以下 docker-compose 片段对您不起作用

version: "3"
services:
  nifi-ingestion:
    volumes:
      - ${STORAGE_VOLUME_PATH}/nifi-ingestion-conf:/opt/nifi/nifi-current/conf

what makes them different is how you use volumes.使它们不同的是您使用卷的方式。 you need to differentiate between mount host paths and mount named volumes您需要区分挂载主机路径和挂载命名卷

You can mount a host path as part of a definition for a single service, and there is no need to define it in the top level volumes key.您可以将主机路径挂载为单个服务定义的一部分,无需在顶级volumes键中定义它。

But, if you want to reuse a volume across multiple services, then define a named volume in the top-level volumes key.但是,如果您想跨多个服务重用一个卷,则在顶级volumes键中定义一个命名卷。

named volumes are managed by docker命名卷由 docker 管理

If you start a container with a volume that does not yet exist, Docker creates the volume for you.如果您使用尚不存在的卷启动容器,Docker 会为您创建卷。

also, would advise you to read this answer另外,建议您阅读此答案

update: you might also want to read about docker nfs volumes更新:您可能还想了解docker nfs 卷

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM