Our website is made possible by displaying online advertisements to our visitors. Please consider supporting us by disabling your ad blocker.

Mine Bitcoin And Other Cryptocurrency Coins With A CPU Miner And Docker

TwitterFacebookRedditLinkedInHacker News

So you’ve been doing a lot of research around the blockchain and the various cryptocurrencies. Don’t worry, I’ve been doing a lot of the same research. There is so much in the news and on social media around this subject, it would be a good idea to educate yourself.

When it comes to cryptocurrencies, there are a lot of people trying to mine it rather than buy it. Depending on the type of currency, it could be near impossible to mine or very easy. Take Bitcoin for example. Bitcoin is one of the most difficult coins to mine, while others such as DigiByte are much easier. That’s not to say the easier coins won’t become more difficult in the future.

So how do you get started with mining?

I use a Mac. If you’re a Mac owner and you try to find a mining tool online, you’re going to struggle because most are for Windows or Linux. We’re going to see how to leverage Docker to mine via a Linux container on any platform.

When it comes to mining, there is CPU mining and GPU mining. Most will tell you that GPU mining will yield better results, but at a higher cost. It will put more stress on your machine and your electrical bill. We’re going to focus on CPU mining.

Creating a Docker Image with a CPU Miner

The most popular CPU miner tool is called cpuminer and there are several variations of it. The maintained original version can use the scrypt and sha256 algorithms which is useful for Bitcoin, Litecoin, and any other coins that can be mined with those algorithms.

The GitHub repository for cpuminer has a Dockerfile file included for easily creating a Docker image:

FROM            ubuntu:14.04
MAINTAINER      Guillaume J. Charmes <guillaume@charmes.net>

RUN             apt-get update -qq && \
                apt-get install -qqy automake libcurl4-openssl-dev git make

RUN             git clone https://github.com/pooler/cpuminer

RUN             cd cpuminer && \
                ./autogen.sh && \
                ./configure CFLAGS="-O3" && \
                make

WORKDIR         /cpuminer
ENTRYPOINT      ["./minerd"]

The configuration above was taken from the Dockerfile file. To build it into a useable image, execute the following with the Docker CLI:

docker build -t cpuminer ./path/to/Dockerfile

Like I had previously mentioned, the maintained original cpuminer only does two algorithms. This can limit your productivity if you’re trying to mine a specific coin. Instead, we may want to check out the cpuminer-multi project which supports a significant amount of algorithms.

The cpuminer-multi project has a Dockerfile file in the GitHub repository that looks like the following:

FROM		ubuntu:14.04
MAINTAINER	Tanguy Pruvot <tanguy.pruvot@gmail.com>

RUN		apt-get update -qq

RUN		apt-get install -qy automake autoconf pkg-config libcurl4-openssl-dev libssl-dev libjansson-dev libgmp-dev make g++ git

RUN		git clone https://github.com/tpruvot/cpuminer-multi -b linux

RUN		cd cpuminer-multi && ./build.sh

WORKDIR		/cpuminer-multi
ENTRYPOINT	["./cpuminer"]

The above configuration is very similar to that of the original project. To build it, execute the following from the Docker CLI:

docker build -t cpuminer-multi ./path/to/Dockerfile

Regardless of the cpuminer variation that you plan on using, the build time may take a few minutes. The build process is downloading a fair amount of dependencies and building the project from source.

Mining a Pool with a Docker Container

Now that we have a CPU mining tool ready to go via Docker and containerization, we have to choose a pool to join and the type of coin that we wish to mine.

For this example, we’re going to mine some DigiByte because we actually stand a chance at the moment versus Bitcoin. To find some pools, visit the DigiHash website.

DigiHash DigiByte Mining Pool

We’re going to use the qubit algorithm, for this reason we’ll need to use the cpuminer-multi image that we had created. From the CLI, execute the following:

docker run -d cpuminer-multi --algo qubit --url stratum+tcp://digihash.co:3012 --user D9Ms9hnm32q9nceN2b9jNshuZhWcobrmQm --pass anything --threads 3

Just so we’re on the same page, the D9Ms9hnm32q9nceN2b9jNshuZhWcobrmQm username in the above command is my public DGB address where mined coins will be sent. You’ll want to replace it with your own address. I have a quad-core CPU, so by choosing to use three threads, I’m using three of my four cores.

To see the progress of your mining, execute the following to obtain your container id:

docker ps

You should see results that look like the following:

CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS               NAMES
6dd40d6130fe        cpuminer-multi      "./cpuminer --algo..."   4 seconds ago       Up 2 seconds                            gallant_gates

With the container id, execute the following to see a live stream of your logs:

docker logs -f 6dd40d6130fe

Remember we’re running our container in detached mode, meaning it will be running as a container in the background. You don’t always need to be tracking the logs.

Most of the logs will spit out information that looks like the following:

[2017-12-20 18:12:25] Starting Stratum on stratum+tcp://digihash.co:3012
[2017-12-20 18:12:25] 3 miner threads started, using 'qubit' algorithm.
[2017-12-20 18:12:29] Stratum difficulty set to 0.01
[2017-12-20 18:12:29] qubit block 5761269, diff 190162.631
[2017-12-20 18:12:31] qubit block 5761270, diff 182120.209
[2017-12-20 18:12:31] CPU #2: 76.54 kH/s
[2017-12-20 18:12:31] CPU #1: 76.44 kH/s
[2017-12-20 18:12:31] CPU #0: 76.50 kH/s
[2017-12-20 18:12:36] qubit block 5761271, diff 172820.491
[2017-12-20 18:12:42] qubit block 5761272, diff 169053.910

This information lets us know that we’re successfully mining, but it doesn’t mean that we’ve found any coins. Occasionally, you’ll see something that looks like the following:

[2017-12-20 18:15:26] accepted: 1/1 (diff 0.116), 207.60 kH/s yes!

This does not mean that you’ve found a coin. It means that you’ve found a share in the pool. You’ll need to find quite a bit of these shares before you end up with a payout.

Conclusion

You just saw how to use Docker to mine for cryptocurrency on your computer. By using Docker, we don’t have to install a miner directly on our host and we don’t have to burden ourselves with trying to find or build for a specific platform. Within our Docker image, we have a Linux build that is ready to be deployed and used as a container at any given time.

Keep in mind that mining any kind of cryptocurrency coin on your computer is difficult, expensive, and probably taxing on your machine. People spend a lot of money on specialized mining hardware. However, play around with the concept, and you never know, you might get lucky.

If you end up striking gold in the form of cryptocurrency, consider donating some to me:

  • Bitcoin (BTC): bc1qths8qh44475883450mf58p3twxe7gm9w5ws4ht
  • Litecoin (LTC): LQxDRKXfx1qE9XNjCzfbQeeKHbQcvxHpGf
  • DigiByte (DGB): D9Ms9hnm32q9nceN2b9jNshuZhWcobrmQm

If you know of a better method to mine using Docker than what I’ve discussed above, share your thoughts in the comments. Many subscribers would benefit from the education on the subject.

Nic Raboy

Nic Raboy

Nic Raboy is an advocate of modern web and mobile development technologies. He has experience in C#, JavaScript, Golang and a variety of frameworks such as Angular, NativeScript, and Unity. Nic writes about his development experiences related to making web and mobile development easier to understand.