A Dockerfile is file containing build instructions to assemble a docker image, the file contains commands like RUN, COPY or WORKDIR.
Click here to learn more about dockerfile.
Before we start, ensure that your terminal is pointing to the right folder.
cd ~/environment/demo-containerise-dotnet/
Lets look at the Dockerfile which tells docker how to build our container image.
nano Dockerfile
You may use a different text editor instead of nano.
Note the commands in the Dockerfile
.
Each step has been commented to describe what the step action.
# FROM sets the base image as sdk:3.1 from microsoft
# https://hub.docker.com/_/microsoft-dotnet-core
FROM mcr.microsoft.com/dotnet/core/sdk:3.1 AS build
# Sets the working directory for commands like RUN , ENTRYPOINT and COPY
WORKDIR /source
# copy csproj and restore as distinct layers
COPY aspnetapp.sln .
COPY aspnetapp/*.csproj ./aspnetapp/
RUN dotnet restore -r linux-x64
# copy everything else to build app
COPY aspnetapp/. ./aspnetapp/
WORKDIR /source/aspnetapp
# Build the application to the /app folder
RUN dotnet publish -c release -o /app -r linux-x64 --self-contained false --no-restore
# final stage/image
# 3.1-bionic used for Ubuntu 18.04
FROM mcr.microsoft.com/dotnet/core/aspnet:3.1-bionic
WORKDIR /app
# Copy artifacts from the previous docker build
COPY --from=build /app ./
ENTRYPOINT ["./aspnetapp"]
References:
Save the file by pressing
1. CONTROL + X
2. Y
3. Enter
If you use a different text editor - the commands would be different.
Build the image using the tag awsbuilders-dotnet
.
docker build -t awsbuilders-dotnet .
Run the image in the background and expose the port 8000.
docker run --rm -d -it -p 8000:80 awsbuilders-dotnet
Use the command curl
to confirm the container is serving the webpage.
curl http://0.0.0.0:8000
The command
above prints the contents of the html file if the container is running.
If you would like to stop the container, you can use the following steps to find and stop the container.
docker ps
docker stop CONTAINER_ID