|
1 | 1 | ## Postgres - Express.js - React.js Boilerplate
|
| 2 | + |
| 3 | +### Introduction |
| 4 | + |
| 5 | +This is a PREN-Stack Dockerized boilerplate. |
| 6 | +A vanila infrastructure made to simplify the develpoment and deploying processes using pern stack and docker, docker-compose. |
| 7 | + |
| 8 | +### Prerequisites |
| 9 | + |
| 10 | +Make sure u have this installed on your machine. |
| 11 | + |
| 12 | +- [x] **Docker** : https://docs.docker.com/engine/install/ |
| 13 | +- [x] **Docker-Compose** : https://docs.docker.com/compose/install/ |
| 14 | +- [x] **Node** : https://nodejs.org/en/ |
| 15 | + |
| 16 | +### File strcutre |
| 17 | + |
| 18 | +``` |
| 19 | +project-name |
| 20 | + | |
| 21 | + |---/ client |
| 22 | + | |
| 23 | + |---/ public |
| 24 | + |---/ src |
| 25 | + | |
| 26 | + .env |
| 27 | + .dockerignore |
| 28 | + .gitignore |
| 29 | + Dockerfile |
| 30 | + Dockerfile.dev |
| 31 | + nginx.conf |
| 32 | + tsconfig.json |
| 33 | + package.json |
| 34 | + | |
| 35 | + |---/ database |
| 36 | + .env |
| 37 | + initdb.sql |
| 38 | + .gitignore |
| 39 | + |---/ server |
| 40 | + | |
| 41 | + |---/ config |
| 42 | + |---/src |
| 43 | + | |
| 44 | + .env |
| 45 | + .dockerignore |
| 46 | + .gitignore |
| 47 | + Dockerfile |
| 48 | + Dockerfile.dev |
| 49 | + nodemon.json |
| 50 | + package.json |
| 51 | + wait-for-it.sh |
| 52 | + | |
| 53 | + | |
| 54 | + docker-compose.yml |
| 55 | + docker-compose-dev.yml |
| 56 | + .prettierrc |
| 57 | + README.md |
| 58 | +``` |
| 59 | + |
| 60 | +### Quick start |
| 61 | + |
| 62 | +Clone this repo to your local machine |
| 63 | + |
| 64 | +``` |
| 65 | +git clone https://github.com/nadavpodjarski/postgres-express-react-typescript-boilerplate.git <YOUR-PROJECT-NAME> |
| 66 | +``` |
| 67 | + |
| 68 | +run the following command to check this demo |
| 69 | + |
| 70 | +``` |
| 71 | +cd project-name/ && sudo docker-compose --file docker-compose-dev.yml up |
| 72 | +``` |
| 73 | + |
| 74 | +**Replace project-name with your own** |
| 75 | + |
| 76 | +## Client |
| 77 | + |
| 78 | +Client has been created with create-react-app and located in ./project-name/client |
| 79 | +In develpoment mode it will be run in a container built with ./client/Dockerfile.dev and will be exposed on port 3000. |
| 80 | +In production mode a client build will be created and will run in a container built with ./client/Dockerfile the client build/static-files and will be served with nginx server and will be exposed on port 80. |
| 81 | + |
| 82 | +Enviornment variables can be moved into running dockerfile itself under ENV or can be decalred in the docker compose file unde enviornemt property |
| 83 | + |
| 84 | +**note that nginx server has a minimalistic configuration** |
| 85 | + |
| 86 | +## Database |
| 87 | + |
| 88 | +Postgres data-base is created with an official postgres image which can be found in docker hub https://hub.docker.com/_/postgres |
| 89 | +the data-base will be initialized with ./database/initdb.sql script. feel free to change it to your own needs. |
| 90 | + |
| 91 | +Enviornment variables will be located in ./database/.env |
| 92 | +and will contain our database credentials : |
| 93 | + |
| 94 | + ``` |
| 95 | + POSTGRES_USER=admin |
| 96 | + POSTGRES_PASSWORD=admin |
| 97 | + POSTGRES_DB=pern_db |
| 98 | + |
| 99 | + ``` |
| 100 | + |
| 101 | +Voluems of our database will be located in ./database/data |
| 102 | + |
| 103 | +> Production volume is located in ./data/prod |
| 104 | +> Development volume is located in ./data/dev |
| 105 | +
|
| 106 | +## Server |
| 107 | + |
| 108 | +Server is located in ./projec-name/server using express. |
| 109 | + |
| 110 | +- In development mode it will run in a container built with ./server/Dockerfile.dev. |
| 111 | + dev mode server will be exposed on port 5500 to the "outside" world and will use volumes for data persistent. |
| 112 | +- In Production mode it will run in a container built with ./server/Dockerfile. |
| 113 | + prod mode server will ve exposed on port 5500 only to the docker composer internal services. as well using volumes for data persistent. |
| 114 | + |
| 115 | +Enviornment variables will be located in ./server/.env |
| 116 | +and will contain postgres credentials to establish connection to our databse. |
| 117 | +with our wait-for-it.sh script the server image will run only after getting confirmation that postgres container is available. |
| 118 | +by that we wont get connection failures due to wrong order of docker composing. |
| 119 | + |
| 120 | +## Docker compose |
| 121 | + |
| 122 | +### Development |
| 123 | + |
| 124 | +To establish a development environment, simply run the following command from the project root folder. |
| 125 | + |
| 126 | +``` |
| 127 | +sudo docker-compose --file docker-compose-dev.yml up |
| 128 | +``` |
| 129 | + |
| 130 | +On save changes in client and server, cotainers will be automatically updated, no need to restart any servers. |
| 131 | +</br> |
| 132 | + |
| 133 | +### Production |
| 134 | + |
| 135 | +To establish production environment, simply run the following command from the root folder. |
| 136 | + |
| 137 | +``` |
| 138 | +sudo docker-compose up |
| 139 | +``` |
| 140 | + |
| 141 | +This will creates build for both server and client, will serve client build with nginx server on port 80 and will communicate with server on port 5500 in the location /api, server will communicate with postgres oon port 5432. |
| 142 | + |
| 143 | +## Demo |
| 144 | + |
| 145 | +Demo is a simple Todolist using react as our front express as backend and postgres as our database |
| 146 | +demo can be started in development mode and in production mode as well. |
| 147 | + |
| 148 | +<img src="./demo1.png" style="box-shadow 0px 10px 10px rgba(0,0,0,0.3);" /> |
| 149 | + |
| 150 | +## Enjoy |
0 commit comments