Deployment
Last updated on
Deploy Blogic Licenses project
š Anything to know to deploy Blogic Licenses project with Docker š š³
API
Repository
Clone the repository and checkout to develop
branch
git clone https://github.com/BLOGICSYSTEMS/CheckLicenseApi
Prepare
In the CheckLicense.API
folder, add the following files:
Dockerfile
FROM mcr.microsoft.com/dotnet/aspnet:3.1 AS base
RUN apt-get update && apt-get install -y libgdiplus
WORKDIR /app
EXPOSE 80
EXPOSE 443
FROM mcr.microsoft.com/dotnet/sdk:3.1 AS build
WORKDIR /src
COPY ["CheckLicense.API.csproj", "./"]
RUN dotnet restore "./CheckLicense.API.csproj"
COPY . .
COPY appsettings.staging.json appsettings.json
RUN dotnet build "CheckLicense.API.csproj" -c Release -o /app
FROM build AS publish
RUN dotnet publish "CheckLicense.API.csproj" -c Release -o /app
FROM base AS final
WORKDIR /app
COPY --from=publish /app .
ENTRYPOINT ["dotnet", "CheckLicense.API.dll"]
appsettings.staging.json
{
"ConnectionStrings": {
"DBConnectionString": "Server=license-database;Database=BLogicLicenses;User=SA;Password=S3cur3P@ssW0rd!;MultipleActiveResultSets=True;"
},
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft": "Warning",
"Microsoft.Hosting.Lifetime": "Information"
}
},
"AllowedHosts": "*",
"AppSettings": {
"Secret": "pq9l2kI7&mnsw@j1bdyw*-akdi^JWUY",
"EPPlus": {
"ExcelPackage": {
"LicenseContext": "NonCommercial"
}
}
}
}
CheckLicense.API.csproj
add missing package cause docker build fail
<ItemGroup>
...
<PackageReference Include="Microsoft.Owin.Security" Version="3.1.0" />
</ItemGroup>
<ItemGroup>
<RuntimeHostConfigurationOption Include="System.Drawing.EnableUnixSupport" Value="true" />
</ItemGroup>
Program.cs
replace the HostBuilder with the following url
public static IHostBuilder CreateHostBuilder(string[] args) => Host.CreateDefaultBuilder(args).ConfigureWebHostDefaults(webBuilder =>
{
webBuilder.UseStartup<Startup>();
webBuilder.UseUrls("http://*:80"); // <-- replace this line
});
Build
Run the following command to build docker image
cd CheckLicense.API
docker build -t ghcr.io/blogic-datht/license-api .
Publish
Create publish.sh
script and paste the following code, then run it to publish docker image to Github registry
publish.sh
script
#!/bin/bash
# Build docker image
REGISTRY='ghcr.io'
OWNER='blogic-datht'
IMAGE_NAME='license-api'
PACKAGE_VERSION='1.0.0'
echo 'š³ Build docker image with tag' "$PACKAGE_VERSION"
# ghcr.io/blogic-datht/license-api
docker build -t "$REGISTRY/$OWNER/$IMAGE_NAME:$PACKAGE_VERSION" -t "$REGISTRY/$OWNER/$IMAGE_NAME:latest" .
echo 'š Build done!'
echo ''
echo 'š Publish image to Github registry'
USERNAME='blogic-datht'
CR_PAT='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'
# login to github registry
echo $CR_PAT | docker login ghcr.io -u $USERNAME --password-stdin
# push image to github registry
docker push $REGISTRY/$OWNER/$IMAGE_NAME:$PACKAGE_VERSION
docker push $REGISTRY/$OWNER/$IMAGE_NAME:latest
Frontend
Repository
Clone the repository and checkout to v2
branch
git clone https://github.com/BLOGICSYSTEMS/CheckLicenseClient
Build
Run the following command to build docker image
docker build -t ghcr.io/blogic-datht/license-client .
Publish
Run the following command to publish docker image to Github registry
npm run publish
Deploy
- Create
docker-compose.yml
version: "3.4"
services:
mssql:
image: "mcr.microsoft.com/mssql/server:2022-latest"
container_name: "license-database"
user: "root"
ports:
- "1436:1433"
environment:
SA_PASSWORD: "S3cur3P@ssW0rd!"
ACCEPT_EULA: "Y"
networks:
- "license_network"
volumes:
- "license_sqldata:/var/opt/mssql"
restart: "unless-stopped"
api:
image: "ghcr.io/blogic-datht/license-api"
container_name: "license-api"
ports:
- "5001:443"
- "5000:80"
volumes:
- "license_api_log_data:/app/Logs"
- "license_api_resource_data:/app/UploadedFiles"
networks:
- "license_network"
restart: "unless-stopped"
client-v1:
image: "ghcr.io/blogic-datht/license-dashboard-v1"
container_name: "license-dashboard-v1"
ports:
- "3300:80"
restart: "unless-stopped"
networks:
- "license_network"
client-v2:
image: "ghcr.io/blogic-datht/license-dashboard"
container_name: "license-dashboard"
ports:
- "3200:80"
restart: "unless-stopped"
networks:
- "license_network"
networks:
license_network:
volumes:
license_sqldata:
license_api_log_data:
license_api_resource_data:
- Start the stack with docker-compose
docker-compose up -d
Restore database
- Download the backed up zip file
- Create backup folder in docker container
docker exec -it license-database mkdir /var/opt/mssql/backup
- Name the backup file as
BlogicLicenses.bak
then copy it to docker container
docker cp BlogicLicenses.bak database:/var/opt/mssql/backup
- List out logical file names and paths inside the backup
with docker command
docker exec -it database /opt/mssql-tools/bin/sqlcmd -S localhost \
-U SA -P 'S3cur3P@ssW0rd!' \
-Q 'RESTORE FILELISTONLY FROM DISK = "/var/opt/mssql/backup/BlogicLicenses.bak"' \
| tr -s ' ' | cut -d ' ' -f 1-2
or with sql query
RESTORE FILELISTONLY FROM DISK = '/var/opt/mssql/backup/BlogicLicenses.bak'
- Restore database. Specify new paths for each of the files in the previous step
with docker command
docker exec -it database /opt/mssql-tools/bin/sqlcmd \
-S localhost -U SA -P 'S3cur3P@ssW0rd!' \
-Q 'RESTORE DATABASE BlogicLicenses FROM DISK = "/var/opt/mssql/backup/BlogicLicenses.bak" WITH MOVE "BlogicLicenses" TO "/var/opt/mssql/data/BlogicLicenses.mdf", MOVE "BlogicLicenses_log" TO "/var/opt/mssql/data/BlogicLicenses_log.ldf"'
or with sql query
RESTORE DATABASE BlogicLicenses FROM DISK = '/var/opt/mssql/backup/BlogicLicenses.bak' WITH
MOVE 'BlogicLicenses' TO '/var/opt/mssql/data/BlogicLicenses.mdf',
MOVE 'BlogicLicenses_log' TO '/var/opt/mssql/data/BlogicLicenses_log.ldf'
Drop database before restore new one:
docker exec -it database /opt/mssql-tools/bin/sqlcmd \
-S localhost -U SA -P 'S3cur3P@ssW0rd!' \
-Q 'use master; ALTER DATABASE BlogicLicenses SET SINGLE_USER WITH ROLLBACK IMMEDIATE; use master; DROP DATABASE BlogicLicenses;'
How to backup database?
Run backup sql database:
docker exec -it database /opt/mssql-tools/bin/sqlcmd \
-S localhost -U SA -P 'S3cur3P@ssW0rd!' \
-Q 'BACKUP DATABASE BlogicLicenses TO DISK = "/var/opt/mssql/backup/BlogicLicenses.bak"'
Copy backup file to host:
docker cp database:/var/opt/mssql/backup/BlogicLicenses.bak ./