How to Mine Bitcoin on Your Laptop Using Docker

Start mining Bitcoin in 5 minutes
John John (304)
20 minutes

Update 3/18/2021: This method no longer works, and I have no plans to update the guide because CPU mining is no longer a realistic option.

Docker and Raspberry Pi developer, Alex Ellis, has created a Docker image that will let you CPU mine Bitcoin in a few painless steps.

I’m this guide, I’ll show you how to mine Bitcoin on your laptop and start raking in those sweet, sweet fractions of a Bitcoin.

Posted in these interests:
h/bitcoin6 guides
h/docker12 guides
h/cryptocurrency5 guides
Create an account on NiceHash

Visit NiceHash and create a new account.

NiceHash is a marketplace for buying and selling compute power. In our case, we're going to sell our cpu power. When you create an account, NiceHash will provide a mining and deposit address. We'll use this address in the next step.

Start mining!

With a NiceHash account and Docker installed, we're ready to start mining.

docker service create --name miner \
    alexellis2/cpu-opt:2018-1-2 ./cpuminer \
    -a cryptonight \
    -o stratum+tcp:// \
    -u 3HSFKUagtL5Z6KFE7gAJ3i9YgWkmaKH6Zb.miner1

This command will create a docker service called miner that will start CPU mining. You'll need to substitute 3HSFKUagtL5Z6KFE7gAJ3i9YgWkmaKH6Zb with the address generated when you created your NiceHash account. And you can replace miner1 with the name of the host, if you want to track it.

If you're like me and you just want to mess around with this on your primary laptop, you'll probably not want to run the miner 100% of the time. I've create a few helper functions to start and stop mining.

First, create a file called start-mining and place it in /usr/local/bin or some other directory in your PATH:


docker service ps miner >/dev/null 2>&1

if [[ $? -eq 1 ]]; then
    docker service create \
        --name miner \
        alexellis2/cpu-opt:2018-1-2 \
        ./cpuminer \
            -a cryptonight \
            -o stratum+tcp:// \
            -u 3HSFKUagtL5Z6KFE7gAJ3i9YgWkmaKH6Zb.miner1
    docker service scale miner=1

docker service logs -f miner

And create an accompanying file called stop-mining.


docker service scale miner=0

Now we'll make these files executable:

chmod +x /usr/local/bin/start-mining
chmod +x /usr/local/bin/stop-mining

The start-mining script creates the miner service if it does not yet exist and ensures the service has one worker if it exists. It's counterpart, stop-mining, simply scales the worker to zero.

If you need to remove the service completely, you can run:

docker service rm miner

Probably not.

First of all, CPU mining has a very low hash rate (like 15 H/s, compared to ASIC miners getting 15 TH/s). You will make some money, but you probably won’t be able to quit your job.

Second, electricity costs money and mining uses electricity.

If you're curious about what kinds of profits are possible, use this profitability calculator.

John John (304)
10 minutes

If you've been experimenting with Docker, you've probably discovered that each time you restart a container the data is gone.