Docker
  • Introduction
    • What is Virtualization?
      • What is a Hypervisor?
      • Microsoft Hyper-V
    • What is Containerization?
      • What is docker?
      • What is a container image?
      • When to use containers?
      • When not to use Docker containers
  • Basics
    • ▶️Pull and run an image
      • Trivia
    • Basic docker commands
    • Container life cycle
  • Build an Image
    • ▶️Console app experiment
      • Hello-world image
    • What is a docker file?
      • Our docker file explained
    • ▶️Web app experiment
      • Create application
      • Create a docker file
      • Build an image
      • Run your image
      • Manipulating containers
  • Layers & Stages
    • Introduction
    • Image layers
    • Multistage images
Powered by GitBook
On this page
  • Create a docker file
  • Create a docker ignore file
  1. Build an Image
  2. Web app experiment

Create a docker file

Create a docker file

In the dotnet-docker directory, create a file named Dockerfile

# syntax=docker/dockerfile:1

FROM mcr.microsoft.com/dotnet/sdk:7.0 as build-env
WORKDIR /src
COPY src/*.csproj .
RUN dotnet restore
COPY src .
RUN dotnet publish -c Release -o /publish

FROM mcr.microsoft.com/dotnet/aspnet:7.0 as runtime
WORKDIR /publish
COPY --from=build-env /publish .
EXPOSE 80
ENTRYPOINT ["dotnet", "myWebApp.dll"]

Create a docker ignore file

To make your build context as small as possible, add a .dockerignore file to your dotnet-docker folder and copy the following into it.

**/bin/
**/obj/

Directory structure

Just to recap, we created a directory in our local machine called dotnet-docker and created a simple .NET application in the src folder. We also created a Dockerfile containing the commands to build an image as well as a .dockerignore file. The dotnet-docker directory structure should now look like:

├── dotnet-docker
│ ├── src/
│ ├── Dockerfile
│ ├── .dockerignore
Explanation for docker file
# syntax=docker/dockerfile:1

# Specify the base image for the build stage
FROM mcr.microsoft.com/dotnet/sdk:7.0 as build-env

# Set the working directory inside the container for the build stage
WORKDIR /src

# Copy the project's .csproj files to the container's working directory
COPY src/*.csproj .

# Restore the dependencies of the project
RUN dotnet restore

# Copy the entire project source code to the container's working directory
COPY src .

# Publish the application in release mode to the /publish directory
RUN dotnet publish -c Release -o /publish

# Specify the base image for the runtime stage
FROM mcr.microsoft.com/dotnet/aspnet:7.0 as runtime

# Set the working directory inside the container for the runtime stage
WORKDIR /publish

# Copy the published output from the build stage to the container's working directory
COPY --from=build-env /publish .

# Expose port 80 for the container
EXPOSE 80

# Set the entry point command to run when the container starts
ENTRYPOINT ["dotnet", "myWebApp.dll"]
PreviousCreate applicationNextBuild an image

Last updated 1 year ago

▶️