Skip to content

Latest commit

 

History

History
84 lines (56 loc) · 2.19 KB

2.Multi-Stage-Builds.md

File metadata and controls

84 lines (56 loc) · 2.19 KB

2.Multi Stage Builds

Multi-stage builds is a feature in a Dockerfiles that enables you to create container images with much smaller footprint. This makes it super easy for us to use container to build our application whithout having to have any build time depedencies running on your machine.

About WebApp

Exercise

In this exercise, we will build a docker image by compiling and building C# WebApp.

Download Sample Code

Download WebApp Sample and unzip into WebApp folder.

View Dockerfile

#1. Open Termminal / Command line and navigate to folder Multi-Stage-Web-App and open Dockerfile

#2. Notice multi-stage build

Dockerfile

#Starts with slim image required to run app
FROM mcr.microsoft.com/dotnet/core/aspnet:2.2-stretch-slim AS base
WORKDIR /app
EXPOSE 80
EXPOSE 443

# Pull fully loaded image that can actually compile and build ASP.NET app
FROM mcr.microsoft.com/dotnet/core/sdk:2.2-stretch AS build
WORKDIR /src
COPY ["Multi-Stage-Web-App/Multi-Stage-Web-App.csproj", "Multi-Stage-Web-App/"]
RUN dotnet restore "Multi-Stage-Web-App/Multi-Stage-Web-App.csproj"
COPY . .
WORKDIR "/src/Multi-Stage-Web-App"
RUN dotnet build "Multi-Stage-Web-App.csproj" -c Release -o /app

# Now run publish to collect required Artifacts
FROM build AS publish
RUN dotnet publish "Multi-Stage-Web-App.csproj" -c Release -o /app

# Now copy the Artifacts onto base image
FROM base AS final
WORKDIR /app
COPY --from=publish /app .
ENTRYPOINT ["dotnet", "Multi-Stage-Web-App.dll"]

Docker Build

Let's build an image from a Dockerfile and tag it as MyWebApp.

#1. Open Termminal / Command line and navigate to WebApp folder (one folder above Dockerfile)

#2. Now let's build the docker  
Docker build -t mywebapp -f ./Multi-Stage-Web-App/Dockerfile .

View the Docker Images

List all the Docker Images, you should see MyWebApp.

docker image ls

Docker Run

A container is a process that runs on a host and is statred using Docker Run

Let's run our web app and explose it on port 8081.

docker run -d -p 8081:80 mywebapp

Stretch Goal

Identify command to stop the container and remove it (hint: rm).