ornwipa / book_recommender

Final project for ComIT's FULL STACK .NET course
GNU General Public License v3.0
1 stars 1 forks source link

Public Trial Version #34

Closed ornwipa closed 3 years ago

ornwipa commented 3 years ago
  1. Enable simultaneous use for several guests
  2. Deploy as containerized image with Docker
  3. Deploy application on Google Cloud platform
ornwipa commented 3 years ago

Several guest can now use the application at once. Guest user data are not saved. Next: to create "contact me" page for users to get notifications and "about" page to explain the next steps (authentication & storing rating data, more books and ratings included, public reviews included).

ornwipa commented 3 years ago

Success with creating Dockerfile and building with docker build -t recommender . Fail when running docker run --name recommender_sample --rm -it -p 5000:80 recommender Error message (in short):

An error occurred using the connection to database 'main' on server '../data_source/recommender.db'.
Microsoft.Data.Sqlite.SqliteException (0x80004005): SQLite Error 14: 'unable to open database file'.
ornwipa commented 3 years ago

New migrations to have database within the same folder as the program, but ...

fail: Microsoft.EntityFrameworkCore.Database.Command[20102]
      Failed executing DbCommand (10ms) [Parameters=[], CommandType='Text', CommandTimeout='30']
      SELECT EXISTS (
          SELECT 1
          FROM "Books" AS "b")
fail: Microsoft.EntityFrameworkCore.Query[10100]
      An exception occurred while iterating over the results of a query for context type 'recommender.Data.ApplicationDbContext'.
      Microsoft.Data.Sqlite.SqliteException (0x80004005): SQLite Error 1: 'no such table: Books'.
fail: Microsoft.EntityFrameworkCore.Database.Command[20102]
      Failed executing DbCommand (0ms) [Parameters=[], CommandType='Text', CommandTimeout='30']
      SELECT "r"."id", "r"."book_id", "r"."rating_", "r"."user_id"
      FROM "Ratings" AS "r"
      WHERE "r"."rating_" = 0
fail: Microsoft.EntityFrameworkCore.Query[10100]
      An exception occurred while iterating over the results of a query for context type 'recommender.Data.ApplicationDbContext'.
      Microsoft.Data.Sqlite.SqliteException (0x80004005): SQLite Error 1: 'no such table: Ratings'.
ornwipa commented 3 years ago

Above problem is okay after commenting the section:

            /*
            using (var scope = host.Services.CreateScope())
            {
                var services = scope.ServiceProvider;
                try
                {
                    SeedData.Initialize(services);
                }
                catch (Exception ex)
                {
                    var logger = services.GetRequiredService<ILogger<Program>>();
                    logger.LogError(ex, "An error occurred seeding the database.");
                }
                CleanData.RemoveZeroRating(services);
            }
            */

... which connects to database at SeedData.Initialize(services) and CleanData.RemoveZeroRating(services) to reset database each time the program runs

Success in creating Docker container and running it but ...

      Now listening on: http://[::]:80
      Content root path: /app

Instead of ...

      Now listening on: http://localhost:5000
      Content root path: /home/ornwipa/Desktop/ComIT/project/recommender
ornwipa commented 3 years ago

When running docker-compose up, there is error:

warn: Microsoft.AspNetCore.DataProtection.Repositories.FileSystemXmlRepository[60]
      Storing keys in a directory '/root/.aspnet/DataProtection-Keys' that may not be persisted outside of the container. Protected data will be unavailable when container is destroyed.
warn: Microsoft.AspNetCore.DataProtection.KeyManagement.XmlKeyManager[35]
      No XML encryptor configured. Key {1e4d00ba-f355-44a6-a301-0ecfe946e031} may be persisted to storage in unencrypted form.
ornwipa commented 3 years ago

When running docker run -d -p 5000:80 recommender to create an image then checking 'docker ps', Notice that PORT 0.0.0.0:5000->80/tcp means exposing port 80 to the PC's port 5000. The application and database can now run on `http:\localhost:5000.

Reference https://medium.com/@gabrielalejandro7/how-to-make-a-simple-web-fast-using-asp-net-core-2-0-and-docker-e8e8e0687843

ornwipa commented 3 years ago

Connecting to hub.docker.com

