Skip to content

Commit

Permalink
Enable local development for *nix environments using docker
Browse files Browse the repository at this point in the history
  • Loading branch information
jasonho215 committed Feb 7, 2021
1 parent f8ccae8 commit cadb613
Show file tree
Hide file tree
Showing 9 changed files with 172 additions and 1 deletion.
17 changes: 17 additions & 0 deletions Dockerfile.dev
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
ARG BASE_IMAGE_SUFFIX

FROM mcr.microsoft.com/dotnet/core/sdk:3.1-buster${BASE_IMAGE_SUFFIX} AS builder
ENV DOTNET_CLI_TELEMETRY_OPTOUT=1
ENV NO_DOCKER 1
ENV IN_DOCKER 1

RUN apt update && apt install -y \
build-essential \
&& rm -rf /var/lib/apt/lists/*
# For compile Less style files in pre-build stage
RUN apt-get update \
&& curl -sL https://deb.nodesource.com/setup_14.x | bash \
&& apt-get install -y nodejs

WORKDIR /src
COPY ./ /src/
38 changes: 38 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
# Supported environment: Unix-like / Powershell
SELF_DIR := $(dir $(lastword $(MAKEFILE_LIST)))

DOCKER_COMPOSE_CMD := docker-compose -f $(SELF_DIR)docker-compose.dev.yml

ifeq (,${NO_DOCKER})
DOCKER_RUN := $(DOCKER_COMPOSE_CMD) run --rm web
else
DOCKER_RUN :=
endif

.PHONY: setup
setup:
@cp Web/appsettings.Development.json.template Web/appsettings.Development.json
@echo "Please update Web/appsettings.Development.json for your local development"
@echo "Your .env now is"
@echo "================"
@cat Web/appsettings.Development.json
@echo "================"
@echo "Please choose either docker-based or localhost setup before starting server"
@echo "export NO_DOCKER=1 and replace Database part in ConnectionStrings.DataContext to 'localhost,1433' in Web/appsettings.Development.json if you use local development"
@$(MAKE) db-setup


.PHONY: db-setup
db-setup:
$(DOCKER_COMPOSE_CMD) up -d db
@echo "IMPORTANT: You need to set up the database by using Data-application Wizard. Use the Database project by Azure Data Studio on Mac or Visual Studio on Windows to finish the setup."
@echo "Check https://docs.microsoft.com/en-us/sql/azure-data-studio/extensions/sql-database-project-extension?view=sql-server-ver15"
@echo "TODO: Automate database setup"

.PHONY: dev
dev:
if [ "${NO_DOCKER}" = "" ]; then \
$(DOCKER_COMPOSE_CMD) up; \
else \
make -f Makefile.nodocker.mk dev; \
fi
13 changes: 13 additions & 0 deletions Makefile.nodocker.mk
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
DOCKER_COMPOSE_CMD := docker-compose -f docker-compose.dev.yml

.PHONY: dev
dev:
make -C $(MODULE) dev

.PHONY: docker-start-db
docker-start-db:
$(DOCKER_COMPOSE_CMD) up -d db

.PHONY: docker-stop-db
docker-stop-db:
$(DOCKER_COMPOSE_CMD) stop db
28 changes: 27 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,30 @@ Front Line Live code and target operating model
|APP_RECAPTCHA_SITEKEY*|-|
|APP_DATACONTEXT*|-|
|WEB_USERSECRETSID*|-|
|SQL_ADMIN_PASSWORD*|-|
|SQL_ADMIN_PASSWORD*|-|

## Local development

### Technologies
- Docker
- Docker Compose
- .NET core 3.1.401
- SQL Server
- Azure Data Studio (Non-Windows environment only)

### Setup
```sh
# Prepare local development settings
$ make setup

# Start servers in Docker
$ make dev

# For those who want to start development on website without Docker
#
# You can start depending services in docker
$ make -f Makefile.nodocker.mk docker-start-db
# And then run the dotnet process in local
$ NO_DOCKER=1 MODULE=Web make dev
```
After running above commands, visit http://localhost:3000 on browser and you should be able to see the web page.
3 changes: 3 additions & 0 deletions Web/.gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,5 @@
# Ignore output of pre-build target
wwwroot/css/**/*.css

# Development settings
appsettings.Development.json
5 changes: 5 additions & 0 deletions Web/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
CONTAINER := web
PROJECT_NAME = Web
PORT = 3000

include ../shared.mk
10 changes: 10 additions & 0 deletions Web/appsettings.Development.json.template
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"Logging": {
"LogLevel": {
"Default": "Debug"
}
},
"ConnectionStrings": {
"DataContext": "Server=db,1433; User Id=SA; Password=veryStrongSecret!; Database=Database; Trusted_Connection=False; MultipleActiveResultSets=true"
}
}
31 changes: 31 additions & 0 deletions docker-compose.dev.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
version: '3.4'

volumes:
db_data: {}

services:
web:
image: ${DOCKER_REGISTRY-}web
build:
context: .
dockerfile: ./Dockerfile.dev
environment:
- ASPNETCORE_ENVIRONMENT=Development
- NO_DOCKER=1
ports:
- "3000:3000"
command: make dev MODULE=Web PORT=3000
volumes:
- ./Web:/src/Web


db:
image: mcr.microsoft.com/azure-sql-edge
user: root
ports:
- 1433:1433
environment:
ACCEPT_EULA: 'true'
SA_PASSWORD: veryStrongSecret!
volumes:
- db_data:/var/opt/mssql:delegated
28 changes: 28 additions & 0 deletions shared.mk
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
SELF_DIR := $(dir $(lastword $(MAKEFILE_LIST)))

CONTAINER ?=

PROJECT_NAME ?=
PORT ?=

DOCKER_COMPOSE_CMD := docker-compose -f $(SELF_DIR)docker-compose.dev.yml

ifeq (,${NO_DOCKER})
DOCKER_RUN := $(DOCKER_COMPOSE_CMD) run -w /src/$(PROJECT_NAME) --rm ${CONTAINER}
else
DOCKER_RUN :=
endif

.PHONY: vendor
vendor:
@echo "Installing dependency..."
$(DOCKER_RUN) dotnet restore
$(DOCKER_RUN) dotnet list $(PROJECT_NAME).csproj package

.PHONY: dev
dev: vendor
$(DOCKER_RUN) dotnet watch run --urls "http://*:$(PORT)"

.PHONY: dist
dist: vendor
$(DOCKER_RUN) dotnet publish -c Release -o dist

0 comments on commit cadb613

Please sign in to comment.