Skip to content

Commit

Permalink
Merge pull request #27 from wearefuturegov/feature/containerised
Browse files Browse the repository at this point in the history
Update dockerfile + documentation + gh actions
  • Loading branch information
apricot13 authored Feb 28, 2023
2 parents f08ecb4 + fa68256 commit 3da1f5b
Show file tree
Hide file tree
Showing 7 changed files with 127 additions and 119 deletions.
9 changes: 5 additions & 4 deletions .env.sample
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=
17 changes: 17 additions & 0 deletions .github/workflows/ci.yml
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
11 changes: 5 additions & 6 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -5,24 +5,23 @@
FROM node:16-alpine3.17 as base_image


# copy node files and install
FROM base_image as build_frontend
COPY ./package.json ./tmp/package.json
COPY ./package-lock.json ./tmp/package-lock.json
RUN cd /tmp && \
npm ci
# ENTRYPOINT ["tail", "-f", "/dev/null"]
npm i
WORKDIR /app


# build and install all the things for the development env
FROM build_frontend as development

COPY --from=build_frontend ./tmp ./app
EXPOSE 3001
CMD ["npm", "run", "dev" ]


# build and install all the things for the development env
FROM build_frontend as production
COPY --from=build_frontend ./tmp ./
COPY . /app
CMD ["npm", "run", "start"]
EXPOSE 3001
CMD ["npm", "run", "start" ]
21 changes: 7 additions & 14 deletions docker-compose.development.yml
Original file line number Diff line number Diff line change
@@ -1,18 +1,15 @@
version: "3.7"
services:
outpost-api:
env_file: ./.env
image: "outpost-api/outpost-api:development"
container_name: outpost-api-app
command: "npm run start"
stdin_open: true
image: "outpost-api:development"
container_name: outpost-api-dev
depends_on:
- mongo
build:
context: ./
dockerfile: ./Dockerfile
target: development
ports:
- 3001:3001
- "3001"
volumes:
- ./:/app:cached
environment:
Expand All @@ -21,22 +18,18 @@ services:
- external_network
- internal_network
mongo:
image: "outpost-api/mongo:development"
image: mongo:6
container_name: outpost-api-mongo
build:
context: ./
dockerfile: ./environment/containers/mongo/Dockerfile
target: development
ports:
- 27017:27017
volumes:
- mongo-volume:/data/db
- outpost-api-mongo-volume:/data/db
networks:
- internal_network
- external_network

volumes:
mongo-volume:
outpost-api-mongo-volume:

networks:
external_network:
Expand Down
37 changes: 37 additions & 0 deletions docker-compose.yml
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
63 changes: 0 additions & 63 deletions makefile

This file was deleted.

88 changes: 56 additions & 32 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`

Expand All @@ -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`.

Expand All @@ -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.
Expand All @@ -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:
Expand Down

0 comments on commit 3da1f5b

Please sign in to comment.