$ docker ps
CONTAINER ID   IMAGE         COMMAND                  CREATED        STATUS        PORTS                  NAMES
d8fbe7b0973d   recommender   "dotnet recommender.…"   44 hours ago   Up 44 hours   0.0.0.0:5000->80/tcp   serene_kilby

$ docker commit -m "initial" -a "ornwipa" d8fbe7b0973d ornwipa/book_recommender:latest
sha256:c3b802affb3dc212ea716109cab980eac629020c547bd2467045c0c8ecb1214b

$ docker push ornwipa/book_recommender
Using default tag: latest
The push refers to repository [docker.io/ornwipa/book_recommender]
e431958149c6: Pushed 
43b92a261d05: Pushed 
9de294c261c0: Pushed 
423470ace333: Pushed 
d3d8723bb140: Pushed 
bbd61b971886: Pushed 
dc4a66fc412f: Pushed 
b22af9287e60: Pushed 
f5600c6330da: Pushed 
latest: digest: sha256:5a3d8b84750288f0a3385e338842c8d878286ef7be58b79c0ea144997031b832 size: 2216

Reference https://medium.com/docker-captain/creating-the-first-docker-image-and-pushing-it-to-docker-hub-4e02bea48e81

ornwipa commented 3 years ago

Publishing on cloud.google.com

$ docker tag recommender gcr.io/nextbook-beta/initial

$ docker push gcr.io/nextbook-beta/initial
Using default tag: latest
The push refers to repository [gcr.io/nextbook-beta/initial]
43b92a261d05: Pushed 
9de294c261c0: Pushed 
423470ace333: Pushed 
d3d8723bb140: Pushed 
bbd61b971886: Pushed 
dc4a66fc412f: Pushed 
b22af9287e60: Pushed 
f5600c6330da: Layer already exists 
latest: digest: sha256:3c865008372f3779edcd6845c452c0ae05434cae208d43fcd664d96878f4d761 size: 2005

$ gcloud config set project nextbook-beta
Updated property [core/project].

