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:
- NVM or NodeJS
v14.17.6
- Docker
v20.10.8
- Yarn
v1.22.10
- Make sure you have the correct version of NodeJS
nvm install;
- In dependencies
yarn install; # or just yarn
- Compile contract
yarn compile:local; # this is for our client/node.js file
- Build Docker image
docker build . -t hhdocker;
- Run Docker image
# !NOTE: Double check no other programs are using that port 8545
docker run -it -d -p 8545:8545 --name myhd hhdocker;
- Verify that container is running
docker logs myhd;
# Should see an output of wallet addresses and private keys
- Compile local contract within Docker
docker exec -it myhd yarn compile:local;
- Deploy local contract within Docker with custom task
docker exec -it myhd yarn deploy:local;
- Create
.env
file used for outclient/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;
- Run our client
node client/node.js;
VoilĂ !
Don't forget to delete your container when you're done.
docker rm -f myhd;
Main test files can be found in /test
.
yarn test:local;