From 9cd67a22c4333199113fa8951c74d6b4b23195e5 Mon Sep 17 00:00:00 2001
From: Toastie <toastie@toastiet0ast.com>
Date: Thu, 13 Feb 2025 12:34:08 +1300
Subject: [PATCH] moved dockerfile to alpine, added some errors in music

---
 Dockerfile                                    | 30 ++++++++++++-------
 docker-entrypoint.sh                          |  3 +-
 src/EllieBot.Voice/LibSodium.cs               |  1 +
 .../Modules/Music/_common/Impl/VoiceProxy.cs  |  3 +-
 4 files changed, 24 insertions(+), 13 deletions(-)

diff --git a/Dockerfile b/Dockerfile
index 1a4e285..3f53c72 100644
--- a/Dockerfile
+++ b/Dockerfile
@@ -11,7 +11,7 @@ COPY src/EllieBot.Voice/*.csproj src/EllieBot.Voice/
 COPY src/EllieBot.GrpcApiBase/*.csproj src/EllieBot.GrpcApiBase/
 
 # Restore the dependencies for the EllieBot project
-RUN dotnet restore src/EllieBot/ -r linux-x64
+RUN dotnet restore src/EllieBot/ -r linux-musl-x64
 
 # Copy the rest of the source code
 COPY . .
@@ -20,30 +20,38 @@ COPY . .
 WORKDIR /source/src/EllieBot
 
 # Build and publish the EllieBot project, then clean up unnecessary files
-RUN dotnet publish -c Release -o /app --self-contained -r linux-x64 --no-restore; \
+RUN dotnet publish -c Release -o /app --self-contained -r linux-musl-x64 --no-restore; \
     mv /app/data /app/data_init; \
     chmod +x /app/EllieBot
 
 # Use the .NET 8.0 runtime as the base image for the final stage
-FROM debian:12.9-slim
+FROM alpine:3.20
 WORKDIR /app
 
+# Music dependencies
+# python3 required for yt-dlp, ffmpeg for conversion
+
 ADD --chmod=755 https://github.com/yt-dlp/yt-dlp/releases/latest/download/yt-dlp /usr/local/bin/yt-dlp
+RUN apk add --no-cache ffmpeg python3 libsodium
 
-# Create a new user, install dependencies, and set up sudoers file
-RUN apt update; \
-    apt install -y --no-install-recommends \
-        libicu-dev ca-certificates \
-        ffmpeg python3; \
-    apt autoremove -y; \
-    apt clean -y;
+# Required dependencies
+# libc6-compat is required for .NET 8.0
+# libstdc++ and libgcc are required for (.NET 8.0 ? or drawing?)
+# icu-libs is required for C# globalization
 
-RUN update-ca-certificates
+RUN apk update; \
+    apk add --no-cache libstdc++ libgcc icu-libs libc6-compat \
+    && rm -rf /var/cache/apk/*;
 
 # Copy the built application and the entrypoint script from the build stage
 COPY --from=build /app ./
 COPY docker-entrypoint.sh /usr/local/sbin/
 
+# TODO: is there a better way to link the libsodium.so?
+# TODO: Temporary linking, needs a proper solution
+RUN rm /app/data_init/lib/libsodium.so \
+    && ln -s /usr/lib/libsodium.so.26 /app/data_init/lib/libsodium.so
+
 # Define the data directory as a volume
 VOLUME [ "/app/data" ]
 
diff --git a/docker-entrypoint.sh b/docker-entrypoint.sh
index 385b868..20de617 100644
--- a/docker-entrypoint.sh
+++ b/docker-entrypoint.sh
@@ -6,7 +6,8 @@ data_init="/app/data_init"
 data="/app/data"
 
 # Merge data_init into data without overwrites.
-cp -R -n "$data_init/." "$data/"
+# cp -R -n "$data_init/." "$data/"
+cp -R "$data_init/." "$data/"
 
 echo "Yt-dlp update"
 # TODO: Update yt-dlp. It should not crash the entrypoint if ca-certificates is not installed
diff --git a/src/EllieBot.Voice/LibSodium.cs b/src/EllieBot.Voice/LibSodium.cs
index bbbc77d..897da5c 100644
--- a/src/EllieBot.Voice/LibSodium.cs
+++ b/src/EllieBot.Voice/LibSodium.cs
@@ -5,6 +5,7 @@ namespace EllieBot.Voice
 {
     internal static unsafe class Sodium
     {
+
         private const string SODIUM = "data/lib/libsodium";
 
         [DllImport(SODIUM, EntryPoint = "crypto_secretbox_easy", CallingConvention = CallingConvention.Cdecl)]
diff --git a/src/EllieBot/Modules/Music/_common/Impl/VoiceProxy.cs b/src/EllieBot/Modules/Music/_common/Impl/VoiceProxy.cs
index 08bb8b8..8b6dac2 100644
--- a/src/EllieBot/Modules/Music/_common/Impl/VoiceProxy.cs
+++ b/src/EllieBot/Modules/Music/_common/Impl/VoiceProxy.cs
@@ -41,8 +41,9 @@ public sealed class VoiceProxy : IVoiceProxy
             vc.SendPcmFrame(gw, data, 0, length);
             return true;
         }
-        catch (Exception)
+        catch (Exception ex)
         {
+            Log.Error(ex, "Couldn't send pcm frame: {Message}", ex.Message);
             return false;
         }
     }