Saturday 27 November 2021

Mining Monero coin using Docker and K8s!

 Following on from the previous blog post where we installed and configured Docker Desktop and enabled Kubernetes (K8s), I thought I'd play around with mining a digital currency.  There are so many digital currencies around I went for one that doesn't require a massive computer to do this, so I went for Monero which can be mined using a Raspberry Pi.

Now mining Monero isn't going to make you rich unless you've got a room full of hefty powered computers but it can help you understand a bit about how Docker and Kubernetes work and make a penny or two in the process.

Creating the Dockerfile

To do this we are going to create our own Docker container which I've based on the Alpine image of linux.  The main reason for this is that it is small, lightweight and perfect for what we need.
Create a new directory somewhere on your machine and create a new file called dockerfile, with no extension.
Open the file in your favourite text editor and add the following line:

FROM alpine:latest

This will tell Docker when we build our container that we want to use the latest version of the Alpine image on the Docker hub, it's under 3mb - not bad for an operating system!

For this container instead of downloading the latest version to make it a bit more of a challenge and to show what can be achieved in a Docker file we're going to clone the git repository and build the code.

To do this, now we need to update the package manager so that it is up to date and install some of the tools that we need, so add the following to the file:

RUN apk update
RUN apk add git build-base cmake libuv-dev libressl-dev hwloc-dev 

Great, at this point our image will have Alpine linux along with some tools installed, now lets clone the git repo of the miner.

RUN git clone

When we build the image this will use git which was installed earlier, then connect to GitHub and download all of the files.
To build the code I followed the instructions in the xmrig documentation appending the RUN command.

RUN mkdir build
RUN cmake ..
RUN make -j$(nproc)

Now we're almost done, at this point when we build the image it will download Alpine linux, install some tools, download the code from the GitHub repo and then build it.

Monero Wallet

To be able to mine any digital currency you need to have somewhere to store it, this is called a digital wallet.
If you don't have a Monero wallet browse to their website and download the GUI Wallet.  Install this (you may have add approval rules for your anti-virus).
Once installed run the program and follow the steps, select a Simple Mode and Create a New Wallet.  This will ask you for a name and a location, it will also have a mnemonic seed.  It is very important that this is stored somewhere safe as without it you won't be able to use your wallet!  Have a printed out copy as well as storing it in a password safe is a good idea.  Finally create a (secure password) and store that in your password safe.
Once you've done this Monero will need to synchronise which will take a few minutes (don't panic).
Now you can click on Account and finally click the icon to copy the Primary Account address, this is the ID of your Monero wallet.

Mining Pools

To increase your chances of earning money from a digital currency people group together into a pool, these will then give you a percentage of the revenue generated depending on how much your computer helped.
I've used a pool called Monero Ocean but there are other options available which can be found by using the xmrig configuration wizard.

The final step is to add the line to the Dockerfile to start the miner:
CMD ./xmrig -o -u <Wallet ID>
Replacing <Wallet ID> with the one copied from the Monero GUI Wallet.

Your complete Dockerfile should look like this:

FROM alpine:latest
RUN apk update
RUN apk add git build-base cmake libuv-dev libressl-dev hwloc-dev

RUN git clone

RUN mkdir build
RUN cmake ..
RUN make -j$(nproc)

CMD ./xmrig -o -u <Wallet ID>

Build the Docker Image

To build the image simply open a command prompt and set the directory to be where you created the dockerfile.

docker build -t monero .

This will build the image and give it a tag of the name monero.  You can of course change this to be anything you like.

Once it is built you can view the image by typing

docker images

It should look like this:

Now we can start the container by running:

docker run -it monero

The switch -it will start the container interactively, allowing us to see the output.  It should look like this:

Wahoo, we've built a container from scratch that will download the code from github, build it and then run the code to generate Monero.


To take this to the next stage let's deploy this to our Docker Desktop Kubernetes installation.

To deploy this to K8s we need a yaml file, this file describes to k8s what it needs to deploy our container.
Create a new file (I called mine monero.yaml) in the same location as the dockerfile and add the following:

apiVersion: apps/v1
kind: Deployment
  name: monero
  namespace: monero
  replicas: 2
      app: monero
        app: monero
        - name: monero
          image: monero:latest
          imagePullPolicy: IfNotPresent
              memory: 4096Mi
              cpu: "1"
              memory: 4096Mi
              cpu: "0.5"

Some things to point out:

  replicas: 2

This is the number of instances we want in our cluster.  If you are running this locally you may be limited by the amount of memory your computer has.

          image: monero:latest
This is the name of the image we build locally, if you called this something different it will need to be updated here.
          imagePullPolicy: IfNotPresent
Because our image is local and not publicly available we need it to check locally.

At the bottom of the file are the resources, these don't need to be specified but I've found that it needs around 4GB of RAM, if it doesn't have enough the container will be killed and restarted.

Now we're almost there, before we can deploy this we need to create the namespace for our deployment, as specified in the file this is called monero.

Open a PowerShell window and type:

kubectl config current-context

This should state:

If it doesn't list the contexts:
kubectl config get-contexts

Then select your cluster:
kubectl config use-context docker-desktop

To create the namespace, which we called monero type:
kubectl create namespace monero

Now to deploy our image to our cluster creating 2 replicas type:
kubectl apply -f monero.yaml

Which should respond
deployment.apps/monero configured

To view the status you can describe the pod with this command:
kubectl describe pod --namespace monero

If you've installed the dashboard (see my previous post) you should be able to see what is happening with a GUI.  Run the command:
kubectl proxy

Enter the token to login which can be obtained using this command:
kubectl -n kubernetes-dashboard get secret $(kubectl -n kubernetes-dashboard get sa/admin-user -o jsonpath="{.secrets[0].name}") -o go-template="{{.data.token | base64decode}}"

Make sure you select the monero namespace and you can see the pods that are running and mining!

Viewing your earnings!

This isn't going to make you rich but you can view the amount of money you have earned on the Monero Ocean website.
Simply paste in your wallet ID and it will provide a run down of how much you have contributed and earned.  Don't expect to earn more than a few pence per day!