|
| 1 | +--- |
| 2 | +description: Create a Docker Compose application using ASP.NET Core and SQL Server on Linux in Docker. |
| 3 | +keywords: dotnet, .NET, Core, example, ASP.NET Core, SQL Server, mssql |
| 4 | +title: "Quickstart: Compose and ASP.NET Core with SQL Server" |
| 5 | +--- |
| 6 | + |
| 7 | +This quick-start guide demonstrates how to use Docker Engine on Linux and Docker |
| 8 | +Compose to set up and run the sample ASP.NET Core application using the |
| 9 | +[ASP.NET Core Build image](https://hub.docker.com/r/microsoft/aspnetcore-build/) |
| 10 | +with the |
| 11 | +[SQL Server on Linux image](https://hub.docker.com/r/microsoft/mssql-server-linux/). |
| 12 | +You just need to have [Docker Engine](https://docs.docker.com/engine/installation/) |
| 13 | +and [Docker Compose](https://docs.docker.com/compose/install/) installed on your |
| 14 | +platform of choice: Linux, Mac or Windows. |
| 15 | + |
| 16 | +For this sample, we will create a sample .NET Core Web Application using the |
| 17 | +`aspnetcore-build` Docker image. After that, we will create a `Dockerfile`, |
| 18 | +configure this app to use our SQL Server database, and then create a |
| 19 | +`docker-compose.yml` that will define the behavior of all of these components. |
| 20 | + |
| 21 | +> **Note**: This sample is made for Docker Engine on Linux. For Windows |
| 22 | +> Containers, visit |
| 23 | +> [Docker Labs for Windows Containers](https://github.com/docker/labs/tree/master/windows). |
| 24 | +
|
| 25 | +1. Create a new directory for your application. |
| 26 | + |
| 27 | + This directory will be the context of your docker-compose project. For |
| 28 | + [Docker for Windows](https://docs.docker.com/docker-for-windows/#/shared-drives) and |
| 29 | + [Docker for Mac](https://docs.docker.com/docker-for-mac/#/file-sharing), you |
| 30 | + have to set up file sharing for the volume that you need to map. |
| 31 | + |
| 32 | +1. Within your directory, use the `aspnetcore-build` Docker image to generate a |
| 33 | + sample web application within the container under the `/app` directory and |
| 34 | + into your host machine in the working directory: |
| 35 | + |
| 36 | + ```bash |
| 37 | + $ docker run -v ${PWD}:/app --workdir /app microsoft/aspnetcore-build:lts dotnet new mvc --auth Individual |
| 38 | + ``` |
| 39 | + |
| 40 | + > **Note**: If running in Docker for Windows, make sure to use Powershell |
| 41 | + or specify the absolute path of your app directory. |
| 42 | + |
| 43 | +1. Create a `Dockerfile` within your app directory and add the following content: |
| 44 | + |
| 45 | + ```dockerfile |
| 46 | + FROM microsoft/aspnetcore-build:lts |
| 47 | + COPY . /app |
| 48 | + WORKDIR /app |
| 49 | + RUN ["dotnet", "restore"] |
| 50 | + RUN ["dotnet", "build"] |
| 51 | + EXPOSE 80/tcp |
| 52 | + RUN chmod +x ./entrypoint.sh |
| 53 | + CMD /bin/bash ./entrypoint.sh |
| 54 | + ``` |
| 55 | + |
| 56 | + This file defines how to build the web app image. It will use the |
| 57 | + [microsoft/aspnetcore-build](https://hub.docker.com/r/microsoft/aspnetcore-build/), |
| 58 | + map the volume with the generated code, restore the dependencies, build the |
| 59 | + project and expose port 80. After that, it will call an `entrypoint` script |
| 60 | + that we will create in the next step. |
| 61 | + |
| 62 | +1. The `Dockerfile` makes use of an entrypoint to your webapp Docker |
| 63 | + image. Create this script in a file called `entrypoint.sh` and paste the |
| 64 | + contents below. |
| 65 | + |
| 66 | + > **Note**: Make sure to use UNIX line delimiters. The script won't work if |
| 67 | + > you use Windows-based delimiters (Carriage return and line feed). |
| 68 | +
|
| 69 | + ```bash |
| 70 | + #!/bin/bash |
| 71 | +
|
| 72 | + set -e |
| 73 | + run_cmd="dotnet run --server.urls http://*:80" |
| 74 | +
|
| 75 | + until dotnet ef database update; do |
| 76 | + >&2 echo "SQL Server is starting up" |
| 77 | + sleep 1 |
| 78 | + done |
| 79 | +
|
| 80 | + >&2 echo "SQL Server is up - executing command" |
| 81 | + exec $run_cmd |
| 82 | + ``` |
| 83 | +
|
| 84 | + This script will restore the database after it starts up, and then will run |
| 85 | + the application. This allows some time for the SQL Server database image to |
| 86 | + start up. |
| 87 | +
|
| 88 | +1. Create a `docker-compose.yml` file. Write the following in the file, and |
| 89 | + make sure to replace the password in the `SA_PASSWORD` environment variable |
| 90 | + under `db` below. This file will define the way the images will interact as |
| 91 | + independent services. |
| 92 | +
|
| 93 | + > **Note**: The SQL Server container requires a secure password to startup: |
| 94 | + > Minimum length 8 characters, including uppercase and lowercase letters, |
| 95 | + > base 10 digits and/or non-alphanumeric symbols. |
| 96 | +
|
| 97 | + ```none |
| 98 | +
|
| 99 | + version: '3' |
| 100 | +
|
| 101 | + services: |
| 102 | + web: |
| 103 | + build: . |
| 104 | + ports: |
| 105 | + - "8000:80" |
| 106 | + depends_on: |
| 107 | + - db |
| 108 | + db: |
| 109 | + image: "microsoft/mssql-server-linux" |
| 110 | + environment: |
| 111 | + SA_PASSWORD: "your_password" |
| 112 | + ACCEPT_EULA: "Y" |
| 113 | + ``` |
| 114 | +
|
| 115 | + This file defines the `web` and `db` micro-services, their relationship, the |
| 116 | + ports they are using, and their specific environment variables. |
| 117 | +
|
| 118 | +1. Go to `Startup.cs` and locate the function called `ConfigureServices` (Hint: |
| 119 | + it should be under line 42). Replace the entire function to use the following |
| 120 | + code (watch out for the brackets!). |
| 121 | +
|
| 122 | + > **Note**: Make sure to update the `Password` field in the `connection` |
| 123 | + > variable below to the one you defined in the `docker-compose.yml` file. |
| 124 | +
|
| 125 | + ```csharp |
| 126 | + [...] |
| 127 | + public void ConfigureServices(IServiceCollection services) |
| 128 | + { |
| 129 | + // Database connection string. |
| 130 | + // Make sure to update the Password value below from "your_password" to your actual password. |
| 131 | + var connection = @"Server=db;Database=master;User=sa;Password=your_password;"; |
| 132 | +
|
| 133 | + // This line uses 'UseSqlServer' in the 'options' parameter |
| 134 | + // with the connection string defined above. |
| 135 | + services.AddDbContext<ApplicationDbContext>( |
| 136 | + options => options.UseSqlServer(connection)); |
| 137 | +
|
| 138 | + services.AddIdentity<ApplicationUser, IdentityRole>() |
| 139 | + .AddEntityFrameworkStores<ApplicationDbContext>() |
| 140 | + .AddDefaultTokenProviders(); |
| 141 | +
|
| 142 | + services.AddMvc(); |
| 143 | +
|
| 144 | + // Add application services. |
| 145 | + services.AddTransient<IEmailSender, AuthMessageSender>(); |
| 146 | + services.AddTransient<ISmsSender, AuthMessageSender>(); |
| 147 | + } |
| 148 | + [...] |
| 149 | + ``` |
| 150 | + |
| 151 | +1. Ready! You can now run the `docker-compose build` command. |
| 152 | +
|
| 153 | + ```bash |
| 154 | + $ docker-compose build |
| 155 | + ``` |
| 156 | +
|
| 157 | +1. Make sure you allocate at least 4GB of memory to Docker Engine. Here is how |
| 158 | + to do it on |
| 159 | + [Docker for Mac](https://docs.docker.com/docker-for-mac/#/advanced) and |
| 160 | + [Docker for Windows](https://docs.docker.com/docker-for-windows/#/advanced). |
| 161 | + This is necessary to run the SQL Server on Linux container. |
| 162 | +
|
| 163 | +1. Run the `docker-compose up` command. After a few seconds, you should be able |
| 164 | + to open [localhost:8000](http://localhost:8000) and see the ASP.NET core |
| 165 | + sample website. The application is listening on port 80 by default, but we |
| 166 | + mapped it to port 8000 in the `docker-compose.yml`. |
| 167 | +
|
| 168 | + ```bash |
| 169 | + $ docker-compose up |
| 170 | + ``` |
| 171 | +
|
| 172 | + Go ahead and try out the website! This sample will use the SQL Server |
| 173 | + database image in the back-end for authentication. |
| 174 | +
|
| 175 | +Ready! You now have a ASP.NET Core application running against SQL Server in |
| 176 | +Docker Compose! This sample made use of some of the most popular Microsoft |
| 177 | +products for Linux. To learn more about Windows Containers, check out |
| 178 | +[Docker Labs for Windows Containers](https://github.com/docker/labs/tree/master/windows) |
| 179 | +to try out .NET Framework and more SQL Server tutorials. |
| 180 | +
|
| 181 | +## Next steps |
| 182 | +
|
| 183 | +- [Build your app using SQL Server](https://www.microsoft.com/en-us/sql-server/developer-get-started/?utm_medium=Referral&utm_source=docs.docker.com) |
| 184 | +- [SQL Server on Docker Hub](https://hub.docker.com/r/microsoft/mssql-server-linux/) |
| 185 | +- [ASP.NET Core](https://www.asp.net/core) |
| 186 | +- [ASP.NET Core Docker image](https://hub.docker.com/r/microsoft/aspnetcore/) on DockerHub |
0 commit comments