From 1b8588241bdba6c87ec11c8d97a987552b9ef314 Mon Sep 17 00:00:00 2001
From: kaichao <kaichaosuna@gmail.com>
Date: Fri, 15 May 2020 12:27:30 +0800
Subject: [PATCH] Use docker to setup a local dev chain  (#41)

* use docker to setup a local dev chain.

* install docker link

* update command

* Update README.md

Co-authored-by: joe petrowski <25483142+joepetrowski@users.noreply.github.com>

* more commands to start or check a node.

* purge chain in docker

Co-authored-by: joe petrowski <25483142+joepetrowski@users.noreply.github.com>
---
 .gitignore            |  8 +++++++-
 README.md             | 26 ++++++++++++++++++++++++++
 docker-compose.yml    | 17 +++++++++++++++++
 scripts/docker_run.sh | 10 ++++++++++
 4 files changed, 60 insertions(+), 1 deletion(-)
 create mode 100644 docker-compose.yml
 create mode 100755 scripts/docker_run.sh

diff --git a/.gitignore b/.gitignore
index 4969d2e6f..dee6ee199 100644
--- a/.gitignore
+++ b/.gitignore
@@ -4,4 +4,10 @@
 # These are backup files generated by rustfmt
 **/*.rs.bk
 
-.DS_Store
\ No newline at end of file
+.DS_Store
+
+# The cache for docker container dependency
+.cargo
+
+# The cache for chain data in container
+.local
\ No newline at end of file
diff --git a/README.md b/README.md
index d3502ccb4..475444ad2 100644
--- a/README.md
+++ b/README.md
@@ -80,6 +80,32 @@ cargo run -- \
 
 Additional CLI usage options are available and may be shown by running `cargo run -- --help`.
 
+### Run in Docker
+
+Install [Docker](https://docs.docker.com/get-docker/) first, then run the following command to start a single node development chain. This command will firstly comipile your code, then start a local dev netork.
+
+```bash
+./scripts/docker_run.sh
+```
+
+If you just want to run the compiled binary,
+
+```bash
+./scripts/docker_run.sh ./target/release/node-template --dev --ws-external
+```
+
+Other commands are similar. Let's try purge the local dev chain here:
+
+```bash
+./scripts/docker_run.sh ./target/release/node-template purge-chain --dev
+```
+
+You can also check whether the code is able to compile or not,
+
+```bash
+./scripts/docker_run.sh cargo check
+```
+
 ## Advanced: Generate Your Own Substrate Node Template
 
 A substrate node template is always based on a certain version of Substrate. You can inspect it by
diff --git a/docker-compose.yml b/docker-compose.yml
new file mode 100644
index 000000000..494c0a015
--- /dev/null
+++ b/docker-compose.yml
@@ -0,0 +1,17 @@
+version: "3.2"
+
+services:
+  dev:
+    container_name: node-template
+    image: parity/rust-builder:latest
+    working_dir: /var/www/node-template
+    ports:
+      - "9944:9944"
+    environment:
+      - CARGO_HOME=/var/www/node-template/.cargo
+    volumes:
+      - .:/var/www/node-template
+      - type: bind
+        source: ./.local
+        target: /root/.local
+    command: bash -c "cargo build --release && ./target/release/node-template --dev --ws-external"
diff --git a/scripts/docker_run.sh b/scripts/docker_run.sh
new file mode 100755
index 000000000..61e6b0fd1
--- /dev/null
+++ b/scripts/docker_run.sh
@@ -0,0 +1,10 @@
+#!/usr/bin/env bash
+
+set -e
+
+echo "*** Start Substrate node template ***"
+
+cd $(dirname ${BASH_SOURCE[0]})/..
+
+docker-compose down --remove-orphans
+docker-compose run --rm --service-ports dev $@
\ No newline at end of file
-- 
GitLab