Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
.dockerignore
.git
docker-compose.yml
Dockerfile
node_modules
dist
data
logs
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ dist/
.tmp
logs/
src/Contexts/Mooc/Courses/infrastructure/persistence/courses.*
data
6 changes: 6 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
FROM node:12.16.3-slim

WORKDIR /code

COPY package.json package-lock.json ./
RUN npm install
39 changes: 39 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
.PHONY = default deps build test start clean start-database

IMAGE_NAME := codelytv/typescript-ddd-skeleton
SERVICE_NAME := app
Comment on lines +3 to +4
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Love it. 🎩


# Test if the dependencies we need to run this Makefile are installed
DOCKER := $(shell command -v docker)
DOCKER_COMPOSE := $(shell command -v docker-compose)
deps:
ifndef DOCKER
@echo "Docker is not available. Please install docker"
@exit 1
endif
ifndef DOCKER_COMPOSE
@echo "docker-compose is not available. Please install docker-compose"
@exit 1
endif
Comment on lines +6 to +17
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Very good one! 🙌


default: build

# Build image
build:
docker build -t $(IMAGE_NAME):dev .

# Run tests
test: build
docker-compose run --rm $(SERVICE_NAME) bash -c 'npm run build && npm run test'

# Start the application
start: build
docker-compose up $(SERVICE_NAME) && docker-compose down

# Clean containers
clean:
docker-compose down --rmi local --volumes --remove-orphans

# Start mongodb container in background
start_database:
docker-compose up -d mongo
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What do you think about adding the Node dependencies management to the Makefile in order to make it easier to handle them from within the Node Docker container?

You have an example in the PHP repository.

In that case, we even create a temporal Docker container to run Composer (the PHP dependencies manager). This way we don't need to install Composer in the PHP container, but because Node integrates it all in the same service, we could simplify it just directly interacting with the Node Container.

The goal behind this would be to ensure we install/update dependencies using the very same Node version that executes our code in an easy way (just a make) command 😊

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good idea! We will take a look and include that possibility here.

Copy link
Collaborator Author

@Fortiz2305 Fortiz2305 Jul 1, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure if I am missing something, but I think we are getting the requirement of ensuring that we install the dependencies using the same Node version every time we launch a make command. We are getting that by setting the build step as a dependency for all the rules.

Example:

# Build image
build:
	docker build  -t $(IMAGE_NAME):dev .

# Run tests
test: build
	docker-compose run --rm $(SERVICE_NAME) bash -c 'npm run build && npm run test'

Setting the build as a dependency of the test step we have a tradeoff: This adds some time in order to build the image, but it ensures that the dependencies are correct (it uses the docker cache and it will rebuild the image in case a dependency changes). Currently, it is not taking much time once the docker image was built for the first time so we will leave as it is for now. If this is slower in the future, we can create an install rule in the Makefile as you said.

Does it make sense for you?

25 changes: 25 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
version: '3.8'

services:
app:
build: .
command: bash -c "npm run build && npm run start"
ports:
- 3000:3000
environment:
- MONGO_URL=mongodb://mongo:27017/dev
depends_on:
- mongo
volumes:
- .:/code:delegated
- node_modules:/code/node_modules:delegated

mongo:
image: mongo:3.4.6
volumes:
- ./data/mongo:/data/db:delegated
ports:
- 27017:27017

volumes:
node_modules:
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
"start": "NODE_ENV=production node dist/src/apps/mooc_backend/server",
"build": "npm run build:clean && npm run build:tsc && npm run build:di",
"build:tsc": "tsc -p tsconfig.prod.json",
"build:di": "copy 'src/**/*.yaml' dist/src",
"build:di": "copy 'src/**/*.{json,yaml}' dist/src",
"build:clean": "rm -r dist; exit 0"
},
"dependencies": {
Expand Down
3 changes: 2 additions & 1 deletion src/apps/mooc_backend/config/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ const convictConfig = convict({
url: {
doc: 'The Mongo connection URL',
format: String,
env: 'MONGO_URL'
env: 'MONGO_URL',
default: 'mongodb://mongo:27017/dev'
}
}
});
Expand Down
1 change: 1 addition & 0 deletions src/apps/mooc_backend/config/production.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{}