How-To Deploy a Private Docker Registry on Google Cloud Platform with Nexus

January 08, 2019 By Mohamed Labouardy

5 minute read time

In this post, I will walk you through how to deploy Sonatype Nexus OSS 3 on Google Cloud Platform and how to create a private Docker hosted repository to store your Docker images and other build artifacts (maven, npm and pypi, etc). To achieve this, we need to bake our machine image using Packer to create a gold image with Nexus preinstalled and configured. Terraform will be used to deploy a Google compute instance based on the baked image. The following schema describes the build workflow:

Docker 1

PS : All the templates used in this tutorial, can be found on my GitHub.

To get started, we need to create the machine image to be used with Google Compute Engine (GCE). Packer will create a temporary instance based on the CentOS image and use a shell script to provision the instance:

 The shell script, will install the latest stable version of Nexus OSS based on official documentation and wait for the service to be up and running, then it will use the Scripting API to post a groovy script:

The script will create a Docker private registry listening on port 5000:

Once the template files are defined, issue packer build command to bake our machine image:

docker 2

If you head back to Images section from Compute Engine dashboard, a new image called nexus should be created:

dock3

Now we are ready to deploy Nexus, we will create a Nexus server based on the machine image we baked with Packer. The template file is self-explanatory, it creates a set of firewall rules to allow inbound traffic on port 8081 (Nexus GUI) and 22 (SSH) from anywhere, and creates a google compute instance based on the Nexus image:

On the terminal, run the terraform init command to download and install the Google provider, shown as follows:

dock4

Create an execution plan (dry run) with the terraform plan command. It shows you things that will be created in advance, which is good for debugging and ensuring that you’re not doing anything wrong, as shown in the next screenshot:

dock6

When you’re ready, go ahead and apply the changes by issuing terraform apply:

dock 7

Terraform will create the needed resources and display the public ip address of the nexus instance on the output section. Jump back to GCP Console, your nexus instance should be created:

dock8

If you point your favorite browser to http://instance_ip:8081, you should see the Sonatype Nexus Repository Manager interface:

dock9

Click the “Sign in” button in the upper right corner and use the username “admin” and the password “admin123”. Then, click on the cogwheel to go to the server administration and configuration section. Navigate to “Repositories”, our private Docker repository should be created as follows:

dock10

The docker repository is published as expected on port 5000:

dock11

Hence, we need to allow inbound traffic on that port, so update the firewall rules accordingly:

Issue terrafrom apply command to apply the changes:

dock12

Your private docker registry is ready to work at instance_ip:5000, let’s test it by pushing a docker image.

Since we have exposed the private Docker registry on a plain HTTP endpoint, we need to configure the Docker daemon that will act as client to the private Docker registry as to allow for insecure connections.

dock13

 

On Windows or Mac OS X: Click on the Docker icon in the tray to open Preferences. Click on the Daemon tab and add the IP address on which the Nexus GUI is exposed along with the port number 5000 in Insecure registries section. Don’t forget to Apply & Restart for the changes to take effect and you’re ready to go.


Other OS: Follow the official guide.

 

You should now be able to log in to your private Docker registry using the following command:

dock14

And push your docker images to the registry with the docker push command:


dock16

If you head back to Nexus Dashboard, your docker image should be stored with the latest tag:

dock15

Drop your comments, feedback, or suggestions below — or connect with me directly on Twitter @mlabouardy.

A version of this article originally published on Hackernoon. Re-published with the author's permission. 

Tags: Nexus OSS, Docker, Nexus Repository, Nexus 3.0, docker cloud, Packer, google cloud platform, Terraform, Product

Written by Mohamed Labouardy

Mohamed is Software Engineer/DevOps at InterCloud. Interested in AWS, Docker, Android, Go & ChatOps. A contributor to numerous open-source projects including Telegraf, DialogFlow, Docker ... He is currently writing a book on Serverless architecture in AWS, blogs at labouardy.com. You can reach him on Twitter @mlabouardy