This project is build with ASP.NET Core framwork, conecting to a database in SQL Server 2017 on Docker. To setup a simple web app with ASP.NET Core framework, you can refer to this tutorial on microsoft.com
To run this project on your local machine, Setup SQL Server on Docker and connect to it on local dev environment.
To deploy this project to Docker, Setup SQL Server on Docker and follow steps 6, 7, 8, 9 on the Setup project to deploy to Docker section.
- Download SQL Server container
docker pull microsoft/mssql-server-linux:2017-latest- Install SQL Server container (replace all
<YourStrong!Passw0rd>after this to your own password)
docker run -e "ACCEPT_EULA=Y" -e "SA_PASSWORD=<YourStrong!Passw0rd>" -p 1433:1433 --name sql1 -d microsoft/mssql-server-linux:2017-latest- View docker containers to check if microsoft/mssql-server-linux is running
docker ps- Access your database from Database IDE with this configuration:
Host: localhost
Port: 1433
User: SA
Password: <YourStrong!Passw0rd>-
On your Database IDE, create a database instance (the instance name will be
<YourDatabaseInstance>below) -
In
appsettings.jsonfile of your project, add a default connection string (replace all<YourDatabaseInstance>after this to your own instance name)
ConnectionStrings": {
"DefaultConnection": "Data Source=127.0.0.1,1433;Initial Catalog=<YourDatabaseInstance>;User ID=sa;Password=<YourStrong!Passw0rd>"
}- In
Startup.csfile of your project, add a SQLServer connection (or change from the original SQLite connection)
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
.
.
.
services.AddDbContext<ApplicationDbContext>(options =>
options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")));
.
.
.
}-
Create a
Dockerfileon your project's root directory or, Right click project on Solution Explorer > Add > Add Docker Support -
Change
Dockerfileto
FROM microsoft/aspnetcore-build:lts
COPY . /app
WORKDIR /app
RUN dotnet restore ProjectName.csproj
RUN dotnet build ProjectName.csproj
EXPOSE 80/tcp
RUN chmod +x ./entrypoint.sh
CMD /bin/bash ./entrypoint.sh(replace ProjectName to your own project name)
- Create
entrypoint.shfile on your project directory (Same directory asDockerfile), with the following content
#!/bin/bash
set -e
run_cmd="dotnet run --server.urls http://*:80"
until dotnet ef database update; do
>&2 echo "SQL Server is starting up"
sleep 1
done
>&2 echo "SQL Server is up - executing command"
exec $run_cmd- Create or update your
docker-compose.ymlfile on your project's root directory with this content
version: '2'
services:
movievotingcoresql:
image: movievotingcoresql
build:
context: .
dockerfile: Dockerfile
ports:
- "8000:80"
depends_on:
- db
db:
image: microsoft/mssql-server-linux
environment:
SA_PASSWORD: "<YourStrong!Passw0rd>"
ACCEPT_EULA: "Y"
ports:
- "1433:1433"
volumes:
- db-data:/var/opt/mssql/data
volumes:
db-data:- In
Startup.csfile of your project, change you SQLServer connection
public void ConfigureServices(IServiceCollection services)
{
.
.
.
// Database connection string.
// Make sure to update the Password value below from "<YourStrong!Passw0rd>" to your actual password.
var connection = @"Server=db;Database=<YourDatabaseInstance>;User=sa;Password=<YourStrong!Passw0rd>;";
// This line uses 'UseSqlServer' in the 'options' parameter
// with the connection string defined above.
services.AddDbContext<ApplicationDbContext>(
options => options.UseSqlServer(connection));
.
.
.
}- On command prompt, navigate to project's directory
- On command prompt, run
docker-compose build- On command prompt, run
docker-compose upNote: If you run into issue with message similar to Specify which project file to use because this '/app' contains more than one project file., remove docker-compose.dcproj from your project's root directory.
- Check if your
appanddbcontainers are up and running
docker ps- Visit http://localhost:8000
- To stop your the app, open a new tab on your command prompt and run
docker-compose down- Simple ASP.NET Core tutorial - https://docs.microsoft.com/en-us/aspnet/core/tutorials/razor-pages-mac/?view=aspnetcore-2.1
- Run SQL Server 2017 container image on Docker - https://docs.microsoft.com/en-us/sql/linux/quickstart-install-connect-docker?view=sql-server-linux-2017
- Use SQL Server with Docker on local ASP.NET Core project - https://codebrains.io/how-to-use-sql-server-with-docker-and-asp-net-core-on-mac/
- ASP.NET Core with SQL Server on Docker - https://docs.docker.com/compose/aspnet-mssql-compose/