Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve Multi-Instance Docker Support and DB Restoration Fix #47

Merged
Merged
Show file tree
Hide file tree
Changes from 6 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
4 changes: 4 additions & 0 deletions templates/Alloy.Mvc/.env
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# These variables are used for passing values into docker-compose.yml and the containers
SA_PASSWORD=Qwerty12345!
DB_NAME=Alloy.Mvc.1
DB_DIRECTORY=Alloy.Mvc.1
3 changes: 2 additions & 1 deletion templates/Alloy.Mvc/.template.config/template.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@
"exclude": [
"Directory.Build.props",
"docker-compose.yml",
"Docker/**/*"
"Docker/**/*",
".env"
]
}
]
Expand Down
15 changes: 13 additions & 2 deletions templates/Alloy.Mvc/Docker/create-db.sh
Original file line number Diff line number Diff line change
@@ -1,8 +1,19 @@
#!/bin/bash

echo "Creating database..."

let result=1

for i in {1..100}; do
/opt/mssql-tools/bin/sqlcmd -S localhost -U sa -P "$SA_PASSWORD" -Q "IF NOT EXISTS (SELECT * FROM sys.databases WHERE name = '${DB_NAME}') CREATE DATABASE ${DB_NAME} ON (NAME=${DB_NAME}_data, FILENAME='/var/opt/mssql/host_data/${DB_NAME}.mdf') LOG ON (NAME=${DB_NAME}_log, FILENAME='/var/opt/mssql/host_data/${DB_NAME}.ldf')"
if [ $? -eq 0 ]; then
if test -f /var/opt/mssql/host_data/${DB_DIRECTORY}/${DB_NAME}.mdf; then
echo "Restoring from .mdf/.ldf"
/opt/mssql-tools/bin/sqlcmd -S localhost -U sa -P "$SA_PASSWORD" -Q "IF NOT EXISTS (SELECT * FROM sys.databases WHERE name = '${DB_NAME}') CREATE DATABASE ${DB_NAME} ON (NAME=${DB_NAME}_data, FILENAME='/var/opt/mssql/host_data/${DB_DIRECTORY}/${DB_NAME}.mdf') LOG ON (NAME=${DB_NAME}_log, FILENAME='/var/opt/mssql/host_data/${DB_DIRECTORY}/${DB_NAME}.ldf') FOR ATTACH;"
let result=$?
else
/opt/mssql-tools/bin/sqlcmd -S localhost -U sa -P "$SA_PASSWORD" -Q "IF NOT EXISTS (SELECT * FROM sys.databases WHERE name = '${DB_NAME}') CREATE DATABASE ${DB_NAME} ON (NAME=${DB_NAME}_data, FILENAME='/var/opt/mssql/host_data/${DB_DIRECTORY}/${DB_NAME}.mdf') LOG ON (NAME=${DB_NAME}_log, FILENAME='/var/opt/mssql/host_data/${DB_DIRECTORY}/${DB_NAME}.ldf')"
let result=$?
fi
if [ $result -eq 0 ]; then
echo "Creating database completed"
break
else
Expand Down
7 changes: 7 additions & 0 deletions templates/Alloy.Mvc/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,20 @@ $ dotnet run
Prerequisities
- Docker
- Enable Docker support when applying the template
- Review the .env file and make changes where necessary to the Docker-related variables

