The parameter -rm removes the container once it’s finished running, -it starts the container in interactive mode, -p 8080:80 exposes the port 80 of the container to port 8080 on the host and -name provides the name of the instance. With the Docker image created, we can run an instance of the image: docker run -rm -it -p 8000:80 core5-website -name core5-website-app NET 5 runtime images are used and the -t parameter provides the tag / name of the image. The -pull parameter ensures the latest upstream. Now we have our Dockerfile file in place, we can create our Docker image with the following command: docker build -pull -t core5-website. When the container starts, the ENTRYPOINT command runs and starts the app. The use of multiple FROM statements in the Dockerfile is known as a multi-stage build.Ĭopy the artifacts from the previous stage of the multi-stage build.Īnd the last command ENTRYPOINT takes an array of values with the first being the executable to run and the remaining values the arguments to use. NET 5 ASP.NET Core runtime image provided by Microsoft as the base image (rather than the SDK image) to reduce the size of the final image created. Now we’ve compiled and published the website, use the. The -no-restore option prevents the NuGet packages from being restored again as this was done previously in an earlier command. Swap to the /DockerSource/core5-website folder and compile the website with the output going to the /DockerOutput/Website folder in the image. If neither of these files change then Docker knows dotnet restore will yield the same output so can safely bypass this command and use the built-in cache for the corresponding dotnet restore layer instead.Ĭopy all the files from the core5-website project to the to the folder /DockerSource/core5-website in the image. In the previous step, we individually copied the solution and project files to form a separate layer to take advantage of Docker’s build cache. COPY like WORKDIR will create a target folder if it doesn’t exist in the image. WORKDIR will create this folder for us if it doesn’t exist.Ĭopy the solution file to the current image folder ( /DockerSource) and copy the project file to the folder /DockerSource/core5-website in the image. In the image we’re creating, change to the /DockerSource folder. NET 5 runtime base image later as this is smaller so will result in smaller output image. The SDK image is being used so we can compile the app but we’ll actually swap to the. Specifies the base image to use for our image. These commands can look a bit cryptic so I’ll explain them individually. RUN dotnet publish -c release -o /DockerOutput/Website -no -restoreįROM /dotnet/aspnet :5.0ĬOPY -from=build /DockerOutput/Website. # Copy csproj and restore as distinct layersĬOPY core5 -website/*.csproj. The first step to do this is to create a Dockerfile file at the solution root to hold the commands needed to build a Docker image: # First stageįROM /dotnet/sdk :5.0 AS build NET 5 ASP.NET Core website and run it up in Docker. Now when you run the site from Visual Studio, the “Welcome” page should display the time as well as detail that the website it’s not running in a container. Learn about building Web apps with ASP.NET Core. Var runningInContainer = Environment.GetEnvironmentVariable("DOTNET_RUNNING_IN_CONTAINER") = "true"
Add the following code to the /Pages/Index.cshtml page: = "Home page" We’ll make a change to the “Welcome” page to output the current time and to output (via an environment variable) whether the code is running in a container. The site is actually running in HTTP mode on port 5000 ( as well as HTTPS mode on port 5001 ( The website is configured to redirect HTTP requests to HTTPS via the HTTPS Redirection Middleware ( UseHttpsRedirection) in Startup.cs. Now if you run the project from Visual Studio, after accepting any development certificate prompts displayed, your browser should load the “Welcome” page on the URL Name the project core5-website in a new solution core5-docker-playarea. Use the target framework of “.NET 5” and “No Authentication”.
NET 5 ASP.NET Core website in Visual Studio 2019 for Mac and running this website from both Visual Studio and from a Docker image.īefore we start, the following prerequisites need to be installed: