Skip to content

Commit 066980b

Browse files
committed
fix: Dockerfile updates to work out of the box
This cleans up our Docker implementation to allow easier use with docker-compose And with Docker in general. Additionally this will cleanup some of our README Documentation and `.env` use. resolves #63 resolves #31
1 parent 059bd17 commit 066980b

File tree

7 files changed

+33
-86
lines changed

7 files changed

+33
-86
lines changed

.travis.yml

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,6 @@ script:
1717
after_script:
1818
- nyc --reporter=text-lcov npm test > coverage.lcov
1919
- codecov
20-
- >
21-
if [ "$TRAVIS_BRANCH" == "master" ] && [ "TRAVIS_PULL_REQUEST" == "false" ]; then
22-
echo "$DOCKER_PASSWORD" | docker login -u "$DOCKER_USERNAME" --password-stdin
23-
docker build -t codeforbaltimore/bmore-responsive .
24-
docker push codeforbaltimore/bmore-responsive
25-
else
26-
echo "Will not deploy to Dockerhub"
27-
fi
2820
notifications:
2921
slack:
3022
secure: U4+tEb6IRX5XHNoX/cCBr0jxEbCINpWbhX3D4kHf2YOPEKtdGeZFZh/hqAwXSRRdQIXiEvmrRTD42IEZwWK4HgZ6ZTeLF2C17afg7o0kvxHbVhp+1PS9/BFUx0+5KrYB8Ew4US8f/qq6myCx7gtI9dZImH2m5EpgmTHTumEWkGK5PHJBbwL/0GYsO551eT2QimaBvUvAIe++rB5fK0MveCaT2T1ZmprQTQWLRY2tLXbKyBTpiIgC73lbXWCadO2MbBCx7dFSIJVfFWWa86E7dXBfK1Nk4EClMYaAdnqM+mFLx01NINOrpYuzXt8RQivP9OAitTdDpqwIqJcmn+uE3r48w6Hf9onojZfqbytOMIvybgPlLPOhZGwRFFa9xZWUG4JT6X747t/RcrEQXl8wbbm7QaRKLk60zgLH0ysvUFLr6cl+CCzZqTmRbWgGh5NiNS1U6XrjiQR4GVZkKPwLjx9+1ZgaayPkzaFReOETgc72X1WJClkm108R2lLfqvn9iF4C19EmuQpLehzXFXIDN5kHHvKFhbG4jiZwYwMQH0R1OqYhrwB3QWO1Scco6Z7zUcrN9S0KDu4p46sUoLsrk6+DP8VbHDMpiPt0wPOJnvzSeoqYgSYYMuCITr/VMvz6VS7E65swNHMJEWsz+SDsLUnamhUHanMPJzGrk7zmnBY=

Dockerfile

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -5,24 +5,6 @@ FROM node:12.11.0-alpine
55
RUN mkdir -p /usr/src
66
WORKDIR /usr/src
77

8-
# Arguments
9-
ARG node_env=development
10-
ARG port=3000
11-
ARG database=postgres
12-
ARG database_user=postgres
13-
ARG database_password=postgres
14-
ARG database_schema=public
15-
ARG jwt_key=abc_123
16-
17-
# Environment variables
18-
ENV NODE_ENV $node_env
19-
ENV PORT $port
20-
ENV DATABASE $database
21-
ENV DATABASE_USER $database_user
22-
ENV DATABASE_PASSWORD $database_password
23-
ENV DATABASE_SCHEMA $database_schema
24-
ENV JWT_KEY $jwt_key
25-
268
# Install app dependencies
279
COPY ./package*.json ./
2810
RUN npm install

README.md

