/hardhat-docker

A way to Dockerize your Hardhat solidity contracts

Primary LanguageJavaScriptMIT LicenseMIT

How To Dockerize Your Hardhat Solidity Contract

This is the final code from the article "How To Dockerize Your Hardhat Solidity Contract On Localhost". This is a base from the sample project provided by Hardhat but configured to use it with Docker.

If you got value from this and want to see more content from me, please follow/add me on:

Requirements

  • NVM or NodeJS v14.17.6
  • Docker v20.10.8
  • Yarn v1.22.10

Local Setup

  1. Make sure you have the correct version of NodeJS
nvm install;
  1. In dependencies
yarn install; # or just yarn
  1. Compile contract
yarn compile:local; # this is for our client/node.js file
  1. Build Docker image
docker build . -t hhdocker;
  1. Run Docker image
# !NOTE: Double check no other programs are using that port 8545
docker run -it -d -p 8545:8545 --name myhd hhdocker;
  1. Verify that container is running
docker logs myhd;
# Should see an output of wallet addresses and private keys
  1. Compile local contract within Docker
docker exec -it myhd yarn compile:local;
  1. Deploy local contract within Docker with custom task
docker exec -it myhd yarn deploy:local;
  1. Create .env file used for out client/node.js file
# Ugly version
echo "CONTRACT_ADDRESS=$(docker exec -it myhd cat .contract)\nWALLET_ADDRESS=$(docker exec -it myhd cat .wallet;)" > .env;

# Prettier version
# export CONTRACT_ADDRESS="$(docker exec -it myhd cat .contract)";
# export WALLET_ADDRESS="$(docker exec -it myhd cat .wallet)";
# echo "CONTRACT_ADDRESS=$CONTRACT_ADDRESS\nWALLET_ADDRESS=$WALLET_ADDRESS" > .env;
# unset CONTRACT_ADDRESS;
# unset WALLET_ADDRESS;
  1. Run our client
node client/node.js;

VoilĂ !

Don't forget to delete your container when you're done.

docker rm -f myhd;

Running Tests

Main test files can be found in /test.

yarn test:local;