区块链我爱编程

比特币开发-1 Docker 、比特币测试网络

2018-04-06  本文已影响90人  DavieKong

安装Docker环境

什么是Docker?

Docker 属于 Linux 容器的一种封装,提供简单易用的容器使用接口。它是目前最流行的 Linux 容器解决方案。
Docker 将应用程序与该程序的依赖,打包在一个文件里面。运行这个文件,就会生成一个虚拟容器。程序在这个虚拟容器里运行,就好像在真实的物理机上运行一样。有了 Docker,就不用担心环境问题。

总体来说,Docker 的接口相当简单,用户可以方便地创建和使用容器,把自己的应用放入容器。容器还可以进行版本管理、复制、分享、修改,就像管理普通的代码一样。
Docker 的主要用途,目前有三大类。

(1)提供一次性的环境。比如,本地测试他人的软件、持续集成的时候提供单元测试和构建的环境。

(2)提供弹性的云服务。因为 Docker 容器可以随开随关,很适合动态扩容和缩容。

(3)组建微服务架构。通过多个容器,一台机器可以跑多个服务,因此在本机就可以模拟出微服务架构。

安装Docker
本文以ubuntu系统为例

Install using the repository

Before you install Docker CE for the first time on a new host machine, you need to set up the Docker repository. Afterward, you can install and update Docker from the repository.

Set up the repository

  1. Update the apt package index:

    $ sudo apt-get update
    
    
  2. Install packages to allow apt to use a repository over HTTPS:

    $ sudo apt-get install \
        apt-transport-https \
        ca-certificates \
        curl \
        software-properties-common
    
    
  3. Add Docker’s official GPG key:

    $ curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -
    
    

    Verify that you now have the key with the fingerprint 9DC8 5822 9FC7 DD38 854A E2D8 8D81 803C 0EBF CD88, by searching for the last 8 characters of the fingerprint.

    $ sudo apt-key fingerprint 0EBFCD88
    
    pub   4096R/0EBFCD88 2017-02-22
          Key fingerprint = 9DC8 5822 9FC7 DD38 854A  E2D8 8D81 803C 0EBF CD88
    uid                  Docker Release (CE deb) <docker@docker.com>
    sub   4096R/F273FCD8 2017-02-22
    
    
  4. Use the following command to set up the stable repository. You always need the stable repository, even if you want to install builds from the edge or test repositories as well. To add the edge or test repository, add the word edge or test (or both) after the word stable in the commands below.

    Note: The lsb_release -cs sub-command below returns the name of your Ubuntu distribution, such as xenial. Sometimes, in a distribution like Linux Mint, you might need to change $(lsb_release -cs) to your parent Ubuntu distribution. For example, if you are using Linux Mint Rafaela, you could use trusty.

    • x86_64 / amd64
    • armhf
    • IBM Power (ppc64le)
    • IBM Z (s390x)
    $ sudo add-apt-repository \
       "deb [arch=amd64] https://download.docker.com/linux/ubuntu \
       $(lsb_release -cs) \
       stable"
    
    

    Note: Starting with Docker 17.06, stable releases are also pushed to the edge and test repositories.

    Learn about stable and edge channels.

Install Docker CE

  1. Update the apt package index.

    $ sudo apt-get update
    
    
  2. Install the latest version of Docker CE, or go to the next step to install a specific version:

    $ sudo apt-get install docker-ce
    
    

    Got multiple Docker repositories?

    If you have multiple Docker repositories enabled, installing or updating without specifying a version in the apt-get install or apt-get update command always installs the highest possible version, which may not be appropriate for your stability needs.

  3. To install a specific version of Docker CE, list the available versions in the repo, then select and install:

    a. List the versions available in your repo:

    $ apt-cache madison docker-ce
    
    docker-ce | 18.03.0~ce-0~ubuntu | https://download.docker.com/linux/ubuntu xenial/stable amd64 Packages
    
    

    b. Install a specific version by its fully qualified package name, which is the package name (docker-ce) plus the version string (2nd column) up to the first hyphen, separated by a an equals sign (=), for example, docker-ce=18.03.0.ce.

    $ sudo apt-get install docker-ce=<VERSION>
    
    

    The Docker daemon starts automatically.

  4. Verify that Docker CE is installed correctly by running the hello-world image.

    $ sudo docker run hello-world
    
    

windows系统和Mac系统下载安装包点击安装就可以了。

image文件

Docker 把应用程序及其依赖,打包在 image 文件里面。只有通过这个文件,才能生成 Docker 容器。image 文件可以看作是容器的模板。Docker 根据 image 文件生成容器的实例。同一个 image 文件,可以生成多个同时运行的容器实例。

image 是二进制文件。实际开发中,一个 image 文件往往通过继承另一个 image 文件,加上一些个性化设置而生成。举例来说,你可以在 Ubuntu 的 image 基础上,往里面加入 Apache 服务器,形成你的 image。

# 列出本机的所有 image 文件。
$ docker image ls

# 删除 image 文件
$ docker image rm [imageName]

获取iamge

docker image pull library/hello-world

查看本机的image

docker image ls

运行image

docker container run hello-world

容器文件

image 文件生成的容器实例,本身也是一个文件,称为容器文件。

列出本机正在运行的容器

docker container ls

列出本机所有容器,包括终止运行的容器

docker container ls --all

移除容器

docker container rm [containerID]

安装比特币测试网络

下载比特币测试网络镜像

