简体   繁体   English

为什么 docker-compose 在随机路径中创建卷而不是我在 docker-compose.yml 中指定的路径?

[英]Why is docker-compose creating volume in random path instead of the path I specified in docker-compose.yml?

I am trying to store nexus data in persistent volume.我正在尝试将 nexus 数据存储在持久卷中。 To do that I am using this compose yaml:为此,我使用了这个 compose yaml:

version: '3.5'

services:
  nexus:
    image: sonatype/nexus3
    volumes:
       - ./nexus-data:/nexus-data sonatype/nexus3
    ports:
      - "8081:8081"
    networks:
      - devops
    extra_hosts:
      - "my-proxy:my-proxy-address"
    restart: on-failure

networks:
  devops:
    name: devops
    driver: bridge

Before running docker-compose up I have created the nexus-data folder and gave required permissions to the uid/guid 200 as suggested here:在运行docker-compose up之前docker-compose up我已经创建了nexus-data文件夹,并按照此处的建议为uid/guid 200提供了所需的权限:

https://github.com/sonatype/docker-nexus3/blob/master/README.md#persistent-data . https://github.com/sonatype/docker-nexus3/blob/master/README.md#persistent-data

root@master-node:~/docker# ll
total 16
drwxr-xr-x  3 root root 4096 Jan  8 13:37 ./
drwx------ 22 root root 4096 Jan  8 13:40 ../
-rw-r--r--  1 root root  319 Jan  8 13:36 docker-compose.yml
drwxr-xr-x  2  200  200 4096 Jan  8 13:37 nexus-data/

And here docker's volumes list before running compose file (it's empty):在运行撰写文件之前,这里是 docker 的卷列表(它是空的):

root@master-node:~/docker# docker volume ls
DRIVER              VOLUME NAME

After docker-compose up command, docker created the volume as shown below:docker-compose up命令之后,docker 创建了如下图所示的卷:

root@master-node:~/docker# docker volume ls
DRIVER              VOLUME NAME
local               7b7b6517e5ed0e286a8fc7caef756141b5bbdb6e074ef93a657850da3dd78b2b
root@master-node:~/docker# docker volume inspect  7b7b6517e5ed0e286a8fc7caef756141b5bbdb6e074ef93a657850da3dd78b2b 
[
    {
        "CreatedAt": "2020-01-08T13:42:34+03:00",
        "Driver": "local",
        "Labels": null,
        "Mountpoint": "/var/lib/docker/volumes/7b7b6517e5ed0e286a8fc7caef756141b5bbdb6e074ef93a657850da3dd78b2b/_data",
        "Name": "7b7b6517e5ed0e286a8fc7caef756141b5bbdb6e074ef93a657850da3dd78b2b",
        "Options": null,
        "Scope": "local"
    }
]
root@master-node:~/docker# ls /var/lib/docker/volumes/7b7b6517e5ed0e286a8fc7caef756141b5bbdb6e074ef93a657850da3dd78b2b/_data
admin.password  cache  db  elasticsearch  etc  generated-bundles  instances  javaprefs  karaf.pid  keystores  lock  log  orient  port  restore-from-backup  tmp

but the folder I specified in compose file ( nexus-data ) is still empty:但是我在撰写文件( nexus-data )中指定的文件夹仍然是空的:

root@master-node:~/docker# ls nexus-data/
root@master-node:~/docker# 

So, what am I doing wrong here?那么,我在这里做错了什么? Why is nexus-data empty and docker creating volume in another path?为什么nexus-data空并且 docker 在另一条路径中创建卷?

You defined a volume instead of a bind mount, which is what you want.您定义了一个卷而不是绑定安装,这正是您想要的。 Read the documentation about it.阅读有关它的文档

Basically, your configuration makes docker create a volume , which maps to a randomly created directory somewhere under /var/lib/docker/volumes .基本上,您的配置使 docker 创建一个volume ,它映射到/var/lib/docker/volumes下某处随机创建的目录。

If you want a specific directory that you control, you have to create a bind .如果你想要一个你控制的特定目录,你必须创建一个bind That's the reason you don't have data on your chosen directory, as docker ignores it because it is not useful for a volume .这就是您在所选目录中没有数据的原因,因为 docker 会忽略它,因为它对volume没有用。

For it to work, set it like:要使其正常工作,请将其设置为:

    volumes:
      - type: bind
        source: ./nexus-data
        target: /nexus-data

as explained in the compose documentation .如撰写文档中所述 (Also removed the image name from that configuration) (还从该配置中删除了图像名称)

You've created a host volume, aka bind mount (which doesn't show in docker volume ls since it's not a named volume)B from ./nexus-data on the host to /nexus-data sonatype/nexus3 inside the container.您已经创建了一个主机卷,又名绑定安装(它没有显示在docker volume ls因为它不是命名卷)B 从./nexus-data上的/nexus-data sonatype/nexus3到容器内的/nexus-data sonatype/nexus3 This looks like a copy and paste error from a docker run command since you are adding the image name to the path being mounted inside the container.这看起来像是来自docker run命令的复制和粘贴错误,因为您将图像名称添加到安装在容器内的路径中。 You should be able to exec into the container and see your files with:您应该能够 exec 进入容器并查看您的文件:

docker exec ... ls "/nexus-data sonatype/nexus3"

You should remove the image name from the volume path to mount the typical location inside the container:您应该从卷路径中删除映像名称以挂载容器内的典型位置:

version: '3.5'

services:
  nexus:
    image: sonatype/nexus3
    volumes:
       - ./nexus-data:/nexus-data
    ports:
      - "8081:8081"
    networks:
      - devops
    extra_hosts:
      - "my-proxy:my-proxy-address"
    restart: on-failure

networks:
  devops:
    name: devops
    driver: bridge

The volume you did see was an anonymous volume.您确实看到的卷是匿名卷。 This would be from the image itself defining a volume that you did not include in your container spec.这将来自定义您未包含在容器规范中的卷的图像本身。 Inspecting the container using that volume would show where it's mounted, most likely at /nexus-data .使用该卷检查容器将显示它的安装位置,最有可能在/nexus-data

The below docker-compose.yaml works as expected:下面docker-compose.yaml按预期工作:

version: '3.5'

services:
  nexus:
    image: sonatype/nexus3
    volumes:
       - ./nexus-data:/nexus-data
    ports:
      - "8081:8081"
    networks:
      - devops
    extra_hosts:
      - "my-proxy:my-proxy-address"
    restart: on-failure

networks:
  devops:
    name: devops
    driver: bridge

The trailing sonatype/nexus3 in your original docker-compose.yaml spec:原始docker-compose.yaml规范中的尾随sonatype/nexus3

    ...
    volumes:
       - ./nexus-data:/nexus-data sonatype/nexus3
    ...

... throws docker off - the randomly named volume is created given the VOLUME instruction in the sonatype/nexus3 Dockerfile and is mounted to the running container hence why the intended locally mounted directory is empty. ...抛出docker off - 随机命名的卷是根据sonatype/nexus3 DockerfileVOLUME指令创建的,并安装到正在运行的容器中,因此为什么预期的本地安装目录为空。

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

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