Skip to content

Commit

Permalink
sample project showing using a custom base image
Browse files Browse the repository at this point in the history
  • Loading branch information
baronfel committed May 8, 2024
1 parent 69329b5 commit 48691bf
Show file tree
Hide file tree
Showing 9 changed files with 125 additions and 6 deletions.
54 changes: 54 additions & 0 deletions .github/workflows/custom-base-image.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
name: Custom Base Image Example

on:
push:
workflow_dispatch:

env:
GITHUB_USERNAME: baronfel
BASE_IMAGE: "ghcr.io/baronfel/dotnet-runtime-skia-base-image:8.0"

jobs:
create-base-image:
permissions:
contents: read
packages: write
runs-on: ubuntu-latest
steps:
- name: Login to ghcr.io
uses: docker/login-action@v3
with:
registry: ghcr.io
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}

- name: Get the code
uses: actions/checkout@v2

- name: Build the base image
working-directory: src/custom-base-image
run: |
docker build -t ${{ env.BASE_IMAGE }} -f Dockerfile .
docker push ${{ env.BASE_IMAGE }}
use-base-image:
needs: create-base-image
permissions:
contents: read
packages: write

runs-on: ubuntu-latest
steps:
- name: Login to ghcr.io
uses: docker/login-action@v3
with:
registry: ghcr.io
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}

- name: Get .NET
uses: actions/setup-dotnet@v4

- name: Build the app
run: |
dotnet publish -t:PublishContainer -p ContainerBaseImage="${{ env.BASE_IMAGE }}"
6 changes: 0 additions & 6 deletions global.json

This file was deleted.

Binary file removed pkg/Microsoft.NET.Build.Containers.8.0.100-dev.nupkg
Binary file not shown.
7 changes: 7 additions & 0 deletions sdk-container-demo.sln
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "multi-arch-sample", "src\mu
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "msbuild-versioning-sample", "src\msbuild-versioning-sample\msbuild-versioning-sample.csproj", "{70CED231-4FA2-4F47-8069-5C091C2895E9}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "custom-base-image", "src\custom-base-image\custom-base-image.csproj", "{F3CB2379-3249-460B-911A-70BEF33840FD}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand All @@ -38,11 +40,16 @@ Global
{70CED231-4FA2-4F47-8069-5C091C2895E9}.Debug|Any CPU.Build.0 = Debug|Any CPU
{70CED231-4FA2-4F47-8069-5C091C2895E9}.Release|Any CPU.ActiveCfg = Release|Any CPU
{70CED231-4FA2-4F47-8069-5C091C2895E9}.Release|Any CPU.Build.0 = Release|Any CPU
{F3CB2379-3249-460B-911A-70BEF33840FD}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{F3CB2379-3249-460B-911A-70BEF33840FD}.Debug|Any CPU.Build.0 = Debug|Any CPU
{F3CB2379-3249-460B-911A-70BEF33840FD}.Release|Any CPU.ActiveCfg = Release|Any CPU
{F3CB2379-3249-460B-911A-70BEF33840FD}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(NestedProjects) = preSolution
{2D5E802C-0E59-4027-B4AD-B6BB9B2DDCA9} = {0F9FBE5F-DFCF-4AD3-B33E-62F676243120}
{D8C6E9FD-F698-42D1-B1D7-7CCECF45A66D} = {0F9FBE5F-DFCF-4AD3-B33E-62F676243120}
{618B480D-55B9-4550-BE86-8B83D5298C9F} = {0F9FBE5F-DFCF-4AD3-B33E-62F676243120}
{70CED231-4FA2-4F47-8069-5C091C2895E9} = {0F9FBE5F-DFCF-4AD3-B33E-62F676243120}
{F3CB2379-3249-460B-911A-70BEF33840FD} = {0F9FBE5F-DFCF-4AD3-B33E-62F676243120}
EndGlobalSection
EndGlobal
6 changes: 6 additions & 0 deletions src/custom-base-image/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
FROM mcr.microsoft.com/dotnet/runtime:8.0 as base

RUN apt-get update && apt-get install -y \
libfreetype6 \
libfontconfig1 \
&& rm -rf /var/lib/apt/lists/*
2 changes: 2 additions & 0 deletions src/custom-base-image/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
// See https://aka.ms/new-console-template for more information
Console.WriteLine("Hello, World!");
18 changes: 18 additions & 0 deletions src/custom-base-image/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Using a custom base image container

This sample demonstrates how to take control of the base image used by the SDK tooling to build your application.

It includes

* A Dockerfile with some customizations (in this case, installing a package into the runtime environment)
* A Console app that needs that package in order to execute

To build and run the sample, follow these steps:
* build the base image and push it to a registry
* `docker build -f Dockerfile -t <my-registry>/<my-image-name>:<my-tag> .`
* `docker push <my-registry>/<my-image-name>:<my-tag>`
* publish the console application as a container, specifying your base image
* `dotnet publish -t:PublishContainer -p ContainerBaseImage=<my-registry>/<my-image-name>:<my-tag>`

> [!IMPORTANT]
> Pushing images to a registry may require authentication, both on the push and pull side. Make sure you have the necessary credentials to access the registry you are using.
13 changes: 13 additions & 0 deletions src/custom-base-image/custom-base-image.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net8.0</TargetFramework>
<RuntimeIdentifier>linux-x64</RuntimeIdentifier>
<RootNamespace>custom_base_image</RootNamespace>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<EnableSdkContainerSupport>true</EnableSdkContainerSupport>
</PropertyGroup>

</Project>
25 changes: 25 additions & 0 deletions src/custom-base-image/custom-base-image.sln
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 17
VisualStudioVersion = 17.5.002.0
MinimumVisualStudioVersion = 10.0.40219.1
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "custom-base-image", "custom-base-image.csproj", "{572DAF66-27C5-4736-AD1A-790BEB45F45F}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{572DAF66-27C5-4736-AD1A-790BEB45F45F}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{572DAF66-27C5-4736-AD1A-790BEB45F45F}.Debug|Any CPU.Build.0 = Debug|Any CPU
{572DAF66-27C5-4736-AD1A-790BEB45F45F}.Release|Any CPU.ActiveCfg = Release|Any CPU
{572DAF66-27C5-4736-AD1A-790BEB45F45F}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {192A0D2A-7A6B-40E7-BA67-2957C25F8160}
EndGlobalSection
EndGlobal

0 comments on commit 48691bf

Please sign in to comment.