Skip to content
This repository was archived by the owner on Jun 14, 2024. It is now read-only.

Commit 38b7c0b

Browse files
author
Alaan Rahim
authored
Merge pull request #7 from appliedblockchain/docker-dev
Created a docker dev environment
2 parents d9d3479 + e3b2c05 commit 38b7c0b

15 files changed

+258
-21
lines changed

.dockerignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,5 @@ api/node_modules
66
react/node_modules
77
react/build
88
contracts/node_modules
9-
stack
9+
stack
10+
docker-dev

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,4 @@ Mono-repo with awith Base App Example
88
- [API](./api) - Node.js API
99
- [Contracts](./contracts) - Ethereum Solidity contracts
1010
- [Docs](./docs) - Documentation, guides, notes, etc
11+
- [docker-dev](./docker-dev) - Docker development environment with hot reload for the api and react

api/.dockerignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
node_modules
2+
package-lock.json

api/Dockerfile-dev

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
FROM node:10.12-alpine
2+
3+
RUN apk --update --no-cache add alpine-sdk git python openssl curl bash && \
4+
rm -rf /tmp/* /var/cache/apk/*
5+
6+
RUN npm i -g pm2
7+
8+
USER node
9+
10+
RUN mkdir /home/node/api
11+
12+
WORKDIR /home/node/api
13+
14+
COPY package.json package.json
15+
16+
ARG NPM_TOKEN
17+
18+
RUN echo "//registry.npmjs.org/:_authToken=$NPM_TOKEN" > ~/.npmrc &&\
19+
npm i &&\
20+
rm ~/.npmrc
21+
22+
COPY . /home/node/api
23+
24+
EXPOSE 3000
25+
26+
CMD pm2 start run.js --watch . && pm2 logs
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"PORT": "PORT",
3+
"ETHEREUM_JSONRPC_ENDPOINT": "ETHEREUM_JSONRPC_ENDPOINT",
4+
"CONTRACT_ADDRESS": "CONTRACT_ADDRESS"
5+
}

api/package-lock.json

Lines changed: 1 addition & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

contracts/package-lock.json

Lines changed: 25 additions & 13 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

contracts/package.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,12 @@
33
"scripts": {
44
"compile": "truffle compile",
55
"deploy": "node ./bin/deploy && rm -fr ../api/contracts/*.json && cp -f ./build/contracts/*.json ../api/contracts",
6-
"export-addresses": "source ./exportAddresses.sh"
6+
"export-addresses": "source ./exportAddresses.sh",
7+
"install-contracts": "cp -f ./build/contracts/*.json ../api/contracts"
78
},
89
"dependencies": {
910
"dotenv": "^6.0.0",
11+
"typedarray-to-buffer": "^3.1.5",
1012
"web3": "^1.0.0-beta.36",
1113
"web3-utils": "^1.0.0-beta.36"
1214
}

docker-dev/README.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# How to Use
2+
3+
1. Install docker and docker compose
4+
2. In a terminal, run `source docker-aliases.sh`
5+
3. Run `mantle-compose build` to build the api and react images
6+
4. Start parity only: `mantle-compose up parity`
7+
5. While parity is running, deploy the contracts(run `node bin/deploy.js` from the contracts folder)
8+
6. Stop parity and run `mantle-compose up` to start all the services

docker-dev/compose.sh

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
#!/usr/bin/env bash
2+
cd "$(dirname "$0")" || exit 1
3+
4+
configArgs=""
5+
configPathList=(docker-compose-dev.yml)
6+
npmrcPath="$HOME/.npmrc"
7+
8+
for path in "${configPathList[@]}"; do
9+
configArgs="$configArgs -f $path"
10+
done
11+
12+
show_usage() {
13+
echo "\
14+
Usage:
15+
$0 -h
16+
$0 [-t NPM_TOKEN] ARGS...
17+
18+
Flags:
19+
-h : Show this message and exit
20+
-t NPM_TOKEN : A valid token to access the Applied Blockchain private npm repos. If omitted then the current token will be extracted from '$npmrcPath' and used.
21+
22+
Runs 'docker-compose$configArgs ARGS...'
23+
" | fold -s
24+
}
25+
26+
while getopts "ht:" opt; do
27+
case $opt in
28+
h)
29+
show_usage
30+
exit
31+
;;
32+
t)
33+
npmToken="$OPTARG"
34+
;;
35+
\?)
36+
show_usage
37+
exit 2
38+
;;
39+
esac
40+
done
41+
42+
shift $((OPTIND - 1))
43+
44+
if [[ -z $npmToken ]]; then
45+
if [[ ! -f $npmrcPath ]]; then
46+
echo "$npmPath file not found, so NPM_TOKEN argument required"
47+
exit 2
48+
else npmToken="$(cat "$npmrcPath" | grep _authToken |sed 's/.*=\(.*$\)/\1/')"
49+
fi
50+
fi
51+
52+
53+
# echo 'installing contracts in app folders(shared_modules)'
54+
npm --prefix ../contracts run install-contracts
55+
56+
echo 'Loading contracts addresses in env'
57+
source ../api/contracts/exportAddresses.sh
58+
59+
env COMPOSE_PROJECT_NAME="base-app-mantle" NPM_TOKEN="$npmToken" docker-compose $configArgs $@

docker-dev/docker-aliases.sh

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
#!/bin/bash
2+
3+
# alias to docker-compose with the development compose file hard wired, you can start build the stack by running "compose build" for instance.
4+
alias mantle-compose="mantle_compose_dev $@"
5+
6+
# alias compose-integration="docker_compose_integration $@"
7+
8+
# list the running docker containers
9+
alias dcl="docker container list"
10+
11+
# list the existing docker volumes
12+
alias dvl="docker volume list"
13+
14+
# Interactive session with runnig containers
15+
16+
# ssh into a container, take the container id as a parm
17+
alias cssh="docker_ssh $1"
18+
19+
# ssh into a container, take the container image name or a part of the id.
20+
alias dssh="image_ssh $1"
21+
22+
MANTLE_APP_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
23+
24+
# Function used for the aliases
25+
mantle_compose_dev () {
26+
$MANTLE_APP_DIR/compose.sh $@
27+
}
28+
29+
docker_ssh () { docker exec -it $1 bash || docker exec -it $1 sh; }
30+
31+
image_ssh () {
32+
if [[ $1 == "-h" || $1 == "--help" ]]
33+
then
34+
echo 'dssh: "ssh" into a running docker container.'
35+
# echo "|"
36+
echo "Usage:"
37+
echo " dssh EXPRESSION. EXPRESSION can be any key word that match against a running container id or image name, Run dcl for a list of currently running containers."
38+
else
39+
id=`docker container list | tr -s ' ' | cut -f1 -f2 -d" " |grep "$1" | cut -f1 -d" "`;
40+
docker_ssh $id;
41+
fi
42+
}

docker-dev/docker-compose-dev.yml

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
version: '3'
2+
3+
services:
4+
react:
5+
build:
6+
context: ../react
7+
dockerfile: Dockerfile-dev
8+
args:
9+
- NPM_TOKEN
10+
volumes:
11+
- ../react/src/:/home/node/react/src
12+
ports:
13+
- 3000:3000
14+
api:
15+
build:
16+
context: ../api
17+
dockerfile: Dockerfile-dev
18+
args:
19+
- NPM_TOKEN
20+
volumes:
21+
- ../api/src/:/home/node/api/src
22+
- ../api/config/:/home/node/api/config
23+
- ../api/contracts/:/home/node/api/contracts
24+
environment:
25+
NODE_ENV: development
26+
ETHEREUM_JSONRPC_ENDPOINT: http://parity:8545
27+
CONTRACT_ADDRESS: "$CONTRACT_ADDRESS"
28+
networks:
29+
- main
30+
ports:
31+
- 8080:8080
32+
depends_on:
33+
- parity
34+
- redis
35+
parity:
36+
image: appliedblockchain/parity-solo-instant:latest
37+
entrypoint: ["/parity/parity", "--config", "/solo/node.toml", "--unsafe-expose", "--gasprice", "1"]
38+
restart: always
39+
volumes:
40+
- parity:/solo/chains
41+
ports:
42+
- 8545:8545
43+
- 8546:8546
44+
networks:
45+
- main
46+
redis:
47+
image: redis
48+
volumes:
49+
- redis:/data
50+
networks:
51+
- main
52+
volumes:
53+
parity:
54+
redis:
55+
networks:
56+
main:

react/.dockerignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
node_modules
2+
build
3+
Dockerfile
4+
Dockerfile-dev

react/Dockerfile-dev

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
FROM node:10.7-alpine
2+
3+
RUN apk add --update --no-cache alpine-sdk git python && \
4+
rm -rf /tmp/* /var/cache/apk/*
5+
6+
USER node
7+
8+
RUN mkdir /home/node/react
9+
WORKDIR /home/node/react
10+
11+
COPY package.json package.json
12+
13+
ARG NPM_TOKEN
14+
15+
RUN echo "//registry.npmjs.org/:_authToken=$NPM_TOKEN" > ~/.npmrc &&\
16+
npm i &&\
17+
rm ~/.npmrc
18+
19+
COPY . /home/node/react/
20+
21+
EXPOSE 3000
22+
23+
CMD npm start

react/package-lock.json

Lines changed: 1 addition & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)