Lines changed: 27 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
11
[![Build Status](https://travis-ci.org/CodeForBaltimore/Bmore-Responsive.svg?branch=master)](https://travis-ci.org/CodeForBaltimore/Bmore-Responsive) [![codecov](https://codecov.io/gh/CodeForBaltimore/Bmore-Responsive/branch/master/graph/badge.svg)](https://codecov.io/gh/CodeForBaltimore/Bmore-Responsive) [![Known Vulnerabilities](https://snyk.io/test/github/CodeForBaltimore/Bmore-Responsive/badge.svg)](https://snyk.io/test/github/CodeForBaltimore/Bmore-Responsive)
2-
3-
# Bmore Responsive
2+
<!-- TOC -->autoauto- [1. Bmore Responsive](#1-bmore-responsive)auto - [1.1. Documentation](#11-documentation)auto - [1.1.1. API Spec](#111-api-spec)auto - [1.1.2. Database Documentation](#112-database-documentation)auto- [2. Setup](#2-setup)auto - [2.1. Node and Express setup](#21-node-and-express-setup)auto - [2.2. Environment variables](#22-environment-variables)auto - [2.3. PostgreSQL](#23-postgresql)auto - [2.3.1. Sequelize](#231-sequelize)auto - [2.4. Docker](#24-docker)auto- [3. Using this product](#3-using-this-product)auto - [3.1. Testing](#31-testing)auto- [4. Sources and Links](#4-sources-and-links)autoauto<!-- /TOC -->
3+
# 1. Bmore Responsive
44
An API to drive disaster and emergency response systems.
55

6-
## Documentation
6+
## 1.1. Documentation
77
We've included a `docs` folder with a template [Tech Spec](/docs/Tech_Spec.md) and [Best Practices](/docs/Best_Practices.md) document, though using Github's Wiki capabilities is also a good idea. This will get you started with documenting your project. Other documents and relevant information that has no other place can live in the `docs` folder. Replace this paragraph with a brief breakdown of what you've included in your `docs` folder.
88

9-
### API Spec
9+
### 1.1.1. API Spec
1010
Our API spec is on Swagger. You can view it here https://app.swaggerhub.com/apis/codeforbaltimore/bmoreResponsive/1.0.0#/ or you can find the `swagger.json` file in our `docs` folder.
1111

12-
### Database Documentation
12+
### 1.1.2. Database Documentation
1313
Our database documentation can be found in our `docs` folder under `database.csv`. This documentation was created using SchemaSpy. Instructions for use can be found here https://github.com/bcgov/schemaspy
1414

15-
## Setup
15+
# 2. Setup
1616
A `Dockerfile` and `docker-compose` file have been included for convenience, however this may not be the best local setup for this project. For more information on how to use Docker with this project, please see the [docker section](#docker).
1717

1818
To work on this project you should have:
@@ -21,38 +21,31 @@ To work on this project you should have:
2121
- Docker (optional)
2222
Once you have the prerequisite software installed you can proceed to setup this application.
2323

24-
### Node and Express setup
24+
## 2.1. Node and Express setup
2525
This application is designed to work as an API driven by Express. To setup your environment first you must install all required dependencies by running the following command from the root of your project directory:
2626
```
2727
npm install
2828
```
2929
Once all dependencies are installed you will need to setup some environment variables to interact with your database and application.
3030

31-
### Environment variables
31+
## 2.2. Environment variables
3232
You will need to set some local environment variables to run this application. This is true even if you're using Docker.
3333
```
3434
touch .env
3535
echo 'NODE_ENV=local
3636
PORT=<your port>
37-
DATABASE_HOST=<your database host>
38-
DATABASE=<your database name>
39-
DATABASE_USER=<your database user>
40-
DATABASE_PASSWORD=<your database password>
4137
DATABASE_SCHEMA=<your database schema>
4238
JWT_KEY=<your secret JWT seed phrase or key>
4339
DATABASE_URL_DEV=<your connection string based on above variables>
4440
' >> ./.env
4541
```
4642

47-
And example of the `DATABASE_URL_DEV` would be `DATABASE_URL_DEV=postgres://postgres:.@localhost:5432/postgres`
43+
And example of the `DATABASE_URL` would be `DATABASE_URL=postgres://user:pass@example.com:5432/dbname`
4844

4945
The various variables are defined as follows:
5046
- `NODE_ENV` = The label for your environment.
5147
- `PORT` = The local port you wish to run on. Defaults to `3000`.
52-
- `DATABASE_HOST` = The hostname of your db. _Probably_ `localhost` but if you're using our `docker-compose.yml` set it to `db`.
53-
- `DATABASE` = Your database name. Postgres default is `postgres`.
54-
- `DATABASE_USER` = Your local database login username. Postgres default is `postgres`.
55-
- `DATABASE_PASSWORD` = Your local database login password. Postgres default is `postgres`.
48+
- `DATABASE_URL` = The URL string for your db connection. For example: `postgres://user:pass@example.com:5432/dbname`
5649
- `DATABASE_SCHEMA` = Your local database schema. Postgres default is `public`.
5750
- `JWT_KEY` = A secret value to generate JWT's locally.
5851
- `BYPASS_LOGIN` = _optional_ Allows you to hit the endpoints locally without having to login. If you wish to bypass the login process during local dev, set this to `true`.
@@ -63,19 +56,17 @@ _We do not recommend using the default options for PostgreSQL. The above values
6356
- `DATABASE_HOST`: The IP address Docker is running on. You can find this by running `docker-machine ip` but it's usually `192.168.99.100` instead of `localhost`
6457
- `DATABASE_URL_DEV`: This will need to be adjusted as well, for example `DATABASE_URL_DEV=postgres://postgres:.@localhost:5432/postgres` would become `DATABASE_URL_DEV=postgres://postgres:.@192.168.99.100:5432/postgres`
6558

66-
### PostgreSQL
59+
## 2.3. PostgreSQL
6760
***You will need a PostgreSQL database running locally to run this application locally.*** You may setup PostgreSQL however you wish, however we recommend using Docker using the instructions found here: https://hub.docker.com/_/postgres
6861

6962
If you are using the Docker method you may spin up your database layer by running this command:
7063
```
71-
docker run -d -e POSTGRES_HOST_AUTH_METHOD=trust -p 5432:5432 postgres
64+
docker run -d -e POSTGRES_PASSWORD=<your chosen password> -p 5432:5432 postgres
7265
```
7366

74-
_Note that POSTGRES_HOST_AUTH_METHOD=trust is insecure and will allow anything to connect to that db! Only use for testing and/or local dev!_
75-
7667
If you're running a database in another way then we trust you can sort it out on your own because you're awesome :sunglasses:
7768

78-
### Sequelize _<optional>_
69+
### 2.3.1. Sequelize
7970
You can run the application without doing anything and it will create the tables needed to operate automatically. It will not, however, create users. If you would like to seed your database with users you will need to follow a few steps.
8071
1. You may create your database tables without running the application by running `npm run db-create`.
8172
2. You can now seed your database by running `npm run db-seed`.
@@ -86,30 +77,35 @@ To create new models, migrations, and seeders you _must_ use the Sequelize CLI c
8677
- `npx sequelize-cli model:generate --name User --attributes firstName:string,lastName:string,email:string` - Creates a model under `/src/models` and a migration script.
8778
- `npx sequelize-cli seed:generate --name demo-user` - Creates a seeder for the `User` model and migration previously setup.
8879

89-
### Docker
80+
## 2.4. Docker
9081
To use the `docker-compose.yml` file included you will first need to set [environment variables](#environment-variables). You **MUST** set your `DATABASE_HOST` to `db` to use the `docker-compose` solution.
9182

92-
If you are running your own database, but want to use the `Dockerfile` you will need to run that this way on a Mac:
83+
If you are running your own database, but want to use the `Dockerfile` you will need to run that this way:
9384
```
9485
docker build -t bmoreres .
95-
docker run -d -p 3000:3000 -e DATABASE_HOST=docker.for.mac.host.internal bmoreres
86+
docker run -d -p 3000:3000 --env-file=.env bmoreres
9687
```
97-
On Windows you would run:
88+
Note that `DATABASE_URL` host location will be different depending on what OS you're using. On Mac it is `docker.for.mac.host.internal` and on Windows it is `docker.for.win.host.internal` if using `docker-compose` it will be `db`
89+
90+
You can manually set the environment variables and not use a `.env` file by setting the following vars:
9891
```
99-
docker build -t bmoreres .
100-
docker run -d -p 3000:3000 -e DATABASE_HOST=docker.for.win.host.internal bmoreres
92+
-e NODE_ENV=development
93+
-e PORT=3000
94+
-e DATABASE_DATABASE_SCHEMA=<your database schema>
95+
-e JWT_KEY=<your JWT phrase>
96+
-e DATABASE_URL=<your connection string>
10197
```
10298

103-
## Using this product
99+
# 3. Using this product
104100
You may use this product to create and manage users for your front-end. More to come!
105101
To run the application--after the above steps are completed--run `npm start`.
106102

107-
## Testing
103+
## 3.1. Testing
108104
To test your code you may write test cases to `./index.spec.js` and then run the tests with `npm test`.
109105

110106
To check your linting you may run `npm run lint` and to format and automatically fix your formatting run `npm run format`.
111107

112-
## Sources and Links
108+
# 4. Sources and Links
113109
We are also building a front-end application called [Healthcare Rollcall](https://github.com/CodeForBaltimore/Healthcare-Rollcall) to interact with this backend API. To view that project, or to contribute to it, please visit the repo here: https://github.com/CodeForBaltimore/Healthcare-Rollcall
114110

115111
We will be including multi-repo build processes with the front-end that will reference this project.

docker-compose.yml

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,16 @@
11
version: '3'
22
services:
33
api:
4-
build: .
4+
image: bmoreres
55
depends_on:
66
- db
77
links:
88
- "db: database"
99
ports:
1010
- '3000:3000'
11-
command: npm start
11+
command: >
12+
sh -c "npm run db-create &&
13+
npm start"
1214
volumes:
1315
- .:/app/
1416
- /app/node_modules

scripts/dockerrun.sh

Lines changed: 0 additions & 22 deletions
This file was deleted.

sequelize/config.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
require('dotenv').config();
22
module.exports = {
33
development: {
4-
use_env_variable: 'DATABASE_URL_DEV',
4+
use_env_variable: 'DATABASE_URL',
55
dialect: 'postgres',
66
}
77
};

src/models/index.js

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,8 @@ import _ from 'lodash';
55

66
// Initializes the database.
77
const sequelize = new Sequelize(
8-
process.env.DATABASE,
9-
process.env.DATABASE_USER,
10-
process.env.DATABASE_PASSWORD,
8+
process.env.DATABASE_URL,
119
{
12-
host: process.env.DATABASE_HOST,
1310
dialect: 'postgres'
1411
}
1512
);

0 commit comments

Comments
 (0)