Skip to content

Commit 4a0e63e

Browse files
committed
project refactor
1 parent c2715f3 commit 4a0e63e

File tree

5 files changed

+192
-0
lines changed

5 files changed

+192
-0
lines changed

README.md

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
# FLASK MICROSERVICES
2+
3+
This project is based on [testdriven.io](http://testdriven.io/) Microservices with Docker, Flask and React
4+
5+
### Structure
6+
1. _[flask-microservices-main](https://github.com/repodevs/flask-microservices-main)_ Docker Compose files, Nginx, admin scripts
7+
2. _[flask-microservices-users](https://github.com/repodevs/flask-microservices-users)_ Flask App
8+
3. _[flask-microservices-client](https://github.com/repodevs/flask-microservices-client)_ client-side based on ReactJS
9+
---
10+
11+
## How To Run
12+
----
13+
### Build for Local
14+
1. create docker-machine for local (with virtualbox driver)
15+
```bash
16+
$ docker-machine create --driver virtualbox dev
17+
```
18+
2. activate docker-machine
19+
```bash
20+
$ eval $(docker-machine env dev)
21+
```
22+
3. Build the images and spin up the containers
23+
```bash
24+
$ docker-compose up -d --build
25+
```
26+
4. create and seed the db then run the tests
27+
```bash
28+
$ docker-compose run users-service python manage.py recreate_db
29+
$ docker-compose run users-service python manage.py seed_db
30+
$ docker-compose run users-service python manage.py test
31+
```
32+
33+
Test it by opening your browser [http://0.0.0.0:80](0.0.0.0:80) or [http://0.0.0.0:80/users](0.0.0.0:80/users)
34+
35+
---
36+
### Build for Production
37+
38+
1. create docker-machine for production (with driver digitalocean based on ubuntu 14.04)
39+
```bash
40+
$ docker-machine create --driver digitalocean --digitalocean-access-token=DO_TOKEN --digitalocean-image ubuntu-14-04-x64 prod
41+
```
42+
2. activate docker-machine
43+
```bash
44+
$ eval "$(docker-machine env prod)"
45+
```
46+
3. Build the images and spin up the containers
47+
```bash
48+
$ docker-compose -f docker-compose-prod.yml up -d --build
49+
```
50+
4. create and seed the db then run the tests
51+
```bash
52+
$ docker-compose -f docker-compose-prod.yml run users-service python manage.py recreate_db
53+
$ docker-compose -f docker-compose-prod.yml run users-service python manage.py seed_db
54+
$ docker-compose -f docker-compose-prod.yml run users-service python manage.py test
55+
```
56+
5. watch the logs
57+
```bash
58+
$ docker-compose logs -f users-service
59+
```
60+
6. grab ip
61+
```bash
62+
$ docker-machine ip prod
63+
```
64+
65+
Test it by opening your browser [http://YOUR_PROD_IP:80](YOUR_PROD_IP:80) or [http://YOUR_PROD_IP:80/users](YOUR_PROD_IP:80/users)
66+
67+
----
68+
## Other Commands
69+
70+
To stop Docker container:
71+
```bash
72+
$ docker-compose stop
73+
```
74+
To bring down the container:
75+
```bash
76+
$ docker-compose down
77+
```
78+
Force build:
79+
```bash
80+
$ docker-compose build --no-cache
81+
```
82+
Remove image:
83+
```bash
84+
$ docker rmi $(docker images -q)
85+
```
86+
---
87+
Access database via psql:
88+
```bash
89+
$ docker exec -ti users-db psql -U postgres -W
90+
```
91+

docker-compose-prod.yml

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
version: '2.1'
2+
3+
services:
4+
users-db:
5+
container_name: users-db
6+
build: https://github.com/repodevs/flask-microservices-users.git#master:project/db
7+
ports:
8+
- 5435:5432 # expose port - HOST:CONTAINER
9+
environment:
10+
- POSTGRES_USER=postgres
11+
- POSTGRES_PASSWORD=postgres
12+
healthcheck:
13+
test: exit 0
14+
15+
16+
users-service:
17+
container_name: users-service
18+
build: https://github.com/repodevs/flask-microservices-users.git
19+
expose:
20+
- '5000'
21+
environment:
22+
- APP_SETTINGS=project.config.ProductionConfig
23+
- DATABASE_URL=postgres://postgres:postgres@users-db:5432/users_prod
24+
- DATABASE_TEST_URL=postgres://postgres:postgres@users-db:5432/users_test
25+
depends_on:
26+
users-db:
27+
condition: service_healthy
28+
links:
29+
- users-db
30+
command: gunicorn -b 0.0.0.0:5000 manage:app
31+
32+
33+
nginx:
34+
container_name: nginx
35+
build: ./nginx/
36+
restart: always
37+
ports:
38+
- 80:80
39+
depends_on:
40+
users-service:
41+
condition: service_started
42+
links:
43+
- users-service

docker-compose.yml

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
version: '2.1'
2+
3+
services:
4+
users-db:
5+
container_name: users-db
6+
build: https://github.com/repodevs/flask-microservices-users.git#master:project/db
7+
ports:
8+
- 5435:5432 # expose port - HOST:CONTAINER
9+
environment:
10+
- POSTGRES_USER=postgres
11+
- POSTGRES_PASSWORD=postgres
12+
healthcheck:
13+
test: exit 0
14+
15+
16+
users-service:
17+
container_name: users-service
18+
build: https://github.com/repodevs/flask-microservices-users.git
19+
ports:
20+
- 5001:5000 # expose ports - HOST:CONTAINER
21+
environment:
22+
- APP_SETTINGS=project.config.DevelopmentConfig
23+
- DATABASE_URL=postgres://postgres:postgres@users-db:5432/users_dev
24+
- DATABASE_TEST_URL=postgres://postgres:postgres@users-db:5432/users_test
25+
depends_on:
26+
users-db:
27+
condition: service_healthy
28+
links:
29+
- users-db
30+
31+
32+
nginx:
33+
container_name: nginx
34+
build: ./nginx/
35+
restart: always
36+
ports:
37+
- 80:80
38+
depends_on:
39+
users-service:
40+
condition: service_started
41+
links:
42+
- users-service

nginx/Dockerfile

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
#FROM nginx:1.13.0
2+
FROM nginx:latest
3+
4+
RUN rm /etc/nginx/conf.d/default.conf
5+
ADD /flask.conf /etc/nginx/conf.d

nginx/flask.conf

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
server {
2+
3+
listen 80;
4+
5+
location / {
6+
proxy_pass http://users-service:5000;
7+
proxy_set_header Host $host;
8+
proxy_set_header X-Real-IP $remote_addr;
9+
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
10+
}
11+
}

0 commit comments

Comments
 (0)