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

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:

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:

Just so we’re on the same page, the DJMczFzdq2NeBPhBxrFbMJxg98mgWeRWyo 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:

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): 15sf3hLC4JqdBzBxSZ6PWFhbVy85Wo9Whd

Litecoin (LTC): LL89GjA31iBGx9CQV3jpshv5XxoP9pSpLW

DigiByte (DGB): DJMczFzdq2NeBPhBxrFbMJxg98mgWeRWyo

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 is an advocate of modern web and mobile development technologies. He has experience in Java, JavaScript, Golang and a variety of frameworks such as Angular, NativeScript, and Apache Cordova. Nic writes about his development experiences related to making web and mobile development easier to understand.