Learn to deploy a private Geth based Ethereum Blockchain using Docker compose.
Learn to deploy a private Geth based Ethereum Blockchain using Docker compose.
Step 1: Let's create a private Ethereum Blockchain on Docker. We will use Docker Compose to build and manage it.
Step 2: We will use official Geth (Go Ethereum, a Go Implementation of Ethereum) client.
Step 3: First, let's pull Geth Docker image. Click on 'terminal' button and run docker pull command.
Step 4: The Docker Image by default runs Geth. So, by running the image with --help argument, we can check help options for Geth client.
Step 5: We can run it without any arguments if we want to run the client but that won't provide us with a complete setup required for local development.
Step 6: To create a complete setup, we need 3 nodes i.e. Bootnode, JSON-RPC endpoint and Miner.
Step 7: Bootnode: used for peer (other clients) discovery. By default, it listens on port 30303. When a new ethereum client needs to connect to bootnode before it can become part of the network.
Step 8: JSON-RPC endpoint: exposes JSON-RPC API over HTTP on port 8545 (default). This API is needed by wallets like Metamask or apps to connect to our Ethereum network.
Step 9: Miner: As the name suggests, it does 'mining' or creates new blocks on the blockchain. In real world implementation, you need multiple miners but here for our test implementation, one is sufficient.
Step 10: To save time and effort on writing all the files step by step, we have already created the template code in a GitHub repository. Go to terminal and clone the code repo.
Step 11: Switch to VS Code and open the repo directory. You will observe that we have 5 files in it. Ignore 'cheatsheet.txt' for now.
Step 12: genesis.json: Defines the genesis block and other initialization information. We are using 'clique' algorithm (proof-of-authority).
Step 13: genesis_pow.json: This is also a genesis file but for 'ethash' algorithm (proof-of-work). It is provided here just for your reference and won't be used in this tutorial.
Step 14: Dockerfile: To use the network for deploying smart contract and running transactions, we will need to have a pre-funded address and a genesis.json. This Dockefile will help us create our own Docker image with a genesis file and two accounts. The Dockerfile is commented. Please go through it.
Step 15: docker-compose.yaml: This file will help us in creating the network shown in the image below with all required parametes.
Step 16: Now, lets create our personal Docker image from Geth official image. Go to terminal, switch to cloned repo directory and run the docker build command.
Step 17: We want to see the output of the last step of build process. If you used build command without '--progress=plain' option, you won't see output. In that case, run this command.
Step 18: In the output of docker build, you should be able to see similar output as shown in the image. These are the public keys of two accounts created created for our use.
Step 19: The image is ready. Now, lets run docker-compose to bring up the setup. Switch to terminal and run docker-compose command.
Step 20: We have run docker compose in foreground, so let's open another terminal and check the listening port on our localhost.
Step 21: Let's us query JSON-RPC HTTP endpoint to check all created accounts. Open 'cheatsheet.txt' and run the first command on terminal.
Step 22: Similarly, we can use second command provided in that file to q uery balance of the created accounts.
Step 23: We successfully completed private Ethereum blockchain deployment.