|
| 1 | +# Development with Docker Containers for MVC |
| 2 | + |
| 3 | +ASP.NET Zero **Web** solution has a **docker folder** which contains various docker projects for your application to debug for your preferences. |
| 4 | + |
| 5 | +<img src="images/development-docker-mvc/docker-projects-folder-core-mvc.png" alt="docker-projects-folder-core-mvc" style="zoom:150%;" /> |
| 6 | + |
| 7 | +To get started with using docker for development, there are some prerequisites. |
| 8 | + |
| 9 | +### 1. Setting up the infrastructure |
| 10 | + |
| 11 | +Infrastructure contains **mssql-server-linux** as a replacement for your LocalDb and **redis** server for your applications. |
| 12 | + |
| 13 | +In your `aspnet-core\docker\infrastructure\` folder, you will find **run-infrastructure.ps1** file which uses **docker-compose.infrastructure.yml** file to setup your infrastructure. Running the shell script should have a similar output like below. |
| 14 | + |
| 15 | +<img src="images/development-docker-mvc/docker-infrastructure-run.png" alt="docker-infrastructure-run"/> |
| 16 | + |
| 17 | +After running the script, powershell will be hanging (non interactive) for you to see if there is any error occurred while creating the containers. You can use **Ctrl + X** to exit tailing the database logging to make the powershell interactive again. |
| 18 | + |
| 19 | +### 2. Setting up the self-signed certificate |
| 20 | + |
| 21 | +Your applications need certificate to enable https. Normally Visual Studio creates this certificate for you when you run your application for the first time however sometimes it doesn't when docker is involved. To overcome the issue, we create our own self-signed certificate for development. |
| 22 | + |
| 23 | +In your `aspnet-core\docker\certificate\` folder, you will find **create-certificate.ps1** file to create the certificate. After running the shell script, you should have a similar output like below. |
| 24 | +<img src="images/development-docker-mvc/docker-create-dev-certificate.png" alt="docker-create-dev-certificate"/> |
| 25 | + |
| 26 | +### 3. Running the migrator |
| 27 | + |
| 28 | +In your `aspnet-core\docker\migrator\` folder, you will find **run-migrator** file to apply migrations for your application. Running the shell script should have a similar output like below. |
| 29 | + |
| 30 | +<img src="images/development-docker-mvc/docker-migrate.png" alt="docker-migrate"/> |
| 31 | + |
| 32 | + |
| 33 | + |
| 34 | +## Running applications on containers |
| 35 | + |
| 36 | +In your applications **.Web** solution under **docker folder**, set as startup project you want to debug. Visual studio will instantly begin building the container. **Building containers may take some time** for the first time since it will be downloading required base images. |
| 37 | + |
| 38 | +Below you can see **docker-compose-mvc** debugging: |
| 39 | + |
| 40 | +<img src="images/development-docker-mvc/docker-mvc-running.png" alt="docker-mvc-running" style="zoom:100%;" /> |
| 41 | + |
| 42 | +There are three docker solutions you can set as startup project to run; |
| 43 | + |
| 44 | +1. **docker-compose-host:** Runs Web.Host project. |
| 45 | +2. **docker-compose-mvc:** Runs only Web.Mvc project. |
| 46 | +3. **docker-compose-public:** Runs Web.Mvc and Web.Public projects together. |
| 47 | + |
| 48 | +Each docker-compose files have override.yml files to set the other environments like certification information, docker volumes or AspnetCore environment. |
| 49 | + |
| 50 | +<img src="images/development-docker-mvc/docker-override-configuration.png" alt="docker-override-configuration" style="zoom:100%;" /> |
| 51 | + |
| 52 | +Configuration is separated between **docker development** and default **IIS development**. |
| 53 | + |
| 54 | +If you want to change the default connection string when running on containers; you need to change the environment variable **ConnectionStrings__Default** in corresponding **docker-compose.yml** shown below: |
| 55 | + |
| 56 | +<img src="images/development-docker-mvc/docker-cs-configuration.png" alt="docker-cs-configuration" style="zoom:100%;" /> |
| 57 | + |
| 58 | +This environment variable overrides the connection string which has been set in appsettings.json. This way you can use both development environments without mixing up the configurations. |
| 59 | + |
| 60 | +In some cases, you may get **container conflict errors** when switching between docker projects; like debugging **docker-compose-mvc** first then start debugging **docker-compose-public** after stopping the first one. |
| 61 | + |
| 62 | +<img src="images/development-docker-mvc/docker-container-conflict.png" alt="docker-cs-configuration" style="zoom:100%;" /> |
| 63 | + |
| 64 | +This occurs because the mvc container is still alive even if you stop debugging. To avoid container conflicts, you need to **Clean** the solution (right click to docker project and clean option) to remove the container completely before running the **docker-compose-public** which uses common containers like mvc container. |
0 commit comments