Skip to content

Commit 6cdb9e4

Browse files
committedAug 14, 2024··
Optimize for size
1 parent fe0529f commit 6cdb9e4

File tree

6 files changed

+63
-35
lines changed

6 files changed

+63
-35
lines changed
 

‎.github/workflows/docker-publish.yml

+15-15
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
name: Docker
1+
name: Docker Build and Push
22

33
on:
44
push:
@@ -35,35 +35,35 @@ jobs:
3535
username: ${{ github.actor }}
3636
password: ${{ secrets.GITHUB_TOKEN }}
3737

38-
- name: Extract metadata (tags, labels) for Docker
38+
- name: Extract metadata for Docker
3939
id: meta
4040
uses: docker/metadata-action@v4
4141
with:
4242
images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}
43-
tags: |
44-
type=raw,value=latest,enable=${{ github.ref == 'refs/heads/master' }}
45-
type=sha,format=short
46-
type=ref,event=branch
47-
type=ref,event=tag
4843

4944
- name: Build and push Docker image
5045
uses: docker/build-push-action@v4
5146
with:
5247
context: .
48+
platforms: linux/amd64,linux/arm/v7,linux/arm64/v8
5349
push: ${{ github.event_name != 'pull_request' }}
54-
platforms: linux/amd64,linux/arm64,linux/arm/v7
5550
tags: ${{ steps.meta.outputs.tags }}
5651
labels: ${{ steps.meta.outputs.labels }}
57-
cache-from: type=gha
58-
cache-to: type=gha,mode=max
5952

60-
- name: Build and push architecture-specific tags
53+
- name: Create and push architecture-specific tags
6154
if: github.event_name != 'pull_request'
6255
run: |
63-
SHORT_SHA=$(echo ${{ github.sha }} | cut -c1-8 | tr '[:upper:]' '[:lower:]')
64-
REPO_LOWER=$(echo ${{ env.IMAGE_NAME }} | tr '[:upper:]' '[:lower:]')
65-
for arch in amd64 arm64 arm-v7; do
66-
docker buildx build --platform linux/${arch//-/\/} \
56+
REPO_LOWER=$(echo ${{ github.repository }} | tr '[:upper:]' '[:lower:]')
57+
SHORT_SHA=$(echo ${{ github.sha }} | cut -c1-8)
58+
59+
for arch in amd64 arm32v7 arm64v8; do
60+
case $arch in
61+
amd64) platform="linux/amd64" ;;
62+
arm32v7) platform="linux/arm/v7" ;;
63+
arm64v8) platform="linux/arm64/v8" ;;
64+
esac
65+
66+
docker buildx build --platform $platform \
6767
-t ${{ env.REGISTRY }}/${REPO_LOWER}:latest-${arch} \
6868
-t ${{ env.REGISTRY }}/${REPO_LOWER}:${SHORT_SHA}-${arch} \
6969
--push \

‎Dockerfile

+44-16
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,67 @@
11
# Specify the base image for the build stage
22
ARG DOTNET_VERSION=6.0
3-
FROM --platform=$BUILDPLATFORM mcr.microsoft.com/dotnet/sdk:${DOTNET_VERSION} AS build
3+
FROM --platform=$BUILDPLATFORM mcr.microsoft.com/dotnet/sdk:${DOTNET_VERSION}-alpine AS build
44
ARG TARGETARCH
55
ARG TARGETVARIANT
66
WORKDIR /app
77

8-
# Copy everything and restore as distinct layers
9-
COPY . ./
10-
RUN dotnet restore Tv7Playlist.sln
8+
# Copy csproj and restore as distinct layers
9+
COPY ["Tv7Playlist/Tv7Playlist.csproj", "Tv7Playlist/"]
10+
COPY ["Tv7Playlist.Core/Tv7Playlist.Core.csproj", "Tv7Playlist.Core/"]
11+
COPY ["Tv7Playlist.Data/Tv7Playlist.Data.csproj", "Tv7Playlist.Data/"]
12+
RUN dotnet restore "Tv7Playlist/Tv7Playlist.csproj"
1113

