Skip to content

Commit

Permalink
Trying to fix docker setup
Browse files Browse the repository at this point in the history
  • Loading branch information
kadraman committed May 3, 2024
1 parent 7399bbe commit 4a854b0
Show file tree
Hide file tree
Showing 10 changed files with 82 additions and 144 deletions.
9 changes: 5 additions & 4 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
FROM node:15.14.0
FROM node:lts-bullseye

LABEL maintainer="kevin.lee@microfocus.com"
LABEL maintainer="klee2@opentext.com"

# Add docker-compose-wait tool -------------------
ENV WAIT_VERSION 2.7.2
ENV WAIT_VERSION 2.12.1
ADD https://github.com/ufoscout/docker-compose-wait/releases/download/$WAIT_VERSION/wait /wait
RUN chmod +x /wait

# Create app directory
WORKDIR /usr/src/app
WORKDIR /home/node/app

# Install app dependencies
# A wildcard is used to ensure both package.json AND package-lock.json are copied
Expand All @@ -22,6 +22,7 @@ RUN npm install
# Bundle app source
ADD dist ./
COPY config ./config/
COPY mongodb ./mongodb/

# Make port 3000 available to the world outside this container
EXPOSE 3000
Expand Down
2 changes: 1 addition & 1 deletion config/production.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"App": {
"dbConfig": {
"host": "db",
"host": "mongodb",
"port": 27017,
"user": "iwa",
"password": "iwa",
Expand Down
36 changes: 22 additions & 14 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,46 +4,54 @@ services:
container_name: mongodb
restart: unless-stopped
env_file: .env
MONGO_INITDB_ROOT_USERNAME: $MONGO_USERNAME
MONGO_INITDB_ROOT_PASSWORD: $MONGO_PASSWORD
environment:
- MONGO_INITDB_ROOT_USERNAME=$MONGO_ROOT_USERNAME
- MONGO_INITDB_ROOT_PASSWORD=$MONGO_ROOT_PASSWORD
- MONGO_INITDB_DATABASE=$MONGO_DB
- MONGO_USERNAME=$MONGO_USERNAME
- MONGO_PASSWORD=$MONGO_PASSWORD
- MONGO_HOSTNAME=$MONGO_HOSTNAME
- MONGO_PORT=$MONGO_PORT
- MONGO_DB=$MONGO_DB
ports:
- "27017:27017"
environment:
MONGO_INITDB_ROOT_USERNAME: iwa
MONGO_INITDB_ROOT_PASSWORD: iwa
volumes:
- db:/data/db
- ./mongodb/init.sh:/docker-entrypoint-initdb.d/init.sh:ro
networks:
- iwa-api_net
- iwa-api

nodejs:
app:
build:
context: .
dockerfile: Dockerfile
image: nodejs
container_name: nodejs
container_name: iwa-api
restart: unless-stopped
env_file: .env
environment:
- NODE_ENV=production
- MONGO_USERNAME=$MONGO_USERNAME
- MONGO_PASSWORD=$MONGO_PASSWORD
- MONGO_HOSTNAME=mongodb
- MONGO_HOSTNAME=$MONGO_HOSTNAME
- MONGO_PORT=$MONGO_PORT
- MONGO_DB=$MONGO_DB
- WAIT_HOSTS=$MONGO_HOSTNAME:$MONGO_PORT
- WAIT_TIMEOUT=300
- WAIT_SLEEP_INTERVAL=30
- WAIT_HOST_CONNECT_TIMEOUT=30
ports:
- "3000:3000"
volumes:
- dist:/home/node/app
- node_modules:/home/node/app/node_modules
networks:
- iwa-api_net
command: ./wait-for.sh db:27017 -- NODE_ENV=production && cd /home/node/app/ && node dist/index.js
- iwa-api
command: sh -c "/wait && cd /home/node/app && node mongodb/populateDb.js && node index.js"

networks:
iwa-api_net:
iwa-api:
driver: bridge

volumes:
db:
dist:
node_modules:
14 changes: 14 additions & 0 deletions mongodb/init.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# init.sh
set -e

mongosh <<EOF
use admin
db.createUser({
user: '$MONGO_USERNAME',
pwd: '$MONGO_PASSWORD',
roles: [{
role: 'readWrite',
db: '$MONGO_DB'
}]
})
EOF
4 changes: 3 additions & 1 deletion mongodb/populateDb.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ const config = require('config');
const dbHost = config.get('App.dbConfig.host') || 'localhost';
const dbPort = config.get('App.dbConfig.port') || 27017;
const dbName = config.get('App.dbConfig.database') || 'iwa';
const mongoDB = `mongodb://${dbHost}:${dbPort}/${dbName}`;
const dbUser = config.get('App.dbConfig.user') || 'iwa';
const dbPassword = config.get('App.dbConfig.password') || 'iwa';
const mongoDB = `mongodb://${dbUser}:${dbPassword}@${dbHost}:${dbPort}/${dbName}?authSource=admin`;

const Product = require("./models/product");
const User = require("./models/user");
Expand Down
19 changes: 13 additions & 6 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 4 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@
"description": "IWA-API is an insecure Node/Express REST API for use in Fortify demonstrations",
"main": "index.ys",
"scripts": {
"build": "NODE_ENV=production && npx tsc",
"build": "NODE_ENV=production && npx tsc && ts-node src/configs/swagger.config.ts && cp src/configs/swagger_output.json ./dist",
"start": "NODE_ENV=production && node dist/index.js",
"dev": "ts-node-dev --respawn --transpile-only src/index.ts",
"test": "echo \"Error: no test specified\" && exit 1",
"swagger": "ts-node src/configs/swagger.config.ts",
"populate-db": "NODE_ENV=production && node mongodb/populateDb.js all",
"populate-db-dev": "node mongodb/populateDb.js all"
"populate-db-dev": "node mongodb/populateDb.js all"
},
"keywords": [],
"author": {
Expand All @@ -25,8 +25,9 @@
"@types/express-jwt": "^7.4.2",
"@types/helmet": "^4.0.0",
"@types/jsonwebtoken": "^9.0.4",
"@types/node": "^20.8.7",
"@types/node": "^20.12.8",
"ts-node-dev": "^2.0.0",
"tslib": "^2.6.2",
"typescript": "^4.9.5"
},
"dependencies": {
Expand Down
2 changes: 1 addition & 1 deletion src/configs/app.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ class AppConfig {
private dbHost: string = config.get('App.dbConfig.host') || 'localhost';
private dbPort: number = config.get('App.dbConfig.port') || 27017;
private dbName: string = config.get('App.dbConfig.database') || 'iwa';
public mongoUrl: string = `mongodb://${this.dbHost}:${this.dbPort}/${this.dbName}`;
public mongoUrl: string = `mongodb://${this.dbHost}:${this.dbPort}/${this.dbName}?authSource=admin`;

constructor() {
this.app = express();
Expand Down
52 changes: 19 additions & 33 deletions src/configs/swagger_output.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"info": {
"title": "IWA-API",
"version": "1.0",
"description": "IWA-API - An insecure Node/Express REST API for use in Fortify demonstrations. For this sample, you can use the api key demo-access-token to test the authorization filters.",
"description": "IWA-API - An insecure Node/Express REST API for use in Fortify demonstrations.",
"license": {
"name": "GPLv3",
"url": "https://www.gnu.org/licenses/gpl-3.0.en.html"
Expand Down Expand Up @@ -608,7 +608,7 @@
"description": "Gets all existing users searching by %keyword% format",
"parameters": [
{
"name": "authorization",
"name": "Authorization",
"in": "header",
"schema": {
"type": "string"
Expand Down Expand Up @@ -695,7 +695,7 @@
"description": "Creates a new user",
"parameters": [
{
"name": "authorization",
"name": "Authorization",
"in": "header",
"schema": {
"type": "string"
Expand Down Expand Up @@ -779,7 +779,7 @@
"description": "Id of the user to be retrieved. Cannot be empty."
},
{
"name": "authorization",
"name": "Authorization",
"in": "header",
"schema": {
"type": "string"
Expand Down Expand Up @@ -866,7 +866,7 @@
"description": "Id of the user to be updated. Cannot be empty."
},
{
"name": "authorization",
"name": "Authorization",
"in": "header",
"schema": {
"type": "string"
Expand Down Expand Up @@ -951,6 +951,13 @@
"type": "string"
},
"description": "Id of the user to be deleted. Cannot be empty."
},
{
"name": "Authorization",
"in": "header",
"schema": {
"type": "string"
}
}
],
"responses": {
Expand Down Expand Up @@ -1025,6 +1032,13 @@
"summary": "Get a user using query",
"description": "Gets an existing user using a MongoDb Query",
"parameters": [
{
"name": "Authorization",
"in": "header",
"schema": {
"type": "string"
}
},
{
"name": "q",
"description": "MongoDb query",
Expand Down Expand Up @@ -1106,13 +1120,6 @@
"summary": "Find products by keyword(s)",
"description": "Gets all existing products searching by %keyword% format",
"parameters": [
{
"name": "Authorization",
"in": "header",
"schema": {
"type": "string"
}
},
{
"name": "keywords",
"in": "query",
Expand Down Expand Up @@ -1276,13 +1283,6 @@
"type": "string"
},
"description": "Id of the product to be retrieved. Cannot be empty."
},
{
"name": "Authorization",
"in": "header",
"schema": {
"type": "string"
}
}
],
"responses": {
Expand Down Expand Up @@ -1539,13 +1539,6 @@
"type": "string"
}
},
{
"name": "Authorization",
"in": "header",
"schema": {
"type": "string"
}
},
{
"name": "name",
"description": "Id of the product image to be retrieved. Cannot be empty.",
Expand Down Expand Up @@ -1635,13 +1628,6 @@
"type": "string"
},
"description": "Name of the product image to be retrieved. Cannot be empty."
},
{
"name": "Authorization",
"in": "header",
"schema": {
"type": "string"
}
}
],
"responses": {
Expand Down
Loading

0 comments on commit 4a854b0

Please sign in to comment.