【合约开发工具】solidity + hardhat 在 docker 容器中运行

前言

与传统的web开发有所不同,区块链应用的测试工具还并不完善,如何在本地方便的进行联调,打通前端、后端、合约甚至graph节点的数据流,对于开发者而言是一个值得探索的路。

在我们团队中,不同的工程师使用的不同的环境,若要让架构师或者测试工程师在本地轻松的构建环境,不可避免的,我们要借助容器来实现这一目的。

docker在诸多容器中,应用广泛且工具完整,因此我们选用 docker 作为容器。

本文介绍使用了 hardhat 集成的 solidity 的合约如何在 docker 中运行。

Dockerfile

如果想把整个工程作为创建为 docker image,我们需要在根目录创建并编写 dockerfile 与 .dockerignore

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

这里注意,如果是环境是 macos ,一定要忽略 yarn.lock ,里面有一个依赖 linux 装不了。具体是哪个忘记了。

# 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 指令改写

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

最后

可以参考菜鸟教程 https://www.runoob.com/docker/docker-hello-world.html
的相关教程,和容器进行交互。
常用指令如下:

  • 创建新 image
    docker build -t <image_name>:<tag> .
  • 通过镜像创建并运行一个对应指令的容器
    docker run -it --name <docker_name> <image_name>:<tag> <script command>
    例:
    docker run -it --name node dao:v1 start

猜你喜欢

转载自blog.csdn.net/weixin_43742184/article/details/120968794