我正在尝试使用 docker 安装 sonatype-nexus 并希望共享 docker /opt/sonatype-work nexus repo 与主机 (linux ubuntu 14.04) /opt/nexus .

我的 docker 文件:

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 

当我构建此图像(构建成功)时:
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 

如果我删除了 VOLUME /opt/sonatype-nexus从 dockerfile 它工作正常。

你知道什么可能导致这个问题吗?以及如何解决?

请您参考如下方法:

如果您在容器中绑定(bind)挂载主机目录,则主机目录中的文件和目录优先,并挂载在容器镜像中已存在的文件之上。换句话说,它们“掩盖”了容器下面的东西。

绑定(bind)挂载保留主机上存在的目录的权限,如果主机上不存在目录,Docker 使用 root:root 作为所有者创建它。

查看 Dockerfile 中的 useradd nexus,我怀疑 start-nexus.sh 与该用户一起运行 nexus,因此它可能没有绑定(bind)挂载目录(由 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 chown 主机上的目录;
sudo chown -R 123:456 /opt/nexus 

我注意到的一些事情

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

不使用官方图片有什么理由吗?一般来说,推荐使用官方镜像,因为它们是由软件的维护者维护的(在这种情况下是 sonatype),所以应该给你一个最新的(和维护的)软件版本; https://hub.docker.com/r/sonatype/nexus/


评论关闭
IT干货网

微信公众号号:IT虾米 (左侧二维码扫一扫)欢迎添加!