Skip to content

Commit

Permalink
Merge pull request #117 from iisisrael/docker-config
Browse files Browse the repository at this point in the history
Updates dev environment to use docker
  • Loading branch information
gitfrosh authored Apr 15, 2024
2 parents 9b8a129 + 0f54629 commit a7f9d90
Show file tree
Hide file tree
Showing 6 changed files with 185 additions and 20 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,5 @@ docker-compose.yml
dump
.DS_Store
bk
backend/dist
backend/dist
docker/db
101 changes: 82 additions & 19 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,37 +12,100 @@ With Version 2, this Open Source project is now open for contribution! You can h
- **Option 2**
- 👯 Clone this repo to your local machine using `https://github.com/gitfrosh/lotr-api.git`

### Start Mongo DB service

- you'll need MongoDB Community Edition running on your machine, listening on default port 27017
- start a shell and restore LotR data with `mongorestore -d lotr --verbose ./db/bson`

### Start Node / Express backend

- move into the backend folder `cd backend`
- install packages with `npm install`
- get your express server started with `node server.js` on localhost:3001

### Start React frontend

- move into the frontend folder `cd frontend`
- install packages with `npm install`
- get your development server started with `npm start` on localhost:3000
### Dependencies

- install Docker
- if running on Linux, follow [the install instructions for your distro](https://docs.docker.com/engine/install/#supported-platforms)
- otherwise, see [Rancher Desktop](https://rancherdesktop.io/)
- if running Rancher Desktop on Windows, after installing go to **File → Preferences → WSL → Integrations** and check the "**Ubuntu**" box
- build and run the node command line image, which will automatically install the packages for both the backend and frontend apps:
```
make build
```
- if you get an error like this:
```
ERROR: failed to solve: error getting credentials - err: exit status 127, out: ``
```
- create / log into your account on [dockerhub](https://hub.docker.com)
- create a new [Access Token](https://hub.docker.com/settings/security)
- log in on the command line with your usernamne and the access token as your password:
```
docker login
```
### Quick Start
- start all services with The One Command:
```
make up
```
- stop all services with The (Other) One Command:
```
make down
```
- for managing and accessing services individually, continue reading below
### Mongo DB service
- access the mongo command line on the running container:
```
docker exec -it lotr-mongo-1 mongosh
test> use lotr
lotr>
```
- the database files are stored in `./docker/db/` - to restore from the original, stop the running containers, delete that directory, and bring the environment back up:
```
make down
sudo rm -rf ./docker/db
make up
```
### Node / Express backend
- to manage node packages, run the CLI:
```
make cli
user@abc123:/app$ cd backend
user@abc123:/app/backend$ npm outdated
```
- if you want to watch the nodemon output as changes are made to application files, follow the container logs:
```
docker container logs -f lotr-backend-1
```
### React frontend
- to manage node packages, run the CLI:
```
make cli
user@abc123:/app$ cd frontend
user@abc123:/app/frontend$ npm outdated
```
- if you want to watch the webpack output as changes are made to application files, follow the container logs:
```
docker container logs -f lotr-frontend-1
```
### Create a user
- navigate to http://localhost:3000 and sign up
- use your favorite Mongo client to access user documents and get your new access_token for using the secured APIs
- OR login with your credentials in the Frontend to get your token
### Running tests
- navigate to the specific project (*backend* or *frontend*) and execute `npm test`.
- run the CLI, navigate to the specific project (*backend* or *frontend*), and execute `npm test`:
```
make cli
user@abc123:/app$ cd backend
user@abc123:/app/backend$ npm test
```
### **HACK AWAY!** 🔨🔨🔨
### 🔃 Create a new pull request
- using <a href="https://github.com/gitfrosh/lotr-api/compare" target="_blank">`https://github.com/gitfrosh/lotr-api/compare`</a>.
- using <a href="https://github.com/gitfrosh/lotr-api/compare" target="_blank">`https://github.com/gitfrosh/lotr-api/compare`</a>.
## Getting started with Data Improvement / Enhancement
Expand All @@ -56,4 +119,4 @@ The API data is far from perfect: There might be spelling mistakes, duplicates,
- move into the database folder `cd db/csv`
- make your improvements in one of the data CSV files
- 🔃 Create a new pull request using <a href="https://github.com/gitfrosh/lotr-api/compare" target="_blank">`https://github.com/gitfrosh/lotr-api/compare`</a>.
- 🔃 Create a new pull request using <a href="https://github.com/gitfrosh/lotr-api/compare" target="_blank">`https://github.com/gitfrosh/lotr-api/compare`</a>.
39 changes: 39 additions & 0 deletions docker/compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
name: lotr
services:
mongo:
build:
context: ..
dockerfile: docker/dockerfile
target: lotr_mongo
environment:
MONGO_INITDB_DATABASE: lotr
ports:
- 27017:27017
restart: on-failure
volumes:
- ./db:/data/db

backend:
# Using the debian version rather than alpine to support binary execution e.g. bcrypt
image: node:21.2
command: [ "npm", "run", "dev" ]
depends_on:
- mongo
environment:
DATABASE_URL: "mongodb://mongo:27017/lotr"
ports:
- 3001:3001
volumes:
- ../backend:/app
working_dir: /app

frontend:
image: node:21.2-alpine
command: [ "npm", "run", "start" ]
depends_on:
- backend
ports:
- 3000:3000
volumes:
- ../frontend:/app
working_dir: /app
40 changes: 40 additions & 0 deletions docker/dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
####################
### COMMAND LINE ###
####################

# Using the debian version rather than alpine, cuz bash and stuff
FROM node:21.2 as lotr_cli

WORKDIR /app

COPY ./docker/npm-build.sh .

RUN chmod +x npm-build.sh

# Configure the user's id and group id to match the host file system
RUN userdel -r node

ARG USER_ID

ARG GROUP_ID

RUN getent group $GROUP_ID || addgroup --gid $GROUP_ID user

RUN getent passwd $USER_ID || adduser --disabled-password --gecos '' --uid $USER_ID --gid $GROUP_ID user

# Set the active user and open the interactive terminal
USER user

ENTRYPOINT [ "bash" ]



################
### DATABASE ###
################

FROM mongo:7.0-jammy as lotr_mongo

COPY ./db/bson /data/init

RUN echo "mongorestore --archive='/data/init'" > /docker-entrypoint-initdb.d/init.sh
8 changes: 8 additions & 0 deletions docker/npm-build.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#!/bin/bash

# this file is copied into the CLI image to automate installing node dependencies

cd backend
npm install --include=dev
cd ../frontend
npm install --include=dev
14 changes: 14 additions & 0 deletions makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# build the command line interface and install node packages
build:
docker build --build-arg "USER_ID=$$(id -u)" --build-arg "GROUP_ID=$$(id -g)" -f ./docker/dockerfile --target lotr_cli -t lotr-cli:latest .
docker run -v ./backend:/app/backend -v ./frontend:/app/frontend --entrypoint /app/npm-build.sh -it --rm --name lotr-cli lotr-cli

# start and stop the local environment
up:
docker compose -f docker/compose.yml up -d
down:
docker compose -f docker/compose.yml down

# run the command line interface to manage application dependencies and run tests
cli:
docker run -v ./backend:/app/backend -v ./frontend:/app/frontend -it --rm --name lotr-cli lotr-cli

0 comments on commit a7f9d90

Please sign in to comment.