$ gcloud builds submit --tag gcr.io/nextbook-beta/gcp-api
Creating temporary tarball archive of 273 file(s) totalling 67.4 MiB before compression.
Uploading tarball of [.] to [gs://nextbook-beta_cloudbuild/source/1607986298.281194-98abb5f2463942188bc18b5b5c8ead94.tgz]
Created [https://cloudbuild.googleapis.com/v1/projects/nextbook-beta/locations/global/builds/87b5aaaf-f47b-4c55-abb6-e68da26ea493].
Logs are available at [https://console.cloud.google.com/cloud-build/builds/87b5aaaf-f47b-4c55-abb6-e68da26ea493?project=658686198073].
...
latest: digest: sha256:b6b4a83a6e55878d06f7a3adaaff335d97915198b97f88c8e9cd9dd8c7a6c4c4 size: 2005
DONE
-----------------------------------------------------------------------------------------------------------------------------------------

ID                                    CREATE_TIME                DURATION  SOURCE                                                                                       IMAGES                                  STATUS
87b5aaaf-f47b-4c55-abb6-e68da26ea493  2020-12-14T22:57:17+00:00  1M25S     gs://nextbook-beta_cloudbuild/source/1607986501.871094-3d1e12f4cfec4560b177412a473cfdad.tgz  gcr.io/nextbook-beta/gcp-api (+1 more)  SUCCESS

Running the application ... fail!

Reference https://towardsdatascience.com/how-to-deploy-docker-containers-to-the-cloud-b4d89b2c6c31 and https://cloud.google.com/container-registry/docs/pushing-and-pulling

ornwipa commented 3 years ago

New method ...

$ gcloud app deploy
Services to deploy:
descriptor:      [/home/ornwipa/Desktop/ComIT/project/recommender/app.yaml]
source:          [/home/ornwipa/Desktop/ComIT/project/recommender]
target project:  [nextbook-beta]
target service:  [default]
target version:  [20201214t185532]
target url:      [https://nextbook-beta.uc.r.appspot.com]

Beginning deployment of service [default]...
Building and pushing image for service [default]
Started cloud build [a79de4ac-e8d6-4834-bca6-4fd7b74b935a].
To see logs in the Cloud Console: https://console.cloud.google.com/cloud-build/builds/a79de4ac-e8d6-4834-bca6-4fd7b74b935a?project=658686198073
------------------------------------------------------ REMOTE BUILD OUTPUT ------------------------------------------------------
starting build "a79de4ac-e8d6-4834-bca6-4fd7b74b935a"

FETCHSOURCE
Fetching storage object: gs://staging.nextbook-beta.appspot.com/us.gcr.io/nextbook-beta/appengine
...
latest: digest: sha256:51c02254ebf11a0e0b935d23ce7f4a39f05c3d43d1aac7e428e69fc2a3f35e79 size: 2005
DONE
---------------------------------------------------------------------------------------------------------------------------------

Updating service [default] (this may take several minutes)...done.                                                              
Setting traffic split for service [default]...done.                                                                             
Deployed service [default] to [https://nextbook-beta.uc.r.appspot.com]

You can stream logs from the command line by running:
  $ gcloud app logs tail -s default

To view your application in the web browser run:
  $ gcloud app browse

Got ... 5.2 Bad Gateway nginx Reference https://medium.com/net-core/deploy-an-asp-net-core-app-to-google-cloud-d5ff3ff99b2d and https://wideops.com/4-ways-you-can-deploy-an-asp-net-core-app-to-gcp/

ornwipa commented 3 years ago

Add app.yaml file, and run another method ...

$ gcloud builds submit --tag gcr.io/nextbook-beta/initial
Creating temporary tarball archive of 273 file(s) totalling 67.4 MiB before compression.
Uploading tarball of [.] to [gs://nextbook-beta_cloudbuild/source/1608062599.54747-017898f00f0c44aeb25fb049ecc93436.tgz]
Created [https://cloudbuild.googleapis.com/v1/projects/nextbook-beta/locations/global/builds/1b5d47f1-79ac-4c2c-958f-938d4e14c5ea].
Logs are available at [https://console.cloud.google.com/cloud-build/builds/1b5d47f1-79ac-4c2c-958f-938d4e14c5ea?project=658686198073].
--------------------------------------------------------------------- REMOTE BUILD OUTPUT ---------------------------------------------------------------------
starting build "1b5d47f1-79ac-4c2c-958f-938d4e14c5ea"

FETCHSOURCE
Fetching storage object: gs://nextbook-beta_cloudbuild/source/1608062599.54747-017898f00f0c44aeb25fb049ecc93436.tgz#1608062728618893
Copying gs://nextbook-beta_cloudbuild/source/1608062599.54747-017898f00f0c44aeb25fb049ecc93436.tgz#1608062728618893...
- [1 files][ 27.2 MiB/ 27.2 MiB]                                                
Operation completed over 1 objects/27.2 MiB.                                     
BUILD
Already have image (with digest): gcr.io/cloud-builders/docker
Sending build context to Docker daemon  70.96MB
...
latest: digest: sha256:1020b9e36ded41da25d0caf71449ec110801d83e2fe3d429a362ccca04b02635 size: 1792
DONE
---------------------------------------------------------------------------------------------------------------------------------------------------------------

ID                                    CREATE_TIME                DURATION  SOURCE                                                                                      IMAGES                                  STATUS
1b5d47f1-79ac-4c2c-958f-938d4e14c5ea  2020-12-15T20:05:29+00:00  1M26S     gs://nextbook-beta_cloudbuild/source/1608062599.54747-017898f00f0c44aeb25fb049ecc93436.tgz  gcr.io/nextbook-beta/initial (+1 more)  SUCCESS

$ gcloud run deploy --image gcr.io/nextbook-beta/initial --platform managed
Service name (initial):  nextbook-beta
Please specify a region:
...
 [18] us-central1
...
 [22] cancel
Please enter your numeric choice:  18

To make this the default region, run `gcloud config set run/region us-central1`.

Allow unauthenticated invocations to [nextbook-beta] (y/N)?  y

Deploying container to Cloud Run service [nextbook-beta] in project [nextbook-beta] region [us-central1]
⠛ Deploying new service... Cloud Run error: Container failed to start. Failed to start and then listen on the port defined by the PORT environment variable. L
ogs for this revision might contain more information.                                                                                                         
  . Routing traffic...                                                                                                                                        
Logs URL:ng IAM Policy...                                                                                                                                     
https://console.cloud.google.com/logs/viewer?project=nextbook-beta&resource=cloud_run_revisio
n/service_name/nextbook-beta/revision_name/nextbook-beta-00001-raz&advancedFilter=resource.type%3D%22cloud_run_revision%22%0Aresource.labels.service_name%3D%2
X Deploying new service... Cloud Run error: Container failed to start. Failed to start and then listen on the port defined by the PORT environment variable. L
ogs for this revision might contain more information.                                                                                                         
  s for this revision might contain more information.                                                                                                         
Logs URL:
https://console.cloud.google.com/logs/viewer?project=nextbook-beta&resource=cloud_run_revisio
n/service_name/nextbook-beta/revision_name/nextbook-beta-00001-raz&advancedFilter=resource.type%3D%22cloud_run_revision%22%0Aresource.labels.service_name%3D%2
2nextbook-beta%22%0Aresource.labels.revision_name%3D%22nextbook-beta-00001-raz%22                                                                             
  %22nextbook-beta%22%0Aresource.labels.revision_name%3D%22nextbook-beta-00001-raz%22                                                                         
  . Routing traffic...                                                                                                                                        
  ✓ Setting IAM Policy...                                                                                                                                     

Deployment failed                                                                                                                                             
ERROR: (gcloud.run.deploy) Cloud Run error: Container failed to start. Failed to start and then listen on the port defined by the PORT environment variable. Logs for this revision might contain more information.

Logs URL:
https://console.cloud.google.com/logs/viewer?project=nextbook-beta&resource=cloud_run_revision/service_name/nextbook-beta/revision_name/nextbook-beta-00001-raz&advancedFilter=resource.type%3D%22cloud_run_revision%22%0Aresource.labels.service_name%3D%22nextbook-beta%22%0Aresource.labels.revision_name%3D%22nextbook-beta-00001-raz%22

Resource https://cloud.google.com/run/docs/quickstarts/build-and-deploy#c_1, https://medium.com/@pheyhui/running-net-core-3-1-on-gcp-cloud-run-adff081e6745, https://medium.com/google-cloud/how-to-deploy-asp-net-core-3-x-to-google-app-engine-8813b0ee3521 From error log, the issue is likely due to the omission of dotnet ef database update within the Docker file since (not working on Ubuntu 20.04) --> switch to work on Ubuntu 18.04

ornwipa commented 3 years ago

To be able to run dotnet ef, need to add RUN dotnet tool install -g dotnet-ef and ENV PATH $PATH:/root/.dotnet/tools" to Dockerfile. Resource https://stackoverflow.com/questions/60129303/using-dotnet-ef-with-docker-and-asp-net-core-3-1, https://stackoverflow.com/questions/63559726/how-to-use-globally-installed-tools-between-dockerfile-stages, https://github.com/dotnet/dotnet-docker/issues/1658, https://entityframeworkcore.com/knowledge-base/57066856/dotnet-ef-not-found-in--net-core-3, https://jaliyaudagedara.blogspot.com/2020/03/dotnet-ef-command-doesnt-work-inside.html

ornwipa commented 3 years ago

On Azure CLI, set up Azure resources ...

$ az login
$ az group create -l westus -n recommenderGroup
$ az appservice plan create -g recommenderGroup -n recommenderPlan --sku F1
$ az webapp create -g recommenderGroup -p recommenderPlan -n nextbook

On Azure portal, recommenderGroup shows up in resource groups, recommenderPlan shows up as App Service Plan and nextbook shows up as App Service. http://**nextbook**.azurewebsites.net/ is up and running.

ornwipa commented 3 years ago

On Azure CLI, deploy project files to Azure via Git URL ...

$ az webapp deployment user set --user-name ornwipa
$ az webapp deployment source config-local-git -g recommenderGroup -n nextbook --out tsv
$ git remote add azure https://ornwipa@nextbook.scm.azurewebsites.net/nextbook.git
$ git push azure master

Final messages on terminal ...

remote: Deployment successful.
To https://nextbook.scm.azurewebsites.net/nextbook.git
 * [new branch]      master -> master

Upon opening web browser at http://nextbook.azurewebsites.net/ ...

HTTP Error 500.30 - ANCM In-Process Start Failure

Common solutions to this issue:

Troubleshooting steps:

For more information visit: https://go.microsoft.com/fwlink/?LinkID=2028265

ornwipa commented 3 years ago

image

ornwipa commented 3 years ago

image