```bash
$ docker-compose up
````

> Note that this Docker setup is just configured for local development. Follow this [guide to enable HTTPS](https://github.com/dotnet/dotnet-docker/blob/main/samples/run-aspnetcore-https-development.md).

#### Reclaiming Docker Image Space

1. Backup the App_Data/\${DB_NAME}.mdf and App_Data/\${DB_NAME}.ldf DB restoration files for safety
2. Run `docker compose down --rmi all` to remove containers, networks, and images associated with the specific project instance
3. In the future, run `docker compose up` anytime you want to recreate the images and containers

### Any OS with external database server

Prerequisities
Expand Down
9 changes: 5 additions & 4 deletions templates/Alloy.Mvc/docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,13 @@ services:
dockerfile: ./Docker/db.dockerfile
context: .
environment:
SA_PASSWORD: Qwerty12345!
DB_NAME: cms
SA_PASSWORD: ${SA_PASSWORD}
DB_NAME: ${DB_NAME}
DB_DIRECTORY: ${DB_DIRECTORY}
ports:
- 6000:1433
volumes:
- ./App_Data:/var/opt/mssql/host_data
- ./App_Data:/var/opt/mssql/host_data/${DB_DIRECTORY}
image: Alloy.Mvc.1/db
web:
depends_on:
Expand All @@ -22,7 +23,7 @@ services:
environment:
ASPNETCORE_URLS: http://*:80
ASPNETCORE_ENVIRONMENT: Development
CONNECTIONSTRINGS__EPISERVERDB: Server=db;Database=cms;User Id=sa;Password=Qwerty12345!;Encrypt=False;
CONNECTIONSTRINGS__EPISERVERDB: Server=db;Database=${DB_NAME};User Id=sa;Password=${SA_PASSWORD};Encrypt=False;
ports:
- 5000:80
volumes:
Expand Down
4 changes: 4 additions & 0 deletions templates/Cms.Empty/.env
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# These variables are used for passing values into docker-compose.yml and the containers
SA_PASSWORD=Qwerty12345!
DB_NAME=Cms.Empty.1
DB_DIRECTORY=Cms.Empty.1
3 changes: 2 additions & 1 deletion templates/Cms.Empty/.template.config/template.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@
"exclude": [
"Directory.Build.props",
"docker-compose.yml",
"Docker/**/*"
"Docker/**/*",
".env"
]
}
]
Expand Down
15 changes: 13 additions & 2 deletions templates/Cms.Empty/Docker/create-db.sh
Original file line number Diff line number Diff line change
@@ -1,8 +1,19 @@
#!/bin/bash

echo "Creating database..."

let result=1

for i in {1..100}; do
/opt/mssql-tools/bin/sqlcmd -S localhost -U sa -P "$SA_PASSWORD" -Q "IF NOT EXISTS (SELECT * FROM sys.databases WHERE name = '${DB_NAME}') CREATE DATABASE ${DB_NAME} ON (NAME=${DB_NAME}_data, FILENAME='/var/opt/mssql/host_data/${DB_NAME}.mdf') LOG ON (NAME=${DB_NAME}_log, FILENAME='/var/opt/mssql/host_data/${DB_NAME}.ldf')"
if [ $? -eq 0 ]; then
if test -f /var/opt/mssql/host_data/${DB_DIRECTORY}/${DB_NAME}.mdf; then
echo "Restoring from .mdf/.ldf"
/opt/mssql-tools/bin/sqlcmd -S localhost -U sa -P "$SA_PASSWORD" -Q "IF NOT EXISTS (SELECT * FROM sys.databases WHERE name = '${DB_NAME}') CREATE DATABASE ${DB_NAME} ON (NAME=${DB_NAME}_data, FILENAME='/var/opt/mssql/host_data/${DB_DIRECTORY}/${DB_NAME}.mdf') LOG ON (NAME=${DB_NAME}_log, FILENAME='/var/opt/mssql/host_data/${DB_DIRECTORY}/${DB_NAME}.ldf') FOR ATTACH;"
let result=$?
else
/opt/mssql-tools/bin/sqlcmd -S localhost -U sa -P "$SA_PASSWORD" -Q "IF NOT EXISTS (SELECT * FROM sys.databases WHERE name = '${DB_NAME}') CREATE DATABASE ${DB_NAME} ON (NAME=${DB_NAME}_data, FILENAME='/var/opt/mssql/host_data/${DB_DIRECTORY}/${DB_NAME}.mdf') LOG ON (NAME=${DB_NAME}_log, FILENAME='/var/opt/mssql/host_data/${DB_DIRECTORY}/${DB_NAME}.ldf')"
let result=$?
fi
if [ $result -eq 0 ]; then
echo "Creating database completed"
break
else
Expand Down
7 changes: 7 additions & 0 deletions templates/Cms.Empty/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,20 @@ $ dotnet run
Prerequisities
- Docker
- Enable Docker support when applying the template
- Review the .env file and make changes where necessary to the Docker-related variables

```bash
$ docker-compose up
````

