# Running Pchain in Docker

One of the quickest ways to get Pchain up and running on your machine is by using Docker.

## Install Docker

### **Get docker on Ubuntu**

If your PC's operating system is Ubuntu, you can get docker as follows:

```
sudo apt-get remove docker docker-engine docker-ce docker.io
sudo apt-get install  docker
sudo apt-get install -y docker.io
sudo apt-get install  docker-registry
sudo systemctl start docker
sudo systemctl enable docker
```

To see more details, you can see the [Docker Official install Guide (Ubuntu)](https://docs.docker.com/install/linux/docker-ce/ubuntu/).

*Note:*\
\&#xNAN;*To install Docker CE, you need the 64-bit version of one of these Ubuntu versions:*

* *Bionic 18.04 (LTS)*
* *Xenial 16.04 (LTS)*
* *Trusty 14.04 (LTS)*

### **Get docker on Windows and Mac OS**

You can find the install package on the [Docker Official website](https://www.docker.com/get-started).\
If you want to run docker on windows, ***DO NOT*** use windows containers but use Linux containers. And make sure the *Virtualization Technology* is enabled.\
To see more details, you can see the [Docker Official install Guide (Windows)](https://docs.docker.com/docker-for-windows/install/) and [Docker Official install Guide (Mac OS)](https://docs.docker.com/docker-for-mac/install/).

*Running docker requires a 64-bit operating system, win7 or higher, and supports " Hardware Virtualization Technology", and "virtualization" is available.*

## Build Pchain Dockerfile

To Run Pchain on dockers, you will make a Pchain docker image from `Dockerfile` as follows from within a pchain folder:

```
cd pchain
docker build -t pchian . 
```

You can check the image status by:

```
& docker images
```

If the Pchain image has been built correctly, you can find an image called `pchain` and the `Tag` is `latest`.

## Start the containers

When the Pchain's docker image has been built, you can start Pchian in docker:

```
& docker run -it pchain
```

And you can see the containers status:

```
$ docker ps -a
```

### Single-node on docker

To run a single-node Pchain in docker, execute `docker run -it pchain`. And create accounts with a specified balance, will generate eth\_genesis.json:

```
./pchain --datadir pchainData init_eth_genesis "{10000000000000000000000000000000000, 100}"
```

Generate the Pchain genesis.json with epoch/reward scheme parameters:

```
./pchain --datadir pchainData init pchainData/pchain/eth_genesis.json
```

to start the node:

```
./run.sh
```

### Multi-node on docker

You can deploy multiple nodes through docker in order to test jobs, smart contracts and child chains.

**Add an ordinary node**

Start another docker container, build a fresh node('B') but don't start it (not run the `./run.sh`).\
Here are the steps to make the new node ('B') join the existing node ('A'):

* stop Pchain on A
* add the peer entry,
  * add B's ip:port(say 172.17.0.3:46656) to A's `config.toml`,
  * add A's ip:port(say 172.17.0.2:46656) to B's `config.toml`. Then, A's config.toml should look like:

```
#This is a TOML config file.
moniker = "anonymous"
node_laddr = "tcp://0.0.0.0:46656"
seeds = "172.17.0.3:46656"
fast_sync = true
db_backend = "leveldb"
log_level = "notice"
```

*Note: For more information, see* [*toml official guide*](https://github.com/toml-lang/toml)*.*

* Copy A's `genesis.json` and `eth_genesis.json` to B, make their genesis.json keep the same, therefore the validators are the same between A and B.
* Add A's `enode` to B, you can get A's `enode` by RPC command as follows:

```
curl -H "Content-Type:application/json" -X POST -d '{"method": "admin_nodeInfo", "params": [],"id":1}' "http://x.x.x.x/pchain"
```

* Run Pchain on A and B (order is not relevant). After they are connected, B should start to synchronize blocks from A, and they will have the same blockchain behaviors when synchronization is complete.

**Add a validator node**

In order to add a validator node, there must be existing validators.&#x20;

* Start a new container from Pchain image, add a new node ('C') to the existing network but don't start it (not run the `./run.sh`).
* Pick one validator (node 'A'), add C's pub-key (the long hex-string of `pub_key` part in `/path/to/priv_validator.json`) to A's `validators` part in A's `/path/to/genesis.json`. then A's `validators"` part in `genesis.json` should look like:

```
"validators": [
                ...
                {
                		"eth_account": "0x7eff122b94897ea5b0e2a9abf47b86337fafebdc"
                        "amount": 10,
                        "name": "",
                        "pub_key": [
                                1,
                                "AE7AF8281D31B9A2CB0A9BC75D925CAD7E8DD6781601EF1519E0B0E01612F1FA"
                        ]
                },
                { #here is C's pub-key part
                        "eth_account": "0x32ef122b94897ea5b0e2a9abf4cda6337fafebdc"
                        "amount": 10,	#vote power
                        "name": "",   #name, could be empty
                        "pub_key": [  #pub-key part
                                1,
                                "E94937710077B38C2A1334B89B394D61CFBFEE732A54F9358A73E36ED0827B9A" #copy from C's priv_validator.json
                        ]
                },
                ...
        ]
```

{% hint style="warning" %}
*Important: Make sure C's amount is smaller than 1/2 of the sum of all other validators' amount in order to maintain fault-tolerant consensus*
{% endhint %}

* Copy A's `genesis.json` and `eth_genesis.json` to all other nodes within the network, including C.
* Stop all nodes within the network.
* Start all nodes with C the last one to start.
* Extension: This should work for batch-addition of new validators.

**Multi-node automated deployment**

`openssh-server` `net-tools` `curl` are installed in images. And all operation commands are contained in `add_node.sh`. You might execute this command step by step but not in one time.<br>


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://pliangroup.gitbook.io/plian/master/using-the-blockchain/validator-node-guides/alternate-installation-instructions/running-pchain-in-docker.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
