From f2986c94561317579c08efa55927f9e1c86f9c34 Mon Sep 17 00:00:00 2001 From: Jonas Soderholm Date: Sat, 27 Jan 2018 21:48:57 +0000 Subject: [PATCH 1/3] Added docker support for debugging --- AllReadyApp/Dockerfile-web | 12 ++++++++++ AllReadyApp/Web-App/AllReady/Startup.cs | 13 +++++++---- AllReadyApp/Web-App/AllReady/appsettings.json | 8 ++++++- AllReadyApp/docker-compose.yml | 22 +++++++++++++++++++ AllReadyApp/entrypoint.sh | 12 ++++++++++ 5 files changed, 62 insertions(+), 5 deletions(-) create mode 100644 AllReadyApp/Dockerfile-web create mode 100644 AllReadyApp/docker-compose.yml create mode 100644 AllReadyApp/entrypoint.sh diff --git a/AllReadyApp/Dockerfile-web b/AllReadyApp/Dockerfile-web new file mode 100644 index 000000000..d0bb0f2bb --- /dev/null +++ b/AllReadyApp/Dockerfile-web @@ -0,0 +1,12 @@ +FROM microsoft/aspnetcore-build:2.0 +ENV ASPNETCORE_ENVIRONMENT=Development +ENV ALLREADY_DOCKER_DEBUG=TRUE +COPY . /app +WORKDIR /app +RUN ["dotnet","restore","AllReadyWebOnly.sln"] +WORKDIR /app/Web-App/AllReady +RUN ["npm", "install", "--unsafe-perm"] +RUN ["dotnet","build"] +EXPOSE 80/tcp +RUN chmod +x ../../entrypoint.sh +CMD /bin/bash ../../entrypoint.sh \ No newline at end of file diff --git a/AllReadyApp/Web-App/AllReady/Startup.cs b/AllReadyApp/Web-App/AllReady/Startup.cs index c4b8766de..5e3585232 100644 --- a/AllReadyApp/Web-App/AllReady/Startup.cs +++ b/AllReadyApp/Web-App/AllReady/Startup.cs @@ -52,8 +52,11 @@ public IServiceProvider ConfigureServices(IServiceCollection services) }); // Add Entity Framework services to the services container. - services.AddDbContext(options => options.UseSqlServer(Configuration["Data:DefaultConnection:ConnectionString"])); - + var allReadyDbConnectionString = Environment.GetEnvironmentVariable("ALLREADY_DOCKER_DEBUG") == "TRUE" ? + Configuration["Data:DockerDefaultConnection:ConnectionString"] : + Configuration["Data:DefaultConnection:ConnectionString"]; + services.AddDbContext(options => options.UseSqlServer(allReadyDbConnectionString)); + Options.LoadConfigurationOptions(services, Configuration); services.AddIdentity(options => @@ -168,7 +171,10 @@ public IServiceProvider ConfigureServices(IServiceCollection services) }); //Hangfire - services.AddHangfire(configuration => configuration.UseSqlServerStorage(Configuration["Data:HangfireConnection:ConnectionString"])); + var hangFireConnectionString = Environment.GetEnvironmentVariable("ALLREADY_DOCKER_DEBUG") == "TRUE" ? + Configuration["Data:DockerHangfireConnection:ConnectionString"] : + Configuration["Data:HangfireConnection:ConnectionString"]; + services.AddHangfire(configuration => configuration.UseSqlServerStorage(hangFireConnectionString)); services.AddScoped(); services.AddScoped(); @@ -184,7 +190,6 @@ public void Configure(IApplicationBuilder app, IHostingEnvironment env, AllReady { // Put first to avoid issues with OPTIONS when calling from Angular/Browser. app.UseCors("allReady"); - app.UseSession(); // Add the following to the request pipeline only in development environment. diff --git a/AllReadyApp/Web-App/AllReady/appsettings.json b/AllReadyApp/Web-App/AllReady/appsettings.json index 97f084d92..fb1ebd47a 100644 --- a/AllReadyApp/Web-App/AllReady/appsettings.json +++ b/AllReadyApp/Web-App/AllReady/appsettings.json @@ -33,11 +33,17 @@ }, "Data": { "DefaultConnection": { - "ConnectionString": "Server=(localdb)\\MSSQLLocalDB;Database=AllReady;Integrated Security=true;MultipleActiveResultsets=true;" + "ConnectionString": "Server=(localdb)\\MSSQLLocalDB;Database=AllReady;Integrated Security=true;MultipleActiveResultsets=true;" + }, + "DockerDefaultConnection": { + "ConnectionString": "Server=db,1433;Initial Catalog=master;Persist Security Info=False;User ID=sa;Password=yourStrong(!)Password;MultipleActiveResultSets=False;Encrypt=True;TrustServerCertificate=True;Connection Timeout=30;" }, "HangfireConnection": { "ConnectionString": "Server=(localdb)\\MSSQLLocalDB;Database=AllReady;Integrated Security=true;MultipleActiveResultsets=true;" }, + "DockerHangfireConnection": { + "ConnectionString": "Server=db,1433;Initial Catalog=master;Persist Security Info=False;User ID=sa;Password=yourStrong(!)Password;MultipleActiveResultSets=False;Encrypt=True;TrustServerCertificate=True;Connection Timeout=30;" + }, "Storage": { "AzureStorage": "[storagekey]", "EnableAzureQueueService": "false", diff --git a/AllReadyApp/docker-compose.yml b/AllReadyApp/docker-compose.yml new file mode 100644 index 000000000..d28b20a6d --- /dev/null +++ b/AllReadyApp/docker-compose.yml @@ -0,0 +1,22 @@ +version: "3" +services: + web: + build: + context: . + dockerfile: Dockerfile-web + ports: + - "8000:80" + depends_on: + - db + db: + image: "microsoft/mssql-server-linux:2017-CU3" + volumes: + - sql-server-data:/var/opt/mssql + ports: + - "1433:1433" + environment: + SA_PASSWORD: "yourStrong(!)Password" + ACCEPT_EULA: "Y" + +volumes: + sql-server-data: \ No newline at end of file diff --git a/AllReadyApp/entrypoint.sh b/AllReadyApp/entrypoint.sh new file mode 100644 index 000000000..a725f0adc --- /dev/null +++ b/AllReadyApp/entrypoint.sh @@ -0,0 +1,12 @@ +#!/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 \ No newline at end of file From c4ad825bc69d03402c9750c5b339e3871ae95f4a Mon Sep 17 00:00:00 2001 From: Jonas Soderholm Date: Sat, 3 Feb 2018 15:23:13 +0000 Subject: [PATCH 2/3] Added debuging with VS code and faster build --- AllReadyApp/.dockerignore | 12 +++++++ AllReadyApp/.vscode/launch.json | 24 ++++++++++++++ AllReadyApp/Dockerfile-web | 12 ------- AllReadyApp/Web-App/AllReady/Dockerfile | 32 +++++++++++++++++++ AllReadyApp/Web-App/AllReady/Dockerfile.debug | 12 +++++++ AllReadyApp/docker-compose.yml | 7 +++- AllReadyApp/entrypoint.sh | 3 +- 7 files changed, 87 insertions(+), 15 deletions(-) create mode 100644 AllReadyApp/.dockerignore create mode 100644 AllReadyApp/.vscode/launch.json delete mode 100644 AllReadyApp/Dockerfile-web create mode 100644 AllReadyApp/Web-App/AllReady/Dockerfile create mode 100644 AllReadyApp/Web-App/AllReady/Dockerfile.debug diff --git a/AllReadyApp/.dockerignore b/AllReadyApp/.dockerignore new file mode 100644 index 000000000..2171c0476 --- /dev/null +++ b/AllReadyApp/.dockerignore @@ -0,0 +1,12 @@ +.dockerignore +.env +.git +.gitignore +.vs +.vscode +docker-compose.yml +docker-compose.*.yml +*/bin +*/obj +!obj/Docker/publish/* +!obj/Docker/empty/ \ No newline at end of file diff --git a/AllReadyApp/.vscode/launch.json b/AllReadyApp/.vscode/launch.json new file mode 100644 index 000000000..606a3adeb --- /dev/null +++ b/AllReadyApp/.vscode/launch.json @@ -0,0 +1,24 @@ +{ + "version": "0.2.0", + "configurations": [ + { + "name": "Debug in Docker", + "type": "coreclr", + "request": "launch", + "program": "/app/Web-App/AllReady/bin/Debug/netcoreapp2.0/AllReady.dll", + "cwd": "/app/Web-App/AllReady", + "sourceFileMap": { + "/app": "${workspaceRoot}" + }, + "pipeTransport": { + "pipeProgram": "docker", + "pipeCwd": "${workspaceRoot}", + "pipeArgs": [ + "exec -i allreadyweb" + ], + "quoteArgs": false, + "debuggerPath": "/root/vsdbg/vsdbg" + } + } + ] +} \ No newline at end of file diff --git a/AllReadyApp/Dockerfile-web b/AllReadyApp/Dockerfile-web deleted file mode 100644 index d0bb0f2bb..000000000 --- a/AllReadyApp/Dockerfile-web +++ /dev/null @@ -1,12 +0,0 @@ -FROM microsoft/aspnetcore-build:2.0 -ENV ASPNETCORE_ENVIRONMENT=Development -ENV ALLREADY_DOCKER_DEBUG=TRUE -COPY . /app -WORKDIR /app -RUN ["dotnet","restore","AllReadyWebOnly.sln"] -WORKDIR /app/Web-App/AllReady -RUN ["npm", "install", "--unsafe-perm"] -RUN ["dotnet","build"] -EXPOSE 80/tcp -RUN chmod +x ../../entrypoint.sh -CMD /bin/bash ../../entrypoint.sh \ No newline at end of file diff --git a/AllReadyApp/Web-App/AllReady/Dockerfile b/AllReadyApp/Web-App/AllReady/Dockerfile new file mode 100644 index 000000000..72e3b8e38 --- /dev/null +++ b/AllReadyApp/Web-App/AllReady/Dockerfile @@ -0,0 +1,32 @@ +FROM microsoft/aspnetcore-build:2.0 +ENV ASPNETCORE_ENVIRONMENT=Development +ENV ALLREADY_DOCKER_DEBUG=TRUE + +#Install debugger +RUN apt-get update +RUN apt-get install curl -y unzip +RUN curl -sSL https://aka.ms/getvsdbgsh | bash /dev/stdin -v latest -l ~/vsdbg + +# Install npm packages +WORKDIR /app/Web-App/AllReady +COPY Web-App/AllReady/package.json . +COPY Web-App/AllReady/package-lock.json . +RUN ["npm", "install"] + +# Run dotnet restore +WORKDIR /app +COPY *.sln ./ +COPY Web-App/AllReady/AllReady.csproj Web-App/AllReady/ +COPY AllReady.Core/AllReady.Core.csproj AllReady.Core/ +RUN dotnet restore AllReadyWebOnly.sln +COPY . . +WORKDIR /app/Web-App/AllReady + +# Build project +RUN ["dotnet","build"] + +# Run gulp tasks +RUN gulp clean && gulp min +EXPOSE 80/tcp +RUN chmod +x ../../entrypoint.sh +CMD /bin/bash ../../entrypoint.sh \ No newline at end of file diff --git a/AllReadyApp/Web-App/AllReady/Dockerfile.debug b/AllReadyApp/Web-App/AllReady/Dockerfile.debug new file mode 100644 index 000000000..c53d43aaa --- /dev/null +++ b/AllReadyApp/Web-App/AllReady/Dockerfile.debug @@ -0,0 +1,12 @@ +FROM microsoft/aspnetcore-build:2.0 +RUN mkdir app + +#Install debugger +RUN apt-get update +RUN apt-get install curl -y unzip +RUN curl -sSL https://aka.ms/getvsdbgsh | bash /dev/stdin -v latest -l ~/vsdbg + +EXPOSE 80/tcp + +#Keep the debugger container on +ENTRYPOINT ["tail", "-f", "/dev/null"] \ No newline at end of file diff --git a/AllReadyApp/docker-compose.yml b/AllReadyApp/docker-compose.yml index d28b20a6d..998de186b 100644 --- a/AllReadyApp/docker-compose.yml +++ b/AllReadyApp/docker-compose.yml @@ -3,13 +3,18 @@ services: web: build: context: . - dockerfile: Dockerfile-web + dockerfile: Web-App/AllReady/Dockerfile + container_name: allreadyweb + environment: + - ASPNETCORE_ENVIRONMENT=Development + - ALLREADY_DOCKER_DEBUG=TRUE ports: - "8000:80" depends_on: - db db: image: "microsoft/mssql-server-linux:2017-CU3" + container_name: allreadysqlserver volumes: - sql-server-data:/var/opt/mssql ports: diff --git a/AllReadyApp/entrypoint.sh b/AllReadyApp/entrypoint.sh index a725f0adc..4c439462f 100644 --- a/AllReadyApp/entrypoint.sh +++ b/AllReadyApp/entrypoint.sh @@ -1,8 +1,7 @@ #!/bin/bash set -e -run_cmd="dotnet run --server.urls http://*:80" - +run_cmd="tail -f /dev/null" until dotnet ef database update; do >&2 echo "SQL Server is starting up" sleep 1 From 6f62f3786b576cb3d40c6b0b876801fd6b3f7993 Mon Sep 17 00:00:00 2001 From: Jonas Soderholm Date: Thu, 22 Feb 2018 21:08:10 +0000 Subject: [PATCH 3/3] Moving docker connection strings and fixing line endings in windows@ --- .gitattributes | 5 +++++ AllReadyApp/Web-App/AllReady/Dockerfile | 2 -- AllReadyApp/Web-App/AllReady/Startup.cs | 14 ++++---------- AllReadyApp/Web-App/AllReady/appsettings.json | 6 ------ AllReadyApp/docker-compose.yml | 5 +++-- AllReadyApp/entrypoint.sh | 2 +- 6 files changed, 13 insertions(+), 21 deletions(-) create mode 100644 .gitattributes diff --git a/.gitattributes b/.gitattributes new file mode 100644 index 000000000..9cab97b11 --- /dev/null +++ b/.gitattributes @@ -0,0 +1,5 @@ +# Set default behavior to automatically normalize line endings. +* text=auto + +# Always convert bash script line endings to LF on checkout. +*.sh text eol=lf \ No newline at end of file diff --git a/AllReadyApp/Web-App/AllReady/Dockerfile b/AllReadyApp/Web-App/AllReady/Dockerfile index 72e3b8e38..6311b8662 100644 --- a/AllReadyApp/Web-App/AllReady/Dockerfile +++ b/AllReadyApp/Web-App/AllReady/Dockerfile @@ -1,6 +1,4 @@ FROM microsoft/aspnetcore-build:2.0 -ENV ASPNETCORE_ENVIRONMENT=Development -ENV ALLREADY_DOCKER_DEBUG=TRUE #Install debugger RUN apt-get update diff --git a/AllReadyApp/Web-App/AllReady/Startup.cs b/AllReadyApp/Web-App/AllReady/Startup.cs index 5e3585232..b793ec0ad 100644 --- a/AllReadyApp/Web-App/AllReady/Startup.cs +++ b/AllReadyApp/Web-App/AllReady/Startup.cs @@ -52,11 +52,8 @@ public IServiceProvider ConfigureServices(IServiceCollection services) }); // Add Entity Framework services to the services container. - var allReadyDbConnectionString = Environment.GetEnvironmentVariable("ALLREADY_DOCKER_DEBUG") == "TRUE" ? - Configuration["Data:DockerDefaultConnection:ConnectionString"] : - Configuration["Data:DefaultConnection:ConnectionString"]; - services.AddDbContext(options => options.UseSqlServer(allReadyDbConnectionString)); - + services.AddDbContext(options => options.UseSqlServer(Configuration["Data:DefaultConnection:ConnectionString"])); + Options.LoadConfigurationOptions(services, Configuration); services.AddIdentity(options => @@ -171,11 +168,8 @@ public IServiceProvider ConfigureServices(IServiceCollection services) }); //Hangfire - var hangFireConnectionString = Environment.GetEnvironmentVariable("ALLREADY_DOCKER_DEBUG") == "TRUE" ? - Configuration["Data:DockerHangfireConnection:ConnectionString"] : - Configuration["Data:HangfireConnection:ConnectionString"]; - services.AddHangfire(configuration => configuration.UseSqlServerStorage(hangFireConnectionString)); - + services.AddHangfire(configuration => configuration.UseSqlServerStorage(Configuration["Data:HangfireConnection:ConnectionString"])); + services.AddScoped(); services.AddScoped(); services.AddScoped(); diff --git a/AllReadyApp/Web-App/AllReady/appsettings.json b/AllReadyApp/Web-App/AllReady/appsettings.json index fb1ebd47a..5660e86b2 100644 --- a/AllReadyApp/Web-App/AllReady/appsettings.json +++ b/AllReadyApp/Web-App/AllReady/appsettings.json @@ -35,15 +35,9 @@ "DefaultConnection": { "ConnectionString": "Server=(localdb)\\MSSQLLocalDB;Database=AllReady;Integrated Security=true;MultipleActiveResultsets=true;" }, - "DockerDefaultConnection": { - "ConnectionString": "Server=db,1433;Initial Catalog=master;Persist Security Info=False;User ID=sa;Password=yourStrong(!)Password;MultipleActiveResultSets=False;Encrypt=True;TrustServerCertificate=True;Connection Timeout=30;" - }, "HangfireConnection": { "ConnectionString": "Server=(localdb)\\MSSQLLocalDB;Database=AllReady;Integrated Security=true;MultipleActiveResultsets=true;" }, - "DockerHangfireConnection": { - "ConnectionString": "Server=db,1433;Initial Catalog=master;Persist Security Info=False;User ID=sa;Password=yourStrong(!)Password;MultipleActiveResultSets=False;Encrypt=True;TrustServerCertificate=True;Connection Timeout=30;" - }, "Storage": { "AzureStorage": "[storagekey]", "EnableAzureQueueService": "false", diff --git a/AllReadyApp/docker-compose.yml b/AllReadyApp/docker-compose.yml index 998de186b..80356d06a 100644 --- a/AllReadyApp/docker-compose.yml +++ b/AllReadyApp/docker-compose.yml @@ -6,8 +6,9 @@ services: dockerfile: Web-App/AllReady/Dockerfile container_name: allreadyweb environment: - - ASPNETCORE_ENVIRONMENT=Development - - ALLREADY_DOCKER_DEBUG=TRUE + ASPNETCORE_ENVIRONMENT: Development + Data__DefaultConnection__ConnectionString: "Server=db,1433;Initial Catalog=master;Persist Security Info=False;User ID=sa;Password=yourStrong(!)Password;MultipleActiveResultSets=False;Encrypt=True;TrustServerCertificate=True;Connection Timeout=30;" + Data__HangfireConnection__ConnectionString: "Server=db,1433;Initial Catalog=master;Persist Security Info=False;User ID=sa;Password=yourStrong(!)Password;MultipleActiveResultSets=False;Encrypt=True;TrustServerCertificate=True;Connection Timeout=30;" ports: - "8000:80" depends_on: diff --git a/AllReadyApp/entrypoint.sh b/AllReadyApp/entrypoint.sh index 4c439462f..1a0d33b03 100644 --- a/AllReadyApp/entrypoint.sh +++ b/AllReadyApp/entrypoint.sh @@ -7,5 +7,5 @@ until dotnet ef database update; do sleep 1 done ->&2 echo "SQL Server is up - executing command" +>&2 echo "App and SQL Server is running" exec $run_cmd \ No newline at end of file