sonatype nexus docker音量错误

我triyng使用docker安装sonatype-nexus,并希望与主机(Linux的Ubuntu 14.04) /opt/nexus共享/opt/sonatype-work nexus回购。

我的dockerfile:

 FROM centos:6 MAINTAINER Marcel Birkner <marcel.birkner@codecentric.de> USER root # Update the system RUN yum -y update; \ yum clean all ########################################################## # Install Java JDK, SSH and other useful cmdline utilities ########################################################## RUN yum -y install java-1.7.0-openjdk-devel \ which \ telnet \ unzip \ openssh-server \ sudo \ openssh-clients \ iputils \ iproute \ httpd-tools \ wget \ tar; \ yum clean all ENV JAVA_HOME /usr/lib/jvm/jre ########################################################## # Install Nexus ########################################################## RUN mkdir -p /opt/sonatype-nexus /opt/sonatype-work RUN wget -O /tmp/nexus-latest-bundle.tar.gz http://www.sonatype.org/downloads/nexus-latest-bundle.tar.gz RUN tar xzvf /tmp/nexus-latest-bundle.tar.gz -C /opt/sonatype-nexus --strip-components=1 RUN useradd --user-group --system --home-dir /opt/sonatype-nexus nexus ADD nexus.xml /opt/sonatype-work/nexus/conf/nexus.xml RUN chown -R nexus:nexus /opt/sonatype-work /opt/sonatype-nexus ENV NEXUS_WEBAPP_CONTEXT_PATH /nexus RUN echo "#!/bin/bash" > /opt/start-nexus.sh RUN echo "su -c \"/opt/sonatype-nexus/bin/nexus console\" - nexus" >> /opt/start-nexus.sh RUN chmod +x /opt/start-nexus.sh VOLUME /opt/sonatype-work CMD ["/opt/start-nexus.sh"] EXPOSE 8081 

当我build立这个形象(build立成功):

 docker build -t sonatype/nexus . 

然后我运行它通过这个命令:

 docker run -d -p 8081:8081 --name nexus -v /opt/nexus:/opt/sonatype-work sonatype/nexus 

它立即启动并停止错误显示( docker logs nexus ):

 nexus_1 | jvm 1 | Caused by: java.nio.file.AccessDeniedException: /opt/sonatype-work/nexus nexus_1 | jvm 1 | at sun.nio.fs.UnixException.translateToIOException(UnixException.java:84) ~[na:1.7.0_99] nexus_1 | jvm 1 | at sun.nio.fs.UnixException.rethrowAsIOException(UnixException.java:102) ~[na:1.7.0_99] nexus_1 | jvm 1 | at sun.nio.fs.UnixException.rethrowAsIOException(UnixException.java:107) ~[na:1.7.0_99] nexus_1 | jvm 1 | at sun.nio.fs.UnixFileSystemProvider.createDirectory(UnixFileSystemProvider.java:383) ~[na:1.7.0_99] nexus_1 | jvm 1 | at java.nio.file.Files.createDirectory(Files.java:630) ~[na:1.7.0_99] nexus_1 | jvm 1 | at java.nio.file.Files.createAndCheckIsDirectory(Files.java:734) ~[na:1.7.0_99] nexus_1 | jvm 1 | at java.nio.file.Files.createDirectories(Files.java:720) ~[na:1.7.0_99] nexus_1 | jvm 1 | at org.sonatype.nexus.util.file.DirSupport.mkdir(DirSupport.java:146) ~[na:na] nexus_1 | jvm 1 | at org.sonatype.nexus.util.file.DirSupport.mkdir(DirSupport.java:162) ~[na:na] nexus_1 | jvm 1 | at org.sonatype.nexus.webapp.WebappBootstrap.contextInitialized(WebappBootstrap.java:115) ~[na:na] nexus_1 | jvm 1 | ... 16 common frames omitted nexus_1 | wrapper | <-- Wrapper Stopped 

如果我从docker文件中删除VOLUME /opt/sonatype-nexus它工作正常。

你有什么想法可能会导致这个问题? 以及如何解决它?

如果在容器中绑定挂载主机目录,则主机目录中的文件和目录优先,并挂载到容器映像中已存在的文件上。 换句话说,他们“掩盖”容器中的内容。

绑定挂载保持主机上目录的权限,如果主机上没有目录,Docker会使用root:root作为所有者来创build它。

看看你的Dockerfile中的useradd nexus ,我怀疑start-nexus.sh与那个用户运行nexus,所以它可能没有绑定挂载的目录(它由root拥有)的权限。 您可以通过将目录chowning到容器内的nexus的数字uid / gid来解决这个问题。

要获得该用户的uid / gid ,请交互式启动容器;

 docker run -it --rm sonatype/nexus bash 

而在这个shell里面请求uid / gid:

 id nexus 

这给你一些东西:

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

现在退出容器( exit ),并使用uid / gid在主机上显示目录。

 sudo chown -R 123:456 /opt/nexus 

有些事情我注意到了

它看起来像你build立自己的自定义版本的sonatype nexus图像,但使用相同的名称作为官方形象( sonatype/nexus )。 我build议不要这样做,并给它自己的名字(例如mycompany/nexus ); 这可以防止混淆,也可以防止你自己的图像被官方图像覆盖,如果有人运行docker pull sonatype/nexus

有没有任何理由不使用官方形象? 一般来说,build议使用正式的图像,因为它们由软件的维护者维护(在这种情况下是sonatype),所以应该给你一个最新的(和维护)版本的软件; https://hub.docker.com/r/sonatype/nexus/