Skip to content

Commit

Permalink
Improve Multi-Instance Docker Support and DB Restoration Fix (#47)
Browse files Browse the repository at this point in the history
This PR will accomplish two tasks:

1. The ability for multiple Optimizely Docker projects to be created
without storage conflicts in /var/opt/mssql/host_data
- This commit 7a2b9b5 will add a .env
file to each Docker project that specifies a DB_DIRECTORY. This
environment variable will specify a new directory on
/var/opt/mssql/host_data/ within the container so that the DB may
persist when adding other docker projects on the base image of
mssql/server:2019-latest.
    
3. The ability to restore pre-existing databases from .mdf & .ldf files
on `docker compose up`
- If the container and its network are deleted, then the databases will
be dropped. So when `docker compose up` runs again to create the
containers, then the plain `CREATE DATABASE` command won't work since
there is already a .mdf & .ldf file copied over. This commit
3ca4fc2 will check if the .mdf file
exists. If so, it will add a `FOR ATTACH` command to the end of `CREATE
DATABASE` to restore the DB from .mdf and .ldf.
  • Loading branch information
gabepetersen authored Nov 14, 2023
1 parent 16fd039 commit 0cd218e
Show file tree
Hide file tree
Showing 16 changed files with 137 additions and 53 deletions.
54 changes: 27 additions & 27 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -31,31 +31,31 @@ jobs:
path: artifacts/*.nupkg
retention-days: 2

publish:
name: Publish
needs: build_test_pack
runs-on: windows-latest
env:
nugetSource: https://nuget.pkg.github.com/${{ github.repository_owner }}/index.json
DOTNET_NOLOGO: 1
steps:
- name: Download artifacts
uses: actions/download-artifact@v2
with:
name: packages
- name: Publish to Github Packages
run: dotnet nuget push *.nupkg --source $env:nugetSource --skip-duplicate --api-key ${{ secrets.GITHUB_TOKEN }}
# publish:
# name: Publish
# needs: build_test_pack
# runs-on: windows-latest
# env:
# nugetSource: https://nuget.pkg.github.com/${{ github.repository_owner }}/index.json
# DOTNET_NOLOGO: 1
# steps:
# - name: Download artifacts
# uses: actions/download-artifact@v2
# with:
# name: packages
# - name: Publish to Github Packages
# run: dotnet nuget push *.nupkg --source $env:nugetSource --skip-duplicate --api-key ${{ secrets.GITHUB_TOKEN }}

cleanup:
name: Cleanup
needs: build_test_pack
runs-on: ubuntu-latest
steps:
- name: Cleanup old Packages
uses: actions/delete-package-versions@v3
with:
package-name: 'EPiServer.Templates'
min-versions-to-keep: 10
# 1-in-1-out
num-old-versions-to-delete: 1
delete-only-pre-release-versions: "true"
# cleanup:
# name: Cleanup
# needs: build_test_pack
# runs-on: ubuntu-latest
# steps:
# - name: Cleanup old Packages
# uses: actions/delete-package-versions@v3
# with:
# package-name: 'EPiServer.Templates'
# min-versions-to-keep: 10
# # 1-in-1-out
# num-old-versions-to-delete: 1
# delete-only-pre-release-versions: "true"
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

0 comments on commit 0cd218e

Please sign in to comment.