Search code examples
dockerdocker-composedocker-volumesonatypenexus3

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. To do that I am using this 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:

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):

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

After docker-compose up command, docker created the volume as shown below:

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:

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

So, what am I doing wrong here? Why isnexus-data empty and docker creating volume in another path?


Solution

  • 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. 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. You should be able to exec into the container and see your files with:

    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.