Nexus as private docker registry

Naveen ElwakaNaveen Elwaka
4 min read

To install Nexus Repository Manager on Ubuntu, you can follow these steps:

  1. Prerequisites: Ensure that you have Java Development Kit (JDK) installed on your Ubuntu system. Nexus Repository Manager requires Java to run. You can install OpenJDK using the following command:
sudo apt update
sudo apt install openjdk-11-jdk
  1. Download Nexus Repository Manager: Visit the Sonatype download page (https://www.sonatype.com/nexus/repository-oss/download) and find the latest version of Nexus Repository Manager. Copy the download link for the Linux version of the Nexus Repository Manager.

  2. Download and extract Nexus: Open a terminal and use the wget command to download the Nexus Repository Manager. Replace <DOWNLOAD_LINK> with the actual download link you copied.

wget <DOWNLOAD_LINK>

After the download is complete, extract the downloaded archive using the following command. Replace <NEXUS_VERSION> with the actual version number.

tar -xvzf nexus-<NEXUS_VERSION>.tar.gz
  1. Move Nexus folder: Move the extracted Nexus folder to the desired installation location. For example, you can move it to the /opt directory.
sudo mv nexus-<NEXUS_VERSION> /opt/nexus
  1. Create Nexus user: It is recommended to run Nexus as a dedicated user. Create a new system user for Nexus using the following command:
sudo adduser --system --no-create-home --group nexus
  1. Configure Nexus: Change the ownership of the Nexus installation directory to the Nexus user.
sudo chown -R nexus:nexus /opt/nexus

Next, open the Nexus configuration file using a text editor.

sudo nano /opt/nexus/bin/nexus.rc

Uncomment the run_as_user line and set it to the Nexus user.

run_as_user="nexus"

Save and close the file.

  1. Configure Nexus as a Service: Create a service file for Nexus to run it as a service.
sudo nano /etc/systemd/system/nexus.service

Add the following content to the file:

[Unit]
Description=Nexus Repository Manager
After=network.target

[Service]
Type=forking
LimitNOFILE=65536
ExecStart=/opt/nexus/bin/nexus start
ExecStop=/opt/nexus/bin/nexus stop
User=nexus
Restart=on-abort

[Install]
WantedBy=multi-user.target

Save and close the file.

  1. Start Nexus: Start the Nexus service using the following commands:
sudo systemctl daemon-reload
sudo systemctl start nexus
  1. Access Nexus: Nexus Repository Manager should now be running. You can access it using a web browser by navigating to http://localhost:8081. The first time you access it, you will be prompted to set up the administrator account and configure the Nexus repository.

how to configure the docker host to have nexus as a private repository?

To configure your Docker host to use Nexus as a private repository, follow these steps:

  1. Install Docker: Ensure that Docker is installed on your host machine. You can follow the official Docker documentation for the installation steps specific to your operating system.

  2. Configure Docker to use Nexus as a registry: Docker needs to be configured to recognize Nexus as a private registry. Open the Docker daemon configuration file using a text editor. The location of the file depends on your operating system:

    • Ubuntu: /etc/docker/daemon.json

If the file doesn't exist, create it.

  1. Add the following configuration to the daemon.json file, replacing <NEXUS_HOST> with the hostname or IP address of your Nexus server:

     {
       "insecure-registries": ["<NEXUS_HOST>:8081"]
     }
    

    This configuration allows Docker to communicate with Nexus over HTTP (non-secure). If you're using HTTPS, you'll need to set up TLS certificates and use the secure URL.

  2. Save the daemon.json file and exit the text editor.

  3. Restart Docker: Restart the Docker daemon to apply the configuration changes. Use the appropriate command for your operating system:

    • Ubuntu:

        sudo systemctl restart docker
      
  4. Authenticate with Nexus: Before you can pull or push Docker images from/to Nexus, you need to authenticate your Docker client with Nexus. Run the following command in your terminal:

     docker login -u <NEXUS_USERNAME> -p <NEXUS_PASSWORD> <NEXUS_HOST>:8081
    

    Replace <NEXUS_USERNAME> and <NEXUS_PASSWORD> with your Nexus credentials, and <NEXUS_HOST> with the hostname or IP address of your Nexus server.

    This command will authenticate your Docker client with Nexus and create a login session that allows you to pull and push images.

  5. Pull and push Docker images: You can now pull Docker images from Nexus using their full repository path. For example:

     docker pull <NEXUS_HOST>:8081/<REPOSITORY>/<IMAGE>:<TAG>
    

    Replace <REPOSITORY> with the name of the Nexus repository, <IMAGE> with the name of the Docker image, and <TAG> with the image tag.

    To push Docker images to Nexus, use the following command:

     docker push <NEXUS_HOST>:8081/<REPOSITORY>/<IMAGE>:<TAG>
    

    Ensure that the Docker image is properly tagged before pushing.

0
Subscribe to my newsletter

Read articles from Naveen Elwaka directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Naveen Elwaka
Naveen Elwaka

DevOps ๐Ÿ‘จโ€๐Ÿ’ป | Tech Blogger ๐Ÿ‘จโ€๐Ÿ’ป | Cloud Computing | Cloud Native | Open Source