[Contract development tools] solidity + hardhat run in a docker container

foreword

Different from traditional web development, the testing tools for blockchain applications are still not perfect. How to conveniently carry out joint debugging locally and get through the data flow of front-end, back-end, contracts and even graph nodes is a key issue for developers. A path worth exploring.

In our team, different engineers use different environments. If we want architects or test engineers to easily build environments locally, we must use containers to achieve this goal.

Docker is widely used in many containers and has complete tools, so we choose docker as the container.

This article introduces how to use the solidity contract integrated by hardhat to run in docker.

Dockerfile

If we want to create the entire project as a docker image, we need to create and write dockerfile and .dockerignore in the root directory

Dockerfile

FROM node:16.9 as builder
WORKDIR /app
COPY package.json .
COPY yarn.lock .
RUN yarn install

FROM node:16.9
WORKDIR /app
COPY --from=builder /app/ /app/
COPY . .
EXPOSE 8545
ENTRYPOINT ["yarn"]

.dockerignore

Note here, if the environment is macos, you must ignore yarn.lock, there is a dependency in linux that cannot be installed. Which one is forgotten.

# Logs
logs
*.log
npm-debug.log*
yarn-debug.log*
yarn-error.log*
lerna-debug.log*

# Diagnostic reports (https://nodejs.org/api/report.html)
report.[0-9]*.[0-9]*.[0-9]*.[0-9]*.json

# Runtime data
pids
*.pid
*.seed
*.pid.lock

# Directory for instrumented libs generated by jscoverage/JSCover
lib-cov

# Coverage directory used by tools like istanbul
coverage
*.lcov

# nyc test coverage
.nyc_output

# Grunt intermediate storage (https://gruntjs.com/creating-plugins#storing-task-files)
.grunt

# Bower dependency directory (https://bower.io/)
bower_components

# node-waf configuration
.lock-wscript

# Compiled binary addons (https://nodejs.org/api/addons.html)
build/Release

# Dependency directories
node_modules/
jspm_packages/

# TypeScript v1 declaration files
typings/

# TypeScript cache
*.tsbuildinfo

# Optional npm cache directory
.npm

# Optional eslint cache
.eslintcache

# Microbundle cache
.rpt2_cache/
.rts2_cache_cjs/
.rts2_cache_es/
.rts2_cache_umd/

# Optional REPL history
.node_repl_history

# Output of 'npm pack'
*.tgz

# Yarn Integrity file
.yarn-integrity

# dotenv environment variables file
.env
.env.test

# parcel-bundler cache (https://parceljs.org/)
.cache

# Next.js build output
.next

# Nuxt.js build / generate output
.nuxt
dist

# Gatsby files
.cache/
# Comment in the public line in if your project uses Gatsby and *not* Next.js
# https://nextjs.org/blog/next-9-1#public-directory-support
# public

# vuepress build output
.vuepress/dist

# Serverless directories
.serverless/

# FuseBox cache
.fusebox/

# DynamoDB Local files
.dynamodb/

# TernJS port file
.tern-port

# hardhat
artifacts/
cache/
deployments/
.idea/
.openzeppelin/

# macos
yarn.lock
.DS_Store

package.json directive override

  "scripts": {
    
    
    "start": "hardhat node",
    "dev": "hardhat node",
    "build": "hardhat compile",
    "test": "hardhat test",
    "deploy": "hardhat deploy",
    "lint": "eslint .",
    "lint:fix": "eslint --fix ."
  }

at last

You can refer to the related tutorials of the rookie tutorial https://www.runoob.com/docker/docker-hello-world.html
to interact with the container.
Commonly used commands are as follows:

  • create new image
    docker build -t <image_name>:<tag> .
  • Create and run a container corresponding to the instruction through the image
    docker run -it --name <docker_name> <image_name>:<tag> <script command>
    Example:
    docker run -it --name node dao:v1 start

Guess you like

Origin blog.csdn.net/weixin_43742184/article/details/120968794