sudo docker pull freewill/bitcoin-testnet-box

如果出现错误:

Unable to find image 'hello-world:latest' locally
docker: Error response from daemon: Get https://registry-1.docker.io/v2/library/hello-world/manifests/latest: net/http: TLS handshake timeout.
See 'docker run --help'.

解决办法:

使用国内的Docker仓库daocloud:

$ echo "DOCKER_OPTS=\"\$DOCKER_OPTS --registry-mirror=http://f2d6cb40.m.daocloud.io\"" 
sudo tee -a /etc/default/docker
$ sudo service docker restart

运行Docker镜像

sudo docker run -it -p 19001:19001 -p 19011:19011 freewil/bitcoin-testnet-box

上述命令中的19001 和 19011是配置给两个节点提供RPC服务的端口。
-t:在新容器内指定一个伪终端或终端。

-i:允许你对容器内的标准输入 (STDIN) 进行交互。

启动比特币测试网络

make start

启动成功后,将在本地模拟运行两个比特币测试钱包节点,组成一个私有范围的比特币测试网络。

查看节点信息

make getinfo
bitcoin-cli -datadir=1  getinfo
{
  "version": 130200,
  "protocolversion": 70015,
  "walletversion": 130000,
  "balance": 0.00000000,
  "blocks": 0,
  "timeoffset": 0,
  "connections": 1,
  "proxy": "",
  "difficulty": 4.656542373906925e-10,
  "testnet": false,
  "keypoololdest": 1523007398,
  "keypoolsize": 100,
  "paytxfee": 0.00000000,
  "relayfee": 0.00001000,
  "errors": ""
}
bitcoin-cli -datadir=2  getinfo
{
  "version": 130200,
  "protocolversion": 70015,
  "walletversion": 130000,
  "balance": 0.00000000,
  "blocks": 0,
  "timeoffset": 0,
  "connections": 1,
  "proxy": "",
  "difficulty": 4.656542373906925e-10,
  "testnet": false,
  "keypoololdest": 1523007399,
  "keypoolsize": 100,
  "paytxfee": 0.00000000,
  "relayfee": 0.00001000,
  "errors": ""
}

产生一个区块

make generate

产生100个区块

make generate BLOCKS=100

查看节点状态

bitcoin-cli -datadir=1  getinfo
{
  "version": 130200,
  "protocolversion": 70015,
  "walletversion": 130000,
  "balance": 5050.00000000,
  "blocks": 201,
  "timeoffset": 0,
  "connections": 1,
  "proxy": "",
  "difficulty": 4.656542373906925e-10,
  "testnet": false,
  "keypoololdest": 1523009413,
  "keypoolsize": 100,
  "paytxfee": 0.00000000,
  "relayfee": 0.00001000,
  "errors": ""
}

可以看到钱包节点中有5050个比特币

查看钱包地址

比特币参考客户端维护了一个地址池,地址池的大小可以用 getinfo 命令keypoolsize 参数获取。这些地址是自动生成的,可以被用作公开接收地址或零钱地址。使用 getnewaddress 命令可以获得其中的一个地址:

bitcoin-cli -datadir=2 getnewaddress
mg1xSmdUdwGpwQTYaKLRnoG6pN1rCv6NhM

或者

make address2
bitcoin-cli -datadir=2  getnewaddress
mzHTLmnduaHZrTWpt2zJXrRSiqX1aoPqek

查看地址私钥

bitcoin-cli -datadir2  dumpprivkey 'mg1xSmdUdwGpwQTYaKLRnoG6pN1rCv6NhM'

给钱包2转账

make sendfrom1   ADDRESS=mg1xSmdUdwGpwQTYaKLRnoG6pN1rCv6NhM  AMOUNT=10

注意这里测试地址以m开头,和正式地址以1或3开头是有区别的。

确认交易

产生10个区块,让上面的交易得到足够的确认

make generate BLOCKS=10

然后查看节点二

bitcoin-cli -datadir=2 getinfo

可以看到 里面有10个比特币

tester@965cf497fb58:~/bitcoin-testnet-box$ bitcoin-cli -datadir=2 getinfo
{
  "version": 130200,
  "protocolversion": 70015,
  "walletversion": 130000,
  "balance": 10.00000000,
  "blocks": 210,
  "timeoffset": 0,
  "connections": 1,
  "proxy": "",
  "difficulty": 4.656542373906925e-10,
  "testnet": false,
  "keypoololdest": 1523019944,
  "keypoolsize": 100,
  "paytxfee": 0.00000000,
  "relayfee": 0.00001000,
  "errors": ""
}

查看第一个节点

tester@965cf497fb58:~/bitcoin-testnet-box$ bitcoin-cli -datadir=1 getinfo
{
  "version": 130200,
  "protocolversion": 70015,
  "walletversion": 130000,
  "balance": 5489.99996160,
  "blocks": 210,
  "timeoffset": 0,
  "connections": 1,
  "proxy": "",
  "difficulty": 4.656542373906925e-10,
  "testnet": false,
  "keypoololdest": 1523019944,
  "keypoolsize": 100,
  "paytxfee": 0.00000000,
  "relayfee": 0.00001000,
  "errors": ""
}
tester@965cf497fb58:~/bitcoin-testnet-box$

发现 balance 由 5500 变成了 5489.99996160,转账给节点2 10BTC,其余部分 0.0000384 就是支付给矿工记账的手续费。

上一篇下一篇

猜你喜欢

热点阅读