-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #27 from wearefuturegov/feature/containerised
Update dockerfile + documentation + gh actions
- Loading branch information
Showing
7 changed files
with
127 additions
and
119 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 |
---|---|---|
@@ -1,13 +1,14 @@ | ||
# ----- DOCKER COMPOSE | ||
COMPOSE_PROJECT_NAME=outpost-api | ||
|
||
|
||
# ----- MONGO CONFIG | ||
|
||
# -- using docker development env | ||
DATABASE_URL=mongodb://mongo/outpost_api_development | ||
|
||
# -- running mongo locally | ||
#DATABASE_URL=mongodb://localhost:27017/outpost_api_development | ||
|
||
# -- running with docker and connecting to db on host | ||
#DATABASE_URL=mongodb://host.docker.internal:27017/outpost_development | ||
|
||
|
||
# ----- GEOCODING | ||
GOOGLE_API_KEY= |
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,17 @@ | ||
name: CI | ||
on: push | ||
jobs: | ||
test: | ||
runs-on: ubuntu-latest | ||
steps: | ||
- uses: actions/checkout@v3 | ||
|
||
- uses: actions/setup-node@v3 | ||
with: | ||
node-version: 16 | ||
|
||
- name: Install dependencies | ||
run: npm ci | ||
|
||
- name: Run tests | ||
run: npm run 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
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
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,37 @@ | ||
version: "3.7" | ||
services: | ||
outpost-api: | ||
image: "outpost-api:production" | ||
container_name: outpost-api | ||
restart: unless-stopped | ||
depends_on: | ||
- mongo | ||
build: | ||
target: production | ||
ports: | ||
- "3001" | ||
volumes: | ||
- ./:/app | ||
environment: | ||
NODE_ENV: production | ||
networks: | ||
- internal_network | ||
|
||
mongo: | ||
image: mongo:6 | ||
container_name: outpost-api-mongo | ||
ports: | ||
- 27017:27017 | ||
volumes: | ||
- outpost-api-mongo-volume:/data/db | ||
networks: | ||
- internal_network | ||
- external_network | ||
|
||
volumes: | ||
outpost-api-mongo-volume: | ||
|
||
networks: | ||
external_network: | ||
internal_network: | ||
internal: true |
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 |
---|---|---|
|
@@ -38,7 +38,7 @@ It expects a few environment variables. | |
|
||
`DATABASE_URL` | ||
|
||
- MongoDB connection URI | ||
- MongoDB connection URI nb if you're running in a docker container and want to connect to your local db use `host.docker.internal` instead of `localhost` | ||
|
||
`GOOGLE_API_KEY` | ||
|
||
|
@@ -53,39 +53,64 @@ Other environmental variables: | |
|
||
--- | ||
|
||
## Getting started | ||
## 💻 Getting started | ||
|
||
There are three ways to run the Outpost API locally | ||
### Using docker | ||
|
||
1. As an entire project, alongside [Outpost](https://github.com/wearefuturegov/outpost) and [Scout](https://github.com/wearefuturegov/scout-x), please see the [Outpost](https://github.com/wearefuturegov/outpost) documentation. | ||
2. On its own - using docker-compose | ||
3. On its own - using your own machine | ||
```sh | ||
git clone [email protected]:wearefuturegov/outpost-api-service.git && cd outpost-api-service | ||
|
||
# build the image - if using for the first time | ||
docker build --tag outpost-api-service:development --target development . | ||
|
||
# run the image in local environment | ||
docker run -p 3001:3001 --name outpost-api-service -v $(pwd):/app:cached -i -d outpost-api-service:development | ||
|
||
# setup indices | ||
docker exec -it outpost-api-service npm run prepare-indices | ||
|
||
### On its own - using docker-compose | ||
# access the site | ||
open http://localhost:3001/api/v1/services | ||
|
||
- Requires docker quick to setup and run no worrying about node versions | ||
# open shell in container | ||
docker exec -it outpost-api-service /bin/ash; | ||
|
||
# run tests | ||
docker exec -it outpost-api-service npm run test | ||
|
||
# stop the container | ||
docker stop outpost-api-service | ||
|
||
# start again | ||
docker start outpost-api-service | ||
``` | ||
|
||
### Using docker-compose | ||
|
||
```sh | ||
# checkout the code | ||
git clone \ | ||
[email protected]:wearefuturegov/outpost-api-service.git \ | ||
&& cd outpost-api-service | ||
git clone [email protected]:wearefuturegov/outpost-api-service.git && cd outpost-api-service | ||
|
||
# build the image | ||
make build | ||
docker compose -f docker-compose.development.yml build | ||
|
||
# start the containers | ||
make start | ||
# run the container | ||
docker compose -f docker-compose.development.yml up -d | ||
|
||
# (if this is your first time installing - prepare the db) | ||
make prepare_db | ||
# setup indices | ||
docker compose -f docker-compose.development.yml exec outpost-api-dev npm run prepare-indices; | ||
|
||
# outpost api is running on http://localhost:3001 | ||
``` | ||
# open shell in container | ||
docker compose -f docker-compose.development.yml exec outpost-api-dev /bin/ash; | ||
|
||
# run tests | ||
docker compose -f docker-compose.development.yml exec outpost-api-dev npm run test | ||
|
||
To change the port edit the ports in docker-compose file to portyouwant:3001, then run `make stop && make build && make start` | ||
# stop the container | ||
docker compose -f docker-compose.development.yml stop | ||
|
||
### On its own - using your own machine | ||
``` | ||
|
||
### On your machine | ||
|
||
To run it on your machine you need Node.js, npm, nvm (https://github.com/nvm-sh/nvm) and a working MongoDB database [with the right indices](#indices) available on `localhost:27017`. | ||
|
||
|
@@ -107,17 +132,6 @@ npm run dev | |
|
||
## Deploying it | ||
|
||
Build the image and push to container repository | ||
|
||
```sh | ||
# Build it locally | ||
docker build --no-cache --tag outpost-api:production --target production . | ||
|
||
# Build it ready to push to dockerhub | ||
docker build --no-cache --tag apricot13/outpost-api:production --target production . | ||
docker push apricot13/outpost-api:production | ||
``` | ||
|
||
[![Deploy](https://www.herokucdn.com/deploy/button.svg)](https://heroku.com/deploy) | ||
|
||
It's suitable for 12-factor hosting like Heroku. It has a [Procfile](https://devcenter.heroku.com/articles/procfile) that will make sure the proper MongoDB indices are set up. | ||
|
@@ -126,6 +140,16 @@ It's suitable for 12-factor hosting like Heroku. It has a [Procfile](https://dev | |
npm start | ||
``` | ||
|
||
You can also deploy via docker | ||
|
||
```sh | ||
# build the image | ||
docker compose build | ||
|
||
# run the container | ||
docker compose up -d | ||
``` | ||
|
||
## Indices | ||
|
||
It needs the right indices on the MongoDB collection to enable full-text and geo search. Something like: | ||
|