> Note that this Docker setup is just configured for local development. Follow this [guide to enable HTTPS](https://github.com/dotnet/dotnet-docker/blob/main/samples/run-aspnetcore-https-development.md).

#### Reclaiming Docker Image Space

1. Backup the App_Data/\${DB_NAME}.mdf and App_Data/\${DB_NAME}.ldf DB restoration files for safety
2. Run `docker compose down --rmi all` to remove containers, networks, and images associated with the specific project instance
3. In the future, run `docker compose up` anytime you want to recreate the images and containers

### Any OS with external database server

Prerequisities
Expand Down
9 changes: 5 additions & 4 deletions templates/Cms.Empty/docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,13 @@ services:
dockerfile: ./Docker/db.dockerfile
context: .
environment:
SA_PASSWORD: Qwerty12345!
DB_NAME: cms
SA_PASSWORD: ${SA_PASSWORD}
DB_NAME: ${DB_NAME}
DB_DIRECTORY: ${DB_DIRECTORY}
ports:
- 6000:1433
volumes:
- ./App_Data:/var/opt/mssql/host_data
- ./App_Data:/var/opt/mssql/host_data/${DB_DIRECTORY}
image: Cms.Empty.1/db
web:
depends_on:
Expand All @@ -22,7 +23,7 @@ services:
environment:
ASPNETCORE_URLS: http://*:80
ASPNETCORE_ENVIRONMENT: Development
CONNECTIONSTRINGS__EPISERVERDB: Server=db;Database=cms;User Id=sa;Password=Qwerty12345!;Encrypt=False;
CONNECTIONSTRINGS__EPISERVERDB: Server=db;Database=${DB_NAME};User Id=sa;Password=${SA_PASSWORD};Encrypt=False;
ports:
- 5000:80
volumes:
Expand Down
5 changes: 5 additions & 0 deletions templates/Commerce.Empty/.env
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# These variables are used for passing values into docker-compose.yml and the containers
SA_PASSWORD=Qwerty12345!
DB_NAME=Commerce.Empty.1_cms
DB_NAME_COMMERCE=Commerce.Empty.1_commerce
DB_DIRECTORY=Commerce.Empty.1
3 changes: 2 additions & 1 deletion templates/Commerce.Empty/.template.config/template.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@
"exclude": [
"Directory.Build.props",
"docker-compose.yml",
"Docker/**/*"
"Docker/**/*",
".env"
]
}
]
Expand Down
32 changes: 27 additions & 5 deletions templates/Commerce.Empty/Docker/create-db.sh
Original file line number Diff line number Diff line change
@@ -1,13 +1,35 @@
#!/bin/bash

echo "Creating databases..."

let cmsresult=1
let commerceresult=1

for i in {1..100}; do
/opt/mssql-tools/bin/sqlcmd -S localhost -U sa -P "$SA_PASSWORD" -Q "IF NOT EXISTS (SELECT * FROM sys.databases WHERE name = '${DB_NAME}') CREATE DATABASE ${DB_NAME} ON (NAME=${DB_NAME}_data, FILENAME='/var/opt/mssql/host_data/${DB_NAME}.mdf') LOG ON (NAME=${DB_NAME}_log, FILENAME='/var/opt/mssql/host_data/${DB_NAME}.ldf')"
/opt/mssql-tools/bin/sqlcmd -S localhost -U sa -P "$SA_PASSWORD" -Q "IF NOT EXISTS (SELECT * FROM sys.databases WHERE name = '${DB_NAME_COMMERCE}') CREATE DATABASE ${DB_NAME_COMMERCE} ON (NAME=${DB_NAME_COMMERCE}_data, FILENAME='/var/opt/mssql/host_data/${DB_NAME_COMMERCE}.mdf') LOG ON (NAME=${DB_NAME_COMMERCE}_log, FILENAME='/var/opt/mssql/host_data/${DB_NAME_COMMERCE}.ldf')"
if [ $? -eq 0 ]; then
echo "Creating database completed"
if test -f /var/opt/mssql/host_data/${DB_DIRECTORY}/${DB_NAME}.mdf; then
echo "Restoring CMS DB from .mdf/.ldf"

/opt/mssql-tools/bin/sqlcmd -S localhost -U sa -P "$SA_PASSWORD" -Q "IF NOT EXISTS (SELECT * FROM sys.databases WHERE name = '${DB_NAME}') CREATE DATABASE ${DB_NAME} ON (NAME=${DB_NAME}_data, FILENAME='/var/opt/mssql/host_data/${DB_DIRECTORY}/${DB_NAME}.mdf') LOG ON (NAME=${DB_NAME}_log, FILENAME='/var/opt/mssql/host_data/${DB_DIRECTORY}/${DB_NAME}.ldf') FOR ATTACH;"
let cmsresult=$?
else
/opt/mssql-tools/bin/sqlcmd -S localhost -U sa -P "$SA_PASSWORD" -Q "IF NOT EXISTS (SELECT * FROM sys.databases WHERE name = '${DB_NAME}') CREATE DATABASE ${DB_NAME} ON (NAME=${DB_NAME}_data, FILENAME='/var/opt/mssql/host_data/${DB_DIRECTORY}/${DB_NAME}.mdf') LOG ON (NAME=${DB_NAME}_log, FILENAME='/var/opt/mssql/host_data/${DB_DIRECTORY}/${DB_NAME}.ldf')"
let cmsresult=$?
fi

