Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Docker for development and production environments #261

Open
wants to merge 15 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 11 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# items that do not need to be in a Docker image as they
# are not used by the build system
.circleci/
.git/
docker/Dockerfile.*
.dockerignore
.gitignore
README.md

# items that may be built during Docker image creation
build/
node_modules/
5 changes: 5 additions & 0 deletions .env
Original file line number Diff line number Diff line change
@@ -1 +1,6 @@
ADDRESS=http://localhost
APP=substrate-front-end-template
NODE_ENV=
PORT=8000
PORT_PROD=8001
PORT_NGINX=80
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,4 @@ coverage
!.yarn/sdks
!.yarn/versions
.pnp.*
docker-dev.log
62 changes: 62 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -143,3 +143,65 @@ it also displays the user's token balance. It is included in the template alread
```

Refer to [this doc page](https://github.com/vacp2p/docs.wakuconnect.dev/blob/develop/content/docs/guides/07_reactjs_relay.md).

## Docker

### Development

* Install and run [Docker](https://www.docker.com/)
* Run Substrate front-end from a Docker container and follow the terminal log instructions.
```bash
./docker-dev.sh
```

### Production

* Install and run [Docker](https://www.docker.com/)
* Run Substrate front-end from a Docker container and follow the terminal log instructions.
```bash
./docker-prod.sh
```

### Useful Docker Commands

* Enter Docker container shell
```bash
docker exec -it $CONTAINER_ID /bin/sh
```

* View Docker container logs
```bash
docker logs -f $CONTAINER_ID
```

* List Docker containers
```bash
docker ps -a
```

* List Docker images
```bash
docker images -a
```

* Remove Docker container
```bash
docker stop $CONTAINER_ID; docker rm $CONTAINER_ID;
```

* Remove Docker image
```bash
docker rmi $IMAGE_ID
```

### Useful Nginx Commands

* Verify Nginx Config File Syntax Ok
```bash
nginx -t
```

* [Reload](https://docs.nginx.com/nginx/admin-guide/basic-functionality/runtime-control/) Nginx Config File for changes to take effect
```bash
nginx -s reload
```
21 changes: 21 additions & 0 deletions docker-compose-dev.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
version: "3.8"
services:
dev:
container_name: ${APP}-dev
restart: always
build:
context: "."
dockerfile: ./docker/Dockerfile.dev
args:
- PORT=${PORT}
- APP=${APP}
volumes:
- ./:/usr/local/apps/${APP}:delegated
- ignore:/usr/local/apps/${APP}/node_modules
ports:
- "127.0.0.1:${PORT}:${PORT}"
ltfschoen marked this conversation as resolved.
Show resolved Hide resolved
env_file:
- .env

volumes:
ignore:
16 changes: 16 additions & 0 deletions docker-dev.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#!/usr/bin/env bash
# Copyright 2017-2022 @polkadot authors & contributors
# This software may be modified and distributed under the terms
# of the Apache-2.0 license. See the LICENSE file for details.

trap "echo; exit" INT
trap "echo; exit" HUP
source .env \
&& export APP \
&& export PORT \
&& ./docker/build.sh \
ltfschoen marked this conversation as resolved.
Show resolved Hide resolved
&& printf "\n*** Started building Docker container." \
&& printf "\n*** Please wait... \n***" \
&& DOCKER_BUILDKIT=0 docker compose -f docker-compose-dev.yml up --build
ltfschoen marked this conversation as resolved.
Show resolved Hide resolved
printf "\n*** Finished building Docker container."
printf "\n*** Open web browser: http://localhost:${PORT}\n"
28 changes: 28 additions & 0 deletions docker-prod.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#!/usr/bin/env bash
# Copyright 2017-2022 @polkadot authors & contributors
# This software may be modified and distributed under the terms
# of the Apache-2.0 license. See the LICENSE file for details.

trap "echo; exit" INT
trap "echo; exit" HUP
source .env \
&& export ADDRESS \
&& export APP \
&& NODE_ENV=production \
&& export PORT \
&& export PORT_NGINX \
&& export PORT_PROD \
&& PUBLIC_URL="${ADDRESS}:${PORT_PROD}" \
&& NODE_ENV=${NODE_ENV} PUBLIC_URL=${PUBLIC_URL} ./docker/build.sh \
&& printf "\n*** Started building ${NODE_ENV} Docker container." \
&& printf "\n*** Please wait... \n***" \
&& docker run -it \
-e NODE_ENV=${NODE_ENV} \
-e PORT_NGINX=${PORT_NGINX} \
-e PUBLIC_URL=${PUBLIC_URL} \
-d \
-p ${PORT_PROD}:${PORT_NGINX} \
--name "${APP}-prod" ${APP}

printf "\n*** Finished building ${NODE_ENV} Docker container."
printf "\n*** Open web browser: ${PUBLIC_URL}\n"
22 changes: 22 additions & 0 deletions docker/Dockerfile.dev
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
FROM node:gallium-alpine

ARG APP=${APP}
ARG APP_PATH=/usr/local/apps/${APP}
ENV PATH=${APP_PATH}/node_modules/.bin:$PATH
WORKDIR ${APP_PATH}

COPY package.json .

RUN apk update && apk add --update git && rm -rf /var/cache/apk/*
RUN corepack enable \
&& corepack prepare yarn@stable --activate \
&& yarn set version 3.1.1 \
&& yarn plugin import interactive-tools \
&& echo -e "nodeLinker: node-modules\n\n$(cat ${APP_PATH}/.yarnrc.yml)" > ${APP_PATH}/.yarnrc.yml \
&& yarn

COPY . .

EXPOSE ${PORT}

CMD ["yarn", "run", "start"]
40 changes: 40 additions & 0 deletions docker/Dockerfile.prod
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
FROM node:gallium-alpine AS builder

ARG APP=${APP}
ARG PORT_NGINX=${PORT_NGINX}
ARG APP_PATH=/usr/local/apps/${APP}
# required for it to load correctly
ENV PUBLIC_URL=${PUBLIC_URL}
ENV PATH=${APP_PATH}/node_modules/.bin:$PATH
WORKDIR ${APP_PATH}

COPY package.json .

RUN apk update && apk add --update git && rm -rf /var/cache/apk/*
RUN corepack enable \
&& corepack prepare yarn@stable --activate \
&& yarn set version 3.1.1 \
&& yarn plugin import interactive-tools \
&& echo -e "nodeLinker: node-modules\n\n$(cat ${APP_PATH}/.yarnrc.yml)" > ${APP_PATH}/.yarnrc.yml \
&& yarn
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should be yarn install --production


COPY . .

RUN NODE_ENV=production yarn build

# ===========================================================
FROM nginx:stable-alpine

ARG APP=${APP}
ARG APP_PATH=/usr/local/apps/${APP}
ARG NGINX_PATH=/usr/share/nginx/html
WORKDIR ${NGINX_PATH}
RUN mkdir -p ${NGINX_PATH}/${APP}

COPY --from=builder ${APP_PATH}/docker/nginx.conf /etc/nginx/nginx.conf
COPY --from=builder ${APP_PATH}/build/index.html ${NGINX_PATH}/index.html
COPY --from=builder ${APP_PATH}/build ${NGINX_PATH}/${APP}

EXPOSE ${PORT_NGINX}

CMD ["nginx", "-g", "daemon off;"]
30 changes: 30 additions & 0 deletions docker/build.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#!/usr/bin/env bash
ltfschoen marked this conversation as resolved.
Show resolved Hide resolved
# Copyright 2017-2022 @polkadot/apps authors & contributors
# This software may be modified and distributed under the terms
# of the Apache-2.0 license. See the LICENSE file for details.

# Fail fast on any non-zero exits
set -e

echo "*** Started building $APP"

if [[ $NODE_ENV = "production" ]]
then
echo "*** Building $NODE_ENV $APP"
docker build --progress=plain \
--no-cache \
--build-arg APP=$APP \
--build-arg PORT_NGINX=$PORT_NGINX \
--build-arg PUBLIC_URL=$PUBLIC_URL \
-t $APP \
-f docker/Dockerfile.prod .
else
ltfschoen marked this conversation as resolved.
Show resolved Hide resolved
echo "*** Building $APP"
docker build --progress=plain \
--no-cache \
--build-arg APP=$APP \
-t $APP \
-f docker/Dockerfile.dev .
fi

echo "*** Finished building $APP"
39 changes: 39 additions & 0 deletions docker/nginx.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
#!/usr/bin/env bash
# Copyright 2017-2022 @polkadot/apps authors & contributors
# This software may be modified and distributed under the terms
# of the Apache-2.0 license. See the LICENSE file for details.

# Reference: https://github.com/polkadot-js/apps/blob/master/docker/nginx.conf

# Directives and parameters
user nginx; # Directive in the 'main' context
worker_processes 1;

error_log /var/log/nginx/error.log warn;
pid /var/run/nginx.pid;

events {
# Configuration of connection processing
worker_connections 1024;
}

http {
# Configuration specific to HTTP and affecting all virtual servers
include /etc/nginx/mime.types;
default_type application/octet-stream;

log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';

access_log /var/log/nginx/access.log main;

sendfile on;
#tcp_nopush on;

keepalive_timeout 65;
gzip on;

# Includes server contexts for configuring each HTTP virtual server
include /etc/nginx/conf.d/*.conf;
}
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
"polkadot-js"
],
"scripts": {
"start": "react-app-rewired start",
"start": "WATCHPACK_POLLING=true react-app-rewired start",
"build": "react-app-rewired build",
"test": "CI=true react-app-rewired test",
"eject": "react-scripts eject",
Expand Down