如何使用默认用户和密码创建sonatype / nexus的docker容器?

问题描述 投票:0回答:2

我的docker-compose很简单:

version: "3"

services:

  nexus:
    container_name: nexus
    image: sonatype/nexus:2.14.5-02
    ports:
      - "8081:8081"

  ...
  (some other services)

现在,我必须启动它,然后以管理员身份登录以创建公司用户。我可以通过将自定义配置作为卷添加自定义用户吗?如果是这样,那个配置是​​什么?或者还有另一种方法可以做到这一点。

注意,我也尝试创建容器,添加用户,创建图像。这不起作用,用户在重启时消失了。

docker docker-compose nexus sonatype
2个回答
0
投票

您需要创建两个xml文件security-configuration.xml和security.xml文件。在security.xml文件中,您可以添加所有必需的用户。您可以检查security.xml的链接。 Security.xml details

使用这两个文件创建一个文件夹,并将其用作该文件夹的卷。在/ opt / nexus / nexus-data / conf文件夹中的容器内使用该卷。


0
投票

通过一些更多的实验找到了解决方案。要在启动时为sonatype / nexus docker镜像创建自定义用户:

  1. 手动启动sonatype / nexus,在浏览器中以admin身份登录,创建自定义用户并为其分配至少一个角色。
  2. ${SONATYPE_WORK}/conf/security.xml保存到本地磁盘。这是必要的,因为密码需要编码。解密密钥不会在同一图像的容器之间发生变化。
  3. docker-compose.yaml中为命令创建包装器shell脚本。这将包含至少三个步骤: A.运行nexus应用程序作为后台进程(这是我从父Dockerfile的CMD复制并添加&到它)${JAVA_HOME}/bin/java \ -Dnexus-work=${SONATYPE_WORK} -Dnexus-webapp-context-path=${CONTEXT_PATH} \ -Xms${MIN_HEAP} -Xmx${MAX_HEAP} \ -cp 'conf/:lib/*' \ ${JAVA_OPTS} \ org.sonatype.nexus.bootstrap.Launcher ${LAUNCHER_CONF} & B.在nexus应用程序启动后(为了简单起见,我在这里添加了5秒钟的睡眠)将保存的security.xml复制到${SONATYPE_WORK}/conf/security.xml。这应该已经在docker-compose.yaml中加载到容器ANYWHERE但是$ {SONATYPE_WORK} / conf,这会在启动时崩溃nexus应用程序(我只能推测为什么......) C.执行任何永久命令,以便容器不会退出。一个想法是将nexus应用程序重新连接到shell。还有一个tail -f /path/to/something.txt会起作用。

现在,您应该可以运行docker-compose并在浏览器上使用自定义用户登录。

这里参考我的文件:

init.sh (this is the command wrapper)

#!/usr/bin/env bash

set -x

${JAVA_HOME}/bin/java \
  -Dnexus-work=${SONATYPE_WORK} -Dnexus-webapp-context-path=${CONTEXT_PATH} \
  -Xms${MIN_HEAP} -Xmx${MAX_HEAP} \
  -cp 'conf/:lib/*' \
  ${JAVA_OPTS} \
  org.sonatype.nexus.bootstrap.Launcher ${LAUNCHER_CONF} &

# here some delay may be necessary, or a function to wait the nexus app to populate ${SONATYPE_WORK}/conf.
sleep 5

cp /nexus-dependencies/security-test.xml ${SONATYPE_WORK}/conf/security.xml

# I'm also copying nexus.xml to customize the Snapshot repository.
cp /nexus-dependencies/nexus.xml ${SONATYPE_WORK}/conf/nexus.xml

tail -f /nexus-dependencies/init-nexus.sh

注意:/nexus-dependencies是我在docker-compose.yaml中加载的卷。此目录包含我的security.xml版本,其中包含2个用户(公司和管理员)及其角色。如果用户没有任何角色,则该用户将无法使用。

security.xml (this was copied from a manually created instance)

<?xml version="1.0" encoding="UTF-8"?>
<security>
  <version>2.0.5</version>

  <!-- Users -->
  <users>
    <!-- The Company User -->
    <user>
      <id>companyX</id>
      <firstName>First</firstName>
      <lastName>Last</lastName>
      <password>$shiro1$SHA-512$SOME-ENCODED-PASSWORd-COPIED-FROM-A-PREVIOWS-INSTANCE-OF-THIS-IMAGE==</password>
      <!-- <password>RF1Dkings</password> -->
      <status>active</status>
      <email>[email protected]</email>
    </user>


    <!-- The Admin User -->
    <user>
      <id>admin</id>
      <firstName>Administrator</firstName>
      <lastName>User</lastName>
      <password>$shiro1$SHA-512$This could just be the custom admin password, or not.</password>
      <status>active</status>
      <email>[email protected]</email>
    </user>

  </users>


  <!-- Roles -->
  <userRoleMappings>
    <!-- CompanyX User role mapping -->
    <userRoleMapping>
      <userId>companyX</userId>
      <source>default</source>
      <roles>
        <role>nx-developer</role>
      </roles>
    </userRoleMapping>
  <!-- End CompanyX User role mapping -->


  <!-- Admin User Roles -->
    <userRoleMapping>
      <userId>admin</userId>
      <source>default</source>
      <roles>
        <role>nx-admin</role>
      </roles>
    </userRoleMapping>
    <!-- End Admin User Roles -->

  </userRoleMappings>
</security>

docker-compose.yaml

version: "3"

services:
  ...
  nexus:
    container_name: nexus
    image: sonatype/nexus:2.14.5-02
    ports:
      - "8081:8081"
    volumes:
      - ./nexus-dependencies:/nexus-dependencies
    command: bash /nexus-dependencies/init.sh
  ...
© www.soinside.com 2019 - 2024. All rights reserved.