sonatype nexus docker volume error

If you bind-mount a host directory in a container, the files and directories in the host directory take precedence and are mounted over the files already present inside the container's image. In other words, they "mask" what's underneath in the container.

Bind-mounts keep their permissions of the directory that's present on the host, and if no directory is present on the host, Docker creates it, using root:root as owner.

Looking at the useradd nexus in your Dockerfile, I suspect that start-nexus.sh runs nexus with that user, so it may not have permissions on the bind-mounted directory (which is owned by root). You can fix this by chowning the directory to the numeric uid/gid of nexus inside the container.

To get the uid / gid of that user, start the container interactively;

docker run -it --rm sonatype/nexus bash

And inside that shell request the uid/gid:

id nexus

Which gives you something like:

uid=123(nexus) gid=456(nexus) groups=456(nexus)

Now exit the container (exit), and chown the directory on the host, using the uid/gid;

sudo chown -R 123:456 /opt/nexus

Some things I noticed

It looks like you're building your own custom version of the sonatype nexus image, but use the same name as the official image (sonatype/nexus). I'd recommend not doing that, and giving it your own name (e.g. mycompany/nexus); this prevents confusion, and also prevents your own image to be overwritten with the official image if someone runs docker pull sonatype/nexus.

Is there any reason for not using the official image? In general it's recommended to use the official images, as they are maintained by the maintainers of the software (sonatype in this case), so should give you an up-to-date (and maintained) version of the software; https://hub.docker.com/r/sonatype/nexus/