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

How can I create a docker container of sonatype/nexus with a default user and password?

我的 docker-compose 很简单:

version: "3"

services:

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

  ...
  (some other services)

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

注意,我还尝试创建容器、添加用户、创建图像。这没有用,用户在重启时消失了。

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

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

仅适用于 Nexus 2.x(Nexus 3 使用嵌入式数据库 OrientDB):

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

  1. 手动启动 sonatype/nexus,在浏览器中以管理员身份登录,创建您的自定义用户并为其分配至少一个角色。
  2. ${SONATYPE_WORK}/conf/security.xml 保存到您的本地磁盘。这是必需的,因为需要对密码进行编码。解密密钥不会在同一图像的容器中发生变化。
  3. docker-compose.yaml 中的命令创建包装器 shell 脚本。这将至少包含三个步骤:

    一个。 运行 作为后台进程的 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} &

    乙。在 nexus 应用程序启动后(为了简单起见,我在这里添加了 5 秒的睡眠)将保存的 security.xml 复制到 ${SONATYPE_WORK}/conf/security.xml。这应该已经在 docker-compose.yaml 中加载到容器 ANYWHERE BUT ${SONATYPE_WORK}/conf,这会使 nexus 崩溃启动时的应用程序(我只能推测为什么...)

    C.执行任何永久命令,这样容器就不会退出。一个想法是将 nexus 应用程序重新连接回 shell。 tail -f /path/to/something.txt 也可以。

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

我的文件供参考:

init.sh(这是命令包装器)

#!/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(这是从手动创建的实例复制的)

<?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>what@not.com</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>changeme@yourcompany.com</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
  ...