Docker
# Documentation for creating Dockerfiles
[[TOC]]
Intro¶
Dockerfiles provide a simple and reusable way to automate the process of creating docker images, which are then used to run docker containers. They contain a set of instructions that are executed by the Docker Engine when docker build is ran. Normally this happens each time the build pipeline executes.
Typically there will be one dockerfile per deployable app, such as an Angular frontend or .NET Api. But a single dockerfile can also output multiple apps, for example a solution containing both an Api and Azure Function. Other times a single dockerfile will perform multiple builds, packaging them into a single app that gets deployed, such as a repo that contains both an Api and Ui, where the Api is used to host it's own Angular SPA.
Examples¶
Example 1¶
::: mermaid graph LR; 1[dockerfile] ==>|docker build| 2[npm run build... \n or \n dotnet build... \n or etc...] ==>|Outputs| 3{Docker Image} ==>|Deploy| 4(((Azure App Service WebApp))) :::
Example 2¶
::: mermaid graph LR; 1a[ui.dockerfile] ==>|docker build| 2a[npm run build...] ==>|Outputs| 3a{UI\nDocker Image} ==>|Deploy| 4a(((Azure App Service WebApp)))
1b[api.dockerfile] ==>|docker build| 2b[dotnet build...] ==>|Outputs| 3b{Docker Image} ==>|Deploy| 4b(((Azure App Service WebApp))) :::
Example 3¶
::: mermaid graph LR; 1[dockerfile] ==>|docker build| 2[RUN dotnet restore *.sln... \nRUN dotnet build api.csproj... \nRUN dotnet build func.csproj...] ==>|Outputs| 3{Combined Api/Func\nDocker Image} 3 ==>|API Deploy| 4a(((Azure App Service WebApp))) 3 ==>|Func Deploy| 4b(((Azure App Service FunctionApp))) :::
Example 3b¶
::: mermaid graph TB subgraph g1["Docker Pipeline - Example 3b"] 1[("dockerfile")] 2["RUN dotnet restore *.sln... \nRUN dotnet build api.csproj... \nRUN dotnet build func.csproj..."] 3{"Combined Build Files\nDocker Image"} 4a["Api\nDocker Image"] 4b["Func\nDocker Image"] 5a[["Azure App Service WebApp"]] 5b[["Azure App Service FunctionApp"]] end 1 == docker build ==> 2 2 == Outputs ==> 3 3 == docker cp ./src/api ==> 4a 4a == Deploy ==o 5a 3 == docker cp ./src/func ==> 4b 4b == Deploy ==o 5b style g1 stroke:#000000, stroke-width:5px style 3 stroke-width:5px :::
AI Example¶
::: mermaid graph TB subgraph g1["Docker Pipeline - Example 3b"] 1[("Dockerfile")] 2["RUN dotnet restore *.sln\nRUN dotnet build api.csproj\nRUN dotnet build func.csproj"] 3{"Combined Build Files\nDocker Image"} 4a["Api Docker Image"] 4b["Func Docker Image"] 5a[["Azure App Service\nWebApp"]] 5b[["Azure App Service\nFunctionApp"]] end 1 == "docker build" ==> 2 2 == "Outputs" ==> 3 3 == "docker cp ./src/api" ==> 4a 4a == "Deploy" ==o 5a 3 == "docker cp ./src/func" ==> 4b 4b == "Deploy"==o 5b style g1 stroke:#000000, stroke-width:2px style 3 stroke-width:3px style 1,2,4a,4b,5a,5b fill:#EFEFEF, stroke:#000000, stroke-width:1px, color:#333333 :::
Example 4¶
::: mermaid graph LR; 1[dockerfile] ==>|docker build| 2[RUN npm run build... \nRUN dotnet build api.csproj...\n COPY dist/browser /app] ==>|Outputs| 3{API with UI\nDocker Image} ==>|Deploy| 4(((Azure App Service WebApp))) :::
Dockerfile Stages - Examples¶
::: mermaid graph LR; 1[dockerfile] ==>|docker build| 2[npm run build... \n or \n dotnet build... \n or etc...] ==>|Outputs| 3{Docker Image} ==>|Deploy| 4(((Azure App Service WebApp))) :::
dockerfile Syntax¶
In progress¶
Build without Cache
Command examples for engineering¶
These are mostly useful for DevOps or when needing to issue manual docker commands locally
## Free disk space. Prune unused/dangling local images (supersceded versions of images)
docker system prune -a -f
## Run dotnet app with tracing enabled (don't do on an Azure host, use a local/VM with docker installed)
docker run --env COREHOST_TRACE=1 image_name:tag_name
## or
docker run -it --entrypoint /bin/bash image_name:tag_name
/> COREHOST_TRACE=1 ./Cmg.Product.Api ## On the terminal inside the container, run the app with the env varable