14+
# Copy everything else and build
15+
COPY . .
1216
# Build and publish
1317
RUN if [ "$TARGETARCH" = "amd64" ]; then \
14-
RID=linux-x64; \
18+
RID=linux-musl-x64; \
1519
elif [ "$TARGETARCH" = "arm64" ]; then \
16-
RID=linux-arm64; \
20+
RID=linux-musl-arm64; \
1721
elif [ "$TARGETARCH" = "arm" ] && [ "$TARGETVARIANT" = "v7" ]; then \
18-
RID=linux-arm; \
19-
elif [ "$TARGETARCH" = "386" ]; then \
20-
RID=linux-x86; \
22+
RID=linux-musl-arm; \
2123
else \
2224
echo "Unsupported architecture: $TARGETARCH$TARGETVARIANT"; \
2325
exit 1; \
2426
fi && \
25-
dotnet publish -c Release -o out --no-restore --self-contained false -r $RID Tv7Playlist.sln
27+
echo "Building for RID: $RID" && \
28+
dotnet publish Tv7Playlist/Tv7Playlist.csproj -c Release -o out --no-restore \
29+
--self-contained true \
30+
-r $RID \
31+
/p:PublishSingleFile=true \
32+
/p:PublishTrimmed=false \
33+
/p:IncludeNativeLibrariesForSelfExtract=true \
34+
/p:InvariantGlobalization=true \
35+
/p:UseSystemResourceKeys=true \
36+
/p:IlcGenerateStackTraceData=false \
37+
/p:IlcOptimizationPreference=Size \
38+
/p:IlcDisableReflection=false \
39+
/p:DebugType=None \
40+
/p:DebugSymbols=false \
41+
/p:StripSymbols=true \
42+
/p:EnableCompressionInSingleFile=true
2643

27-
# Build runtime image
28-
FROM mcr.microsoft.com/dotnet/aspnet:${DOTNET_VERSION}
44+
# Runtime stage
45+
FROM mcr.microsoft.com/dotnet/runtime-deps:${DOTNET_VERSION}-alpine
2946
WORKDIR /app
30-
RUN mkdir /data
31-
3247
COPY --from=build /app/out .
3348

49+
# Remove unnecessary files
50+
RUN find . -name '*.pdb' -type f -delete && \
51+
find . -name '*.xml' -type f -delete && \
52+
rm -rf *.dev.json
53+
54+
# Create a non-root user and set up permissions
55+
RUN adduser -u 5678 --disabled-password --gecos "" appuser && \
56+
mkdir -p /app/data && \
57+
chown -R appuser:appuser /app
58+
59+
USER appuser
60+
3461
ENV ASPNETCORE_URLS=http://+:80
62+
ENV DOTNET_SYSTEM_GLOBALIZATION_INVARIANT=1
3563
EXPOSE 80
3664

37-
VOLUME [ "/data" ]
65+
VOLUME [ "/app/data" ]
3866

39-
ENTRYPOINT ["dotnet", "Tv7Playlist.dll"]
67+
ENTRYPOINT ["./Tv7Playlist"]

‎Tv7Playlist.Core/Tv7Playlist.Core.csproj

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<Project Sdk="Microsoft.NET.Sdk">
22
<PropertyGroup>
33
<TargetFramework>net6.0</TargetFramework>
4-
<RuntimeIdentifiers>linux-x64;linux-arm64;linux-arm;linux-x86</RuntimeIdentifiers>
4+
<RuntimeIdentifiers>linux-musl-x64;linux-musl-arm64;linux-musl-arm</RuntimeIdentifiers>
55
</PropertyGroup>
66
<ItemGroup>
77
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="6.0.0" />

‎Tv7Playlist.Data/Tv7Playlist.Data.csproj

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
<PropertyGroup>
33
<LangVersion>latest</LangVersion>
44
<TargetFramework>net6.0</TargetFramework>
5-
<RuntimeIdentifiers>linux-x64;linux-arm64;linux-arm;linux-x86</RuntimeIdentifiers>
5+
<RuntimeIdentifiers>linux-musl-x64;linux-musl-arm64;linux-musl-arm</RuntimeIdentifiers>
66
</PropertyGroup>
77
<ItemGroup>
88
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="6.0.0" />

‎Tv7Playlist/Tv7Playlist.csproj

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
<TargetFramework>net6.0</TargetFramework>
44
<AspNetCoreHostingModel>InProcess</AspNetCoreHostingModel>
55
<LangVersion>latest</LangVersion>
6-
<RuntimeIdentifiers>linux-x64;linux-arm64;linux-arm;linux-x86</RuntimeIdentifiers>
6+
<RuntimeIdentifiers>linux-musl-x64;linux-musl-arm64;linux-musl-arm</RuntimeIdentifiers>
77
</PropertyGroup>
88
<ItemGroup>
99
<PackageReference Include="Microsoft.AspNetCore.Mvc.Razor.RuntimeCompilation" Version="6.0.0" />

‎Tv7Playlist/appsettings.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
},
99
"AllowedHosts": "*",
1010
"SourceType": "Xspf",
11-
"SqLiteConnectionString": "Data Source=/data/playlist.db",
11+
"SqLiteConnectionString": "Data Source=/app/data/playlist.db",
1212
"TV7Url": "https://api.init7.net/tvchannels.xspf",
1313
"UdpxyUrl": "http://192.168.15.2:4022/udp",
1414
"DownloadFileName": "PlaylistTV7udpxy.m3u"

0 commit comments

Comments
 (0)
Please sign in to comment.