Skip to content

Commit

Permalink
feat: dockerize application
Browse files Browse the repository at this point in the history
  • Loading branch information
lotyp committed Dec 9, 2022
1 parent 6b562e6 commit 43195ba
Show file tree
Hide file tree
Showing 6 changed files with 293 additions and 58 deletions.
17 changes: 17 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
APP_NAME=${APP_NAME}
APP_URL=https://${APP_NAME}.${PROJECT_SERVICES_NAMESPACE}.docker

# Should be same with downloaded and configured docker-shared-services
# Default: ss - ([S]hared [S]ervices)
# More info in repository docs: https://github.com/wayofdev/docker-shared-services
SHARED_SERVICES_NAMESPACE=ss

# Should be same with downloaded and configured docker-project-services
# Default: wod
# More info in repository docs: https://github.com/wayofdev/docker-project-services
PROJECT_SERVICES_NAMESPACE=wod

# https://docs.docker.com/compose/reference/envvars/#compose_project_name
# With custom namespace provided, it will be used to prefix all services
# in Docker network for current project
COMPOSE_PROJECT_NAME=nextjs-${APP_NAME}
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
node_modules
.pnp
.pnp.js
.pnpm-store

# testing
coverage
Expand Down Expand Up @@ -35,6 +36,7 @@ yarn-error.log*
.pnpm-debug.log*

# local env files
.env
.env.local
.env.development.local
.env.test.local
Expand Down
182 changes: 182 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,182 @@
-include .env

# BuildKit enables higher performance docker builds and caching possibility
# to decrease build times and increase productivity for free.
export DOCKER_BUILDKIT ?= 1

export SHARED_SERVICES_NETWORK = $(addsuffix _network,$(subst $e.,_,$(SHARED_SERVICES_NAMESPACE)))

# Binary to use, when executing docker-compose tasks
DOCKER_COMPOSE ?= docker-compose

# Support image with all needed binaries, like envsubst, mkcert, wait4x
SUPPORT_IMAGE ?= wayofdev/build-deps:alpine-latest

BUILDER_PARAMS ?= docker run --rm -i \
--env-file ./.env \
--env APP_NAME=$(APP_NAME) \
--env SYSTEM_SERVICES_NAMESPACE=$(SYSTEM_SERVICES_NAMESPACE) \
--env PROJECT_SERVICES_NAMESPACE=$(PROJECT_SERVICES_NAMESPACE) \
--env SHARED_SERVICES_NETWORK=$(SHARED_SERVICES_NETWORK)

BUILDER ?= $(BUILDER_PARAMS) $(SUPPORT_IMAGE)

# Shorthand envsubst command, executed through build-deps
ENVSUBST ?= $(BUILDER) envsubst

APP_RUNNER ?= $(DOCKER_COMPOSE) run --rm --no-deps app
NPM_BIN ?= /bin/pnpm
NPM_RUNNER ?= $(APP_RUNNER) $(NPM_BIN)


# Self documenting Makefile code
# ------------------------------------------------------------------------------------
ifneq ($(TERM),)
BLACK := $(shell tput setaf 0)
RED := $(shell tput setaf 1)
GREEN := $(shell tput setaf 2)
YELLOW := $(shell tput setaf 3)
LIGHTPURPLE := $(shell tput setaf 4)
PURPLE := $(shell tput setaf 5)
BLUE := $(shell tput setaf 6)
WHITE := $(shell tput setaf 7)
RST := $(shell tput sgr0)
else
BLACK := ""
RED := ""
GREEN := ""
YELLOW := ""
LIGHTPURPLE := ""
PURPLE := ""
BLUE := ""
WHITE := ""
RST := ""
endif
MAKE_LOGFILE = /tmp/next-starter-tpl.log
MAKE_CMD_COLOR := $(BLUE)

default: all

help: ## Show this menu
@echo 'Management commands for package:'
@echo 'Usage:'
@echo ' ${MAKE_CMD_COLOR}make${RST} Prepares and spins up project with default settings'
@grep -E '^[a-zA-Z_0-9%-]+:.*?## .*$$' Makefile | awk 'BEGIN {FS = ":.*?## "}; {printf " ${MAKE_CMD_COLOR}make %-21s${RST} %s\n", $$1, $$2}'
@echo
@echo ' 📑 Logs are stored in $(MAKE_LOGFILE)'
@echo
@echo ' 📦 Package next-starter-tpl (github.com/wayofdev/next-starter-tpl)'
@echo ' 🤠 Author Andrij Orlenko (github.com/lotyp)'
@echo ' 🏢 ${YELLOW}Org wayofdev (github.com/wayofdev)${RST}'
.PHONY: help