if test -f /var/opt/mssql/host_data/${DB_DIRECTORY}/${DB_NAME_COMMERCE}.mdf; then
echo "Restoring Commerce DB from .mdf/.ldf"
/opt/mssql-tools/bin/sqlcmd -S localhost -U sa -P "$SA_PASSWORD" -Q "IF NOT EXISTS (SELECT * FROM sys.databases WHERE name = '${DB_NAME_COMMERCE}') CREATE DATABASE ${DB_NAME_COMMERCE} ON (NAME=${DB_NAME_COMMERCE}_data, FILENAME='/var/opt/mssql/host_data/${DB_DIRECTORY}/${DB_NAME_COMMERCE}.mdf') LOG ON (NAME=${DB_NAME_COMMERCE}_log, FILENAME='/var/opt/mssql/host_data/${DB_DIRECTORY}/${DB_NAME_COMMERCE}.ldf') FOR ATTACH;"
let commerceresult=$?
else
/opt/mssql-tools/bin/sqlcmd -S localhost -U sa -P "$SA_PASSWORD" -Q "IF NOT EXISTS (SELECT * FROM sys.databases WHERE name = '${DB_NAME_COMMERCE}') CREATE DATABASE ${DB_NAME_COMMERCE} ON (NAME=${DB_NAME_COMMERCE}_data, FILENAME='/var/opt/mssql/host_data/${DB_DIRECTORY}/${DB_NAME_COMMERCE}.mdf') LOG ON (NAME=${DB_NAME_COMMERCE}_log, FILENAME='/var/opt/mssql/host_data/${DB_DIRECTORY}/${DB_NAME_COMMERCE}.ldf')"
let commerceresult=$?
fi

if [ $cmsresult -eq 0 ] && [ $commerceresult -eq 0 ]; then
echo "Creating databases completed"
break
else
echo "Creating database. Not ready yet..."
echo "Creating databases. Not ready yet..."
sleep 1
fi
done
7 changes: 7 additions & 0 deletions templates/Commerce.Empty/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,20 @@ $ dotnet run
Prerequisities
- Docker
- Enable Docker support when applying the template
- Review the .env file and make changes where necessary to the Docker-related variables

```bash
$ docker-compose up
````

> Note that this Docker setup is just configured for local development. Follow this [guide to enable HTTPS](https://github.com/dotnet/dotnet-docker/blob/main/samples/run-aspnetcore-https-development.md).

#### Reclaiming Docker Image Space

1. Backup the App_Data/\${DB_NAME}.mdf and App_Data/\${DB_NAME}.ldf DB restoration files for safety
2. Run `docker compose down --rmi all` to remove containers, networks, and images associated with the specific project instance
3. In the future, run `docker compose up` anytime you want to recreate the images and containers

### Any OS with external database server

Prerequisities
Expand Down
13 changes: 7 additions & 6 deletions templates/Commerce.Empty/docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,14 @@ services:
dockerfile: ./Docker/db.dockerfile
context: .
environment:
SA_PASSWORD: Qwerty12345!
DB_NAME: cms
DB_NAME_COMMERCE: commerce
SA_PASSWORD: ${SA_PASSWORD}
DB_NAME: ${DB_NAME}
DB_NAME_COMMERCE: ${DB_NAME_COMMERCE}
DB_DIRECTORY: ${DB_DIRECTORY}
ports:
- 6000:1433
volumes:
- ./App_Data:/var/opt/mssql/host_data
- ./App_Data:/var/opt/mssql/host_data/${DB_DIRECTORY}
image: Commerce.Empty.1/db
web:
depends_on:
Expand All @@ -23,8 +24,8 @@ services:
environment:
ASPNETCORE_URLS: http://*:80
ASPNETCORE_ENVIRONMENT: Development
CONNECTIONSTRINGS__EPISERVERDB: Server=db;Database=cms;User Id=sa;Password=Qwerty12345!;Encrypt=False;
CONNECTIONSTRINGS__ECFSQLCONNECTION: Server=db;Database=commerce;User Id=sa;Password=Qwerty12345!;Encrypt=False;
CONNECTIONSTRINGS__EPISERVERDB: Server=db;Database=${DB_NAME};User Id=sa;Password=${SA_PASSWORD};Encrypt=False;
CONNECTIONSTRINGS__ECFSQLCONNECTION: Server=db;Database=${DB_NAME_COMMERCE};User Id=sa;Password=${SA_PASSWORD};Encrypt=False;
ports:
- 5000:80
volumes:
Expand Down