Skip to content

Commit

Permalink
Add base files (#1)
Browse files Browse the repository at this point in the history
* Add initial files

* Add end of file and format

* Fix if syntax

* Add script path

* Fix new line

* Fix new line and add force remove

* Delete directory after move

* Add better outputs

* Fix to only delete backup folder if exist

* Update License

* Move docker-compose files download

* Remove unnecessary rm

* Update name

* Add client Dockerfile

* Fix folder name

* Change backup folder name

* Fix branch name

* Fix condition and download env

* Add wget overwrite

* Move command to download env

* Fix client folder permission denied

* Fix permission

* Change react app to use npm instead of yarn

* Add prompt check yes no before creating react app

* Fix quiet parameter

* Add sudo to docker and fix variable

* Add new line

* Add more files for client folder

* Attempt to fix arg not found

* Added backend section

* Add ARG to dev file

* Add backend files

* Fix npm init dir

* Change to sh

* Fix commands

* Add title to Server configuration

* Remove mkdir

* Improve output

* Extracted function into a separate file

* Improve output

* Separate command from docker run

* Change variable name

* Pass in temp string as env

* Combine to multiline

* Add silent

* Remove old e variable

* Pass ARG to backend Dockerfiles

* Add initial db setup

* Add default

* Fix condition

* Remove .env file

* Change sample size

* Change comparison operator

* Fix condition brackets

* Fix bash condition

* Renaming configuration

* Change argument

* Add ignore files

* Download ignore files too

* Add proxy section

* Remove useless echo

* Make constants into variables

* Convert variables in conf file

* Fixed replacement redirect bug

* Add docker-compose files

* Add sample env files

* Pass env variables to command

* Fix syntax

* Change incorrect output

* Add docker-compose for ssl

* Update syntax

* Fix args syntax

* Change to mapping

* Fix copy syntax

* Add todo

* Pass env variable to a single envsubst call

* Add option to add SSL info

* Add user to mongo image

* Extracted sh into script

* Add startup script for backend

* Add nginx conf for client

* Add backend starter script

* Add missing rm

* Add comment

* Attempt to fix permission in db

* Fix path

* Fix file path

* Add note

* Update ReadMe

* Download LICENSE

* Accept dynamic branch variable

* Fix syntax

* Fix permission for correct folder

* Update ReadMe

* Update README
  • Loading branch information
NanoCode012 committed Apr 15, 2021
1 parent 6fc3b0a commit 38adda9
Show file tree
Hide file tree
Showing 27 changed files with 682 additions and 2 deletions.
2 changes: 2 additions & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
.git
.env
1 change: 1 addition & 0 deletions .env.sample
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
DOCKER_NODE_VERSION=15.13.0-alpine
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
.env

# Ignore all database volume files
db/mongo-volume
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
MIT License

Copyright (c) 2021 NanoCode012
Copyright (c) 2021 Chanvichet Vong

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
54 changes: 53 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1 +1,53 @@
# docker-mern
# Docker MERN

This repo aims to create a one-stop script which would allow the setup of a MERN stack via multiple containers reliably.

# Requirements

- docker (with external network created)
- docker-compose
- wget

# Setup

```bash
$ wget https://raw.githubusercontent.com/NanoCode012/docker-mern/main/setup-server.sh -O setup-server.sh
$ chmod +x setup-server.sh
$ ./setup-server.sh
```

Note: Ignore `Git repo not initialized Error: Command failed: git --version` when creating base react files

### Optional arguments:

- `barebone` run with default options and without creating react and node files

# Usage

## Production

```bash
$ docker-compose -f docker-compose.yml -f docker-compose.prod.yml up -d --build
```

Add `-f docker-compose.ssl.yml` for SSL setup

## Development

```bash
$ docker-compose up -d --build
```

# Configuration

- For more docker-configuration, consider creating a new compose file with the changes and adding it via `-f` option

# Script Development/Test

Pass in environment variable `BRANCH=the_branch_name` before calling script, so that the script knows where to `wget` the files.

# Contribution

PRs are **greatly** appreciated, but please open an Issue first to discuss.

Questions? Open an Issue.
14 changes: 14 additions & 0 deletions backend/.dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# dependencies
node_modules

# misc
.DS_Store
.env
.env.local
.env.development.local
.env.test.local
.env.production.local

npm-debug.log*
yarn-debug.log*
yarn-error.log*
14 changes: 14 additions & 0 deletions backend/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# dependencies
node_modules

# misc
.DS_Store
.env
.env.local
.env.development.local
.env.test.local
.env.production.local

npm-debug.log*
yarn-debug.log*
yarn-error.log*
15 changes: 15 additions & 0 deletions backend/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
ARG DOCKER_NODE_VERSION

FROM node:$DOCKER_NODE_VERSION AS alpine

WORKDIR /app

# Install dependencies
COPY package*.json ./
RUN npm install
COPY . .

EXPOSE 3001

# Default command
CMD ["npm", "run", "start"]
15 changes: 15 additions & 0 deletions backend/Dockerfile.dev
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
ARG DOCKER_NODE_VERSION

FROM node:$DOCKER_NODE_VERSION AS alpine

WORKDIR /app

# Install dependencies
COPY package*.json .
RUN npm install --save-dev
# COPY . .

EXPOSE 3001

# Default command
CMD ["npm", "run", "test"]
12 changes: 12 additions & 0 deletions backend/src/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// Start code from: https://expressjs.com/en/starter/hello-world.html
const express = require("express");
const app = express();
const port = 3001;

app.get("/", (req, res) => {
res.send("Hello World!");
});

app.listen(port, () => {
console.log(`Example app listening at http://localhost:${port}`);
});
13 changes: 13 additions & 0 deletions backend/startup.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#!/bin/sh

# To be run inside docker container

cd backend

npm init -y
npm install --save express

npm install --save-dev --silent nodemon

npm set-script start "node src/index.js"
npm set-script test "nodemon src/index.js"
23 changes: 23 additions & 0 deletions client/.dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# See https://help.github.com/articles/ignoring-files/ for more about ignoring files.

# dependencies
/node_modules
/.pnp
.pnp.js

# testing
/coverage

# production
/build

# misc
.DS_Store
.env.local
.env.development.local
.env.test.local
.env.production.local

npm-debug.log*
yarn-debug.log*
yarn-error.log*
23 changes: 23 additions & 0 deletions client/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# See https://help.github.com/articles/ignoring-files/ for more about ignoring files.

# dependencies
/node_modules
/.pnp
.pnp.js

# testing
/coverage

# production
/build

# misc
.DS_Store
.env.local
.env.development.local
.env.test.local
.env.production.local

npm-debug.log*
yarn-debug.log*
yarn-error.log*
13 changes: 13 additions & 0 deletions client/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
ARG DOCKER_NODE_VERSION

FROM node:$DOCKER_NODE_VERSION as build
WORKDIR /app
COPY package*.json ./
RUN npm install
COPY . .
RUN npm run build

FROM nginx
EXPOSE 3000
COPY ./nginx/default.conf /etc/nginx/conf.d/default.conf
COPY --from=build /app/build /usr/share/nginx/html
9 changes: 9 additions & 0 deletions client/Dockerfile.dev
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
ARG DOCKER_NODE_VERSION

FROM node:$DOCKER_NODE_VERSION as build
WORKDIR /app
COPY package*.json ./
RUN npm install

# Default command
CMD ["npm", "start"]
9 changes: 9 additions & 0 deletions client/nginx/default.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
server {
listen 3000;

location / {
root /usr/share/nginx/html;
index index.html index.htm;
try_files $uri $uri/ /index.html;
}
}
24 changes: 24 additions & 0 deletions docker-compose.override.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
version: "3.3"
services:
nginx:
ports:
- "80:80"
backend:
build:
context: ./backend
dockerfile: Dockerfile.dev
volumes:
- ./backend/:/app/
environment:
- NODE_ENV=development
- CHOKIDAR_USEPOLLING=true

client:
build:
context: ./client
dockerfile: Dockerfile.dev
volumes:
- ./client/:/app/
environment:
- NODE_ENV=development
- CHOKIDAR_USEPOLLING=true
11 changes: 11 additions & 0 deletions docker-compose.prod.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
version: "3.3"
services:
backend:
build:
context: ./backend
dockerfile: Dockerfile

client:
build:
context: ./client
dockerfile: Dockerfile
5 changes: 5 additions & 0 deletions docker-compose.ssl.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
version: '3.3'
services:
nginx:
env_file:
- ./env/nginx.env
46 changes: 46 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
version: "3.3"
services:
nginx:
container_name: ${NGINX_NAME}
depends_on:
- client
- backend
restart: always
build:
dockerfile: Dockerfile
context: ./nginx
client:
container_name: ${CLIENT_NAME}
build:
args:
DOCKER_NODE_VERSION: ${DOCKER_NODE_VERSION}
volumes:
- /app/node_modules # Inside the container, don't try to override this folder, just leave as is
restart: on-failure

backend:
container_name: ${BACKEND_NAME}
build:
args:
DOCKER_NODE_VERSION: ${DOCKER_NODE_VERSION}
volumes:
- /app/node_modules # Inside the container, don't try to override this folder, just leave as is
env_file:
- ./env/backend.env
restart: on-failure

mongo:
image: mongo
container_name: ${DB_NAME}
user: ${USER}:${USER}
volumes:
- ./db/init-mongo.js:/docker-entrypoint-initdb.d/init-mongo.js:ro
- ./db/mongo-volume:/data/db
env_file:
- ./env/mongo.env
restart: on-failure

networks:
default:
external:
name: ${PROXY_NAME}
1 change: 1 addition & 0 deletions env/backend.env.sample
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
DB_CONNECTION_STRING=mongodb://$DB_NAME:27017/$MONGO_INITDB_DATABASE
3 changes: 3 additions & 0 deletions env/mongo.env.sample
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
MONGO_INITDB_DATABASE=$MONGO_INITDB_DATABASE
MONGO_INITDB_USERNAME=$MONGO_INITDB_USERNAME
MONGO_INITDB_PASSWORD=$MONGO_INITDB_PASSWORD
4 changes: 4 additions & 0 deletions env/nginx.env.sample
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
VIRTUAL_HOST=$VIRTUAL_HOST
VIRTUAL_PORT=$VIRTUAL_PORT
LETSENCRYPT_HOST=$LETSENCRYPT_HOST
LETSENCRYPT_EMAIL=$LETSENCRYPT_EMAIL
43 changes: 43 additions & 0 deletions local-scripts.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
#!/bin/bash

# Function to read input from user with a prompt
# Credits: https://github.com/TheRemote/MinecraftBedrockServer/blob/1f27b8ab82f920bb967d1c27ee2fd120a484c99c/SetupMinecraft.sh
function read_with_prompt {
variable_name="$1"
prompt="$2"
default="${3-}"
unset $variable_name
while [[ ! -n ${!variable_name} ]]; do
read -p "$prompt: " $variable_name < /dev/tty
if [ ! -n "`which xargs`" ]; then
declare -g $variable_name=$(echo "${!variable_name}" | xargs)
fi
declare -g $variable_name=$(echo "${!variable_name}" | head -n1 | awk '{print $1;}')
if [[ -z ${!variable_name} ]] && [[ -n "$default" ]] ; then
declare -g $variable_name=$default
fi
echo -n "$prompt : ${!variable_name} -- accept (y/n)?"
read answer < /dev/tty
if [ "$answer" == "${answer#[Yy]}" ]; then
unset $variable_name
else
echo "$prompt: ${!variable_name}"
fi
done
}

function read_yes_no {
variable_name="$1"
prompt="$2"
unset $variable_name

read -p "$prompt -- (y/n)?" answer
case ${answer:0:1} in
y|Y )
declare -g $variable_name=true
;;
* )
declare -g $variable_name=false
;;
esac
}
Loading

0 comments on commit 38adda9

Please sign in to comment.