.EXPORT_ALL_VARIABLES:


# Default action
# Defines default command when `make` is executed without additional parameters
# ------------------------------------------------------------------------------------
all: install hooks up
.PHONY: all


# System Actions
# ------------------------------------------------------------------------------------
env: ## Generate .env file from example, use `make env force=true`, to force re-create file
ifeq ($(FORCE),true)
@echo "${YELLOW}Force re-creating .env file from example...${RST}"
$(ENVSUBST) < ./.env.example > ./.env
else ifneq ("$(wildcard ./.env)","")
@echo ""
@echo "${YELLOW}The .env file already exists! Use FORCE=true to re-create.${RST}"
else
@echo "Creating .env file from example"
$(ENVSUBST) < ./.env.example > ./.env
endif
.PHONY: env

install:
$(NPM_RUNNER) install
.PHONY: install

update:
$(NPM_RUNNER) update
.PHONY: update

build:
$(NPM_RUNNER) run build
.PHONY: build


# Docker Actions
# ------------------------------------------------------------------------------------
up: ## Spin up this project using docker
$(DOCKER_COMPOSE) up --remove-orphans -d
@echo "🚀 Started and available at https://$(APP_NAME).$(PROJECT_SERVICES_NAMESPACE).docker"
.PHONY: up

down: ## Stops and removes all project containers
$(DOCKER_COMPOSE) down --remove-orphans
.PHONY: down

restart: down up ## Removes containers and stops, then starts new instances
.PHONY: restart

clean:
$(DOCKER_COMPOSE) rm --force --stop
.PHONY: clean

ps: ## Show running containers
$(DOCKER_COMPOSE) ps
.PHONY: ps

logs: ## Show all project docker logs
$(DOCKER_COMPOSE) logs -f
.PHONY: logs

ssh:
$(DOCKER_COMPOSE) run --rm -it app sh
.PHONY: ssh

recreate:
rm -rf .pnpm-store node_modules
$(DOCKER_COMPOSE) build app
.PHONY: recreate


# Testing and Code Quality
# ------------------------------------------------------------------------------------
lint: ## Run eslint task
# $(NPM_RUNNER) lint
$(DOCKER_COMPOSE) exec -T app $(NPM_BIN) run lint
.PHONY: lint

lint-staged:
$(DOCKER_COMPOSE) exec -T app $(NPM_BIN) run lint-staged
.PHONY: lint-staged

commitlint:
$(DOCKER_COMPOSE) exec -T app $(NPM_BIN) run --no-install commitlint --edit "${1}"
.PHONY: commitlint

test: ## Run unit tests
$(DOCKER_COMPOSE) exec -T app $(NPM_BIN) run test
.PHONY: test

format: ## Run prettier formatting
$(DOCKER_COMPOSE) exec -T app $(NPM_BIN) run format
.PHONY: format


# Yaml Actions
# ------------------------------------------------------------------------------------
ylint: ## Lints yaml files inside project
yamllint .
.PHONY: ylint


# Git Actions
# ------------------------------------------------------------------------------------
hooks: ## Install git hooks from husky
$(NPM_RUNNER) run prepare
.PHONY: hooks
28 changes: 28 additions & 0 deletions docker-compose.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
---
version: '3.9'

services:
app:
image: wayofdev/node:19-alpine-latest
container_name: ${APP_NAME}_app
restart: on-failure
networks:
- default
- shared
volumes:
- ./:/app:rw
working_dir: /app
command: '/bin/pnpm run dev'
environment:
- TERM=xterm-256color`
labels:
- traefik.enable=true
- traefik.http.routers.${APP_NAME}.rule=Host(`${APP_NAME}.${PROJECT_SERVICES_NAMESPACE}.docker`)
- traefik.http.routers.${APP_NAME}.entrypoints=websecure
- traefik.http.services.${APP_NAME}.loadbalancer.server.port=3000
- traefik.http.routers.${APP_NAME}.tls=true

networks:
shared:
external: true
name: ${SHARED_SERVICES_NETWORK}
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
"lint": "turbo run lint",
"format": "prettier --write \"**/*.{ts,tsx,md}\""
},
"dependencies": {},
"devDependencies": {
"eslint-config-custom": "workspace:*",
"prettier": "latest",
Expand Down
Loading

0 comments on commit 43195ba

Please sign in to comment.