Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
15 changed files
with
319 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,157 @@ | ||
# Developing and Testing Microservices with Docker | ||
|
||
[![Build Status](https://travis-ci.org/mjhea0/node-docker-api.svg?branch=master)](https://travis-ci.org/mjhea0/node-docker-api) | ||
|
||
## Want to learn how to build this project? | ||
|
||
Check out the [blog post](). | ||
|
||
## Want to use this project? | ||
|
||
### Setup | ||
|
||
1. Fork/Clone this repo | ||
|
||
1. Download [Docker](https://docs.docker.com/docker-for-mac/install/) (if necessary) | ||
|
||
1. Make sure you are using a Docker version >= 17: | ||
|
||
```sh | ||
$ docker -v | ||
Docker version 17.03.0-ce, build 60ccb22 | ||
``` | ||
|
||
### Build and Run the App | ||
|
||
#### Set the Environment variables | ||
|
||
```sh | ||
$ export NODE_ENV=development | ||
``` | ||
|
||
Register with the [OpenWeatherMap API](https://openweathermap.org/api), and add the key as an environment variable: | ||
|
||
```sh | ||
$ export OPENWEATHERMAP_API_KEY=YOUR_KEY_HERE | ||
``` | ||
|
||
#### Fire up the Containers | ||
|
||
Build the images: | ||
|
||
```sh | ||
$ docker-compose build | ||
``` | ||
|
||
Run the containers: | ||
|
||
```sh | ||
$ docker-compose up -d | ||
``` | ||
|
||
#### Migrate and Seed | ||
|
||
With the apps up, run: | ||
|
||
```sh | ||
$ sh migrate.sh | ||
``` | ||
|
||
#### Sanity Check | ||
|
||
Test out the following services... | ||
|
||
##### (1) Users - http://localhost:3000 | ||
|
||
| Endpoint | HTTP Method | CRUD Method | Result | | ||
|-----------------|-------------|-------------|---------------| | ||
| /users/ping | GET | READ | `pong` | | ||
| /users/register | POST | CREATE | add a user | | ||
| /users/login | POST | CREATE | log in a user | | ||
| /users/user | GET | READ | get user info | | ||
|
||
##### (2) Locations - http://localhost:3001 | ||
|
||
| Endpoint | HTTP Method | CRUD Method | Result | | ||
|------------------|-------------|-------------|---------------------------| | ||
| /locations/ping | GET | READ | `pong` | | ||
| /locations | GET | READ | get all locations | | ||
| /locations/user | GET | READ | get all locations by user | | ||
| /locations/:id | GET | READ | get a single location | | ||
| /locations | POST | CREATE | add a single location | | ||
| /locations/:id | PUT | UPDATE | update a single location | | ||
| /locations/:id | DELETE | DELETE | delete a single location | | ||
|
||
##### (3) Web - http://localhost:3003 | ||
|
||
| Endpoint | HTTP Method | CRUD Method | Result | | ||
|-----------|-------------|-------------|----------------------| | ||
| / | GET | READ | render main page | | ||
| /login | GET | READ | render login page | | ||
| /login | POST | CREATE | log in a user | | ||
| /register | GET | READ | render register page | | ||
| /register | POST | CREATE | register a new user | | ||
| /logout | GET | READ | log a user out | | ||
| /add | POST | CREATE | add a new location | | ||
| /user | GET | READ | get user info | | ||
|
||
##### (4) Locations Database and (5) Users Database | ||
|
||
To access, get the container id from `docker ps` and then open `psql`: | ||
|
||
```sh | ||
$ docker exec -ti <container-id> psql -U postgres | ||
``` | ||
|
||
##### (6) Functional Tests | ||
|
||
With the app running, update the `NODE_ENV environment variable and then run the tests`: | ||
|
||
```sh | ||
$ export NODE_ENV=test | ||
$ docker-compose up -d | ||
$ docker-compose run tests npm test | ||
``` | ||
|
||
|
||
Update `NODE_ENV` when you're ready to develop again: | ||
|
||
```sh | ||
$ export NODE_ENV=development | ||
$ docker-compose up -d | ||
``` | ||
|
||
#### Commands | ||
|
||
To stop the containers: | ||
|
||
```sh | ||
$ docker-compose stop | ||
``` | ||
|
||
To bring down the containers: | ||
|
||
```sh | ||
$ docker-compose down | ||
``` | ||
|
||
Want to force a build? | ||
|
||
```sh | ||
$ docker-compose build --no-cache | ||
``` | ||
|
||
Remove images: | ||
|
||
```sh | ||
$ docker rmi $(docker images -q) | ||
``` | ||
|
||
Run unit and integration tests: | ||
|
||
```sh | ||
$ export NODE_ENV=test | ||
$ docker-compose up -d | ||
$ docker-compose -f docker-compose-test.yml run users-service npm test | ||
$ docker-compose -f docker-compose-test.yml run locations-service npm test | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
version: '2.1' | ||
|
||
services: | ||
|
||
users-db: | ||
container_name: users-db | ||
build: ./services/users/src/db | ||
ports: | ||
- '5433:5432' | ||
environment: | ||
- POSTGRES_USER=admin | ||
- POSTGRES_PASSWORD=admin | ||
|
||
locations-db: | ||
container_name: locations-db | ||
build: ./services/locations/src/db | ||
ports: | ||
- '5432:5432' | ||
environment: | ||
- POSTGRES_USER=admin | ||
- POSTGRES_PASSWORD=admin | ||
|
||
users-service: | ||
container_name: users-service | ||
build: ./services/users/ | ||
volumes: | ||
- './services/users:/src/app' | ||
- './services/users/package.json:/src/package.json' | ||
ports: | ||
- '3000:3000' | ||
environment: | ||
- DATABASE_URL=postgres://admin:admin@users-db:5432/node_docker_api_users_dev | ||
- DATABASE_TEST_URL=postgres://admin:admin@users-db:5432/node_docker_api_users_test | ||
- NODE_ENV=${NODE_ENV} | ||
- TOKEN_SECRET=changeme | ||
depends_on: | ||
users-db: | ||
condition: service_started | ||
links: | ||
- users-db | ||
|
||
locations-service: | ||
container_name: locations-service | ||
build: ./services/locations/ | ||
volumes: | ||
- './services/locations:/src/app' | ||
- './services/locations/package.json:/src/package.json' | ||
ports: | ||
- '3001:3001' | ||
environment: | ||
- DATABASE_URL=postgres://admin:admin@locations-db:5432/node_docker_api_locations_dev | ||
- DATABASE_TEST_URL=postgres://admin:admin@locations-db:5432/node_docker_api_locations_test | ||
- NODE_ENV=${NODE_ENV} | ||
- TOKEN_SECRET=changeme | ||
- OPENWEATHERMAP_API_KEY=${OPENWEATHERMAP_API_KEY} | ||
depends_on: | ||
locations-db: | ||
condition: service_started | ||
users-service: | ||
condition: service_started | ||
links: | ||
- locations-db | ||
- users-service | ||
|
||
web: | ||
container_name: web | ||
build: ./web/ | ||
volumes: | ||
- './web:/src/app' | ||
- './web/package.json:/src/package.json' | ||
ports: | ||
- '3003:3003' | ||
environment: | ||
- NODE_ENV=${NODE_ENV} | ||
- SECRET_KEY=changeme | ||
depends_on: | ||
users-service: | ||
condition: service_started | ||
locations-service: | ||
condition: service_started | ||
links: | ||
- users-service | ||
- locations-service | ||
|
||
tests: | ||
container_name: tests | ||
build: ./tests/ | ||
volumes: | ||
- './tests:/src/app' | ||
- './tests/package.json:/src/package.json' | ||
depends_on: | ||
users-service: | ||
condition: service_started | ||
locations-service: | ||
condition: service_started | ||
links: | ||
- users-service | ||
- locations-service | ||
- web |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
#!/bin/sh | ||
|
||
docker-compose run users-service knex migrate:latest --env development --knexfile app/knexfile.js | ||
docker-compose run users-service knex seed:run --env development --knexfile app/knexfile.js | ||
docker-compose run locations-service knex migrate:latest --env development --knexfile app/knexfile.js | ||
docker-compose run locations-service knex seed:run --env development --knexfile app/knexfile.js |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
FROM node:latest | ||
|
||
# set working directory | ||
RUN mkdir /src | ||
WORKDIR /src | ||
|
||
# install app dependencies | ||
ENV PATH /src/node_modules/.bin:$PATH | ||
ADD package.json /src/package.json | ||
RUN npm install | ||
|
||
# start app | ||
CMD ["npm", "start"] |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
FROM postgres | ||
|
||
# run create.sql on init | ||
ADD create.sql /docker-entrypoint-initdb.d |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
FROM node:latest | ||
|
||
# set working directory | ||
RUN mkdir /src | ||
WORKDIR /src | ||
|
||
# install app dependencies | ||
ENV PATH /src/node_modules/.bin:$PATH | ||
ADD package.json /src/package.json | ||
RUN npm install | ||
|
||
# start app | ||
CMD ["npm", "start"] |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
FROM postgres | ||
|
||
# run create.sql on init | ||
ADD create.sql /docker-entrypoint-initdb.d |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
FROM node:latest | ||
|
||
# set working directory | ||
RUN mkdir /src | ||
WORKDIR /src | ||
|
||
# install app dependencies | ||
ENV PATH /src/node_modules/.bin:$PATH | ||
ADD package.json /src/package.json | ||
RUN npm install |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
FROM node:latest | ||
|
||
# set working directory | ||
RUN mkdir /src | ||
WORKDIR /src | ||
|
||
# install app dependencies | ||
ENV PATH /src/node_modules/.bin:$PATH | ||
ADD package.json /src/package.json | ||
RUN npm install | ||
|
||
# start app | ||
CMD ["npm", "start"] |