Skip to content
PotatoScript edited this page Apr 16, 2025 · 8 revisions

Docker Tutorial Menu

Title Remark/Code
Setup Setup Docker on your PC
Basic Commands Learn essential Docker commands
Building Images Guide to creating and managing Docker images
Running Containers Learn how to run and manage containers
Dockerfile Guide Step-by-step guide to writing a Dockerfile
Docker Compose How to manage multi-container applications
Volumes Using Docker volumes for persistent data storage
Networking Guide to setting up Docker container networks
Pushing Images to Docker Hub Share your Docker images on Docker Hub
Advanced Features Multi-stage builds and optimization techniques

πŸ”΄ Windows 10/11 Home ➜ ❌ Does not support Windows Containers

πŸ‘‰ If you’re on Home edition, you can only use Linux containers, so WPF apps (which are Windows-only) cannot be containerized with your current OS. Docker Desktop disables that feature on Home Edition.

βœ… Step-by-Step: Convert WPF App to Console App

🧱 Assumption

You already have:

ConvertImageToBase64.sln
β”œβ”€β”€ ConvertImageToBase64.Lib     βœ… your reusable logic
β”œβ”€β”€ ConvertImageToBase64.App     ❌ WPF app β†’ now we’ll replace with Console app

βœ… Step 1: Create a Console Project

From the root:

dotnet new console -n ConvertImageToBase64.Console

You now have:

ConvertImageToBase64.Console/
  └── Program.cs

βœ… Step 2: Add the Console App to Your Solution

dotnet sln add ConvertImageToBase64.Console/ConvertImageToBase64.Console.csproj

βœ… Step 3: Add Reference to Your Library

dotnet add ConvertImageToBase64.Console/ConvertImageToBase64.Console.csproj reference ConvertImageToBase64.Lib/ConvertImageToBase64.Lib.csproj

βœ… Step 4: Move Your Image Conversion Logic to the Library

If it’s not already done, move logic like:

public static class ImageConverter
{
    public static string ConvertToBase64(string imagePath)
    {
        byte[] bytes = File.ReadAllBytes(imagePath);
        return Convert.ToBase64String(bytes);
    }
}

into ConvertImageToBase64.Lib β€” for example in ImageConverter.cs.


βœ… Step 5: Modify Program.cs in Console App

Here's a simple usage:

using System;
using ConvertImageToBase64.Lib;

class Program
{
    static void Main(string[] args)
    {
        Console.WriteLine("Enter the path of the image:");
        string path = Console.ReadLine();

        if (File.Exists(path))
        {
            string base64 = ImageConverter.ConvertToBase64(path);
            Console.WriteLine("Base64:");
            Console.WriteLine(base64);
        }
        else
        {
            Console.WriteLine("File not found.");
        }
    }
}

βœ… Step 6: Run and Test

dotnet run --project ConvertImageToBase64.Console

βœ… Step 7 (Optional): Remove the Old WPF Project

If you no longer need it:

dotnet sln remove ConvertImageToBase64.App/ConvertImageToBase64.App.csproj

Then delete the folder manually.


🐳 Step-by-Step: Dockerize Your .NET Console App

βœ… Step 1: Project Structure

Assume this folder layout:

ConvertImageToBase64/
β”œβ”€β”€ ConvertImageToBase64.Console/      ← Console app
β”‚   └── ConvertImageToBase64.Console.csproj
β”œβ”€β”€ ConvertImageToBase64.Lib/          ← Class Library
β”‚   └── ConvertImageToBase64.Lib.csproj
β”œβ”€β”€ ConvertImageToBase64.sln
└── Dockerfile                         ← We will create this now

βœ… Step 2: Create a Dockerfile in the root folder

Paste this into a file named Dockerfile:

# Stage 1: Build
FROM mcr.microsoft.com/dotnet/sdk:6.0 AS build
WORKDIR /app

# Copy solution and projects
COPY *.sln .
COPY ConvertImageToBase64.Console/*.csproj ./ConvertImageToBase64.Console/
COPY ConvertImageToBase64.Lib/*.csproj ./ConvertImageToBase64.Lib/

# Restore dependencies
RUN dotnet restore

# Copy all source code
COPY ConvertImageToBase64.Console/. ./ConvertImageToBase64.Console/
COPY ConvertImageToBase64.Lib/. ./ConvertImageToBase64.Lib/

# Build the app
RUN dotnet publish ConvertImageToBase64.Console -c Release -o out

# Stage 2: Runtime
FROM mcr.microsoft.com/dotnet/runtime:6.0
WORKDIR /app
COPY --from=build /app/out ./

ENTRYPOINT ["dotnet", "ConvertImageToBase64.Console.dll"]

βœ… Step 3: Build the Docker image

In your terminal at the root of the solution:

docker build -t convertimagebase64 .

βœ… Step 4: Run the Docker container

To run it and interact:

docker run -it --rm convertimagebase64

It'll prompt you to enter the path to an image. If you're working with local files, you'll need to mount a volume.


βš™οΈ Optional: Mount a local folder with images

If you want your container to access images from your host machine:

docker run -it --rm -v "C:\Users\you\Pictures:/images" convertimagebase64

Then inside the app, enter something like:

/images/my-image.png

βœ… Fix it: Manually mount your G: drive

In WSL, try this:

sudo mkdir /mnt/g
sudo mount -t drvfs G: /mnt/g

πŸ’‘ This uses drvfs β€” the Windows-to-WSL filesystem bridge.

If that works, then try:

ls /mnt/g

You should see your GitHub folders πŸŽ‰


🐳 Then, rerun Docker

Now your Docker volume mount will work:

docker run -it --rm -v "/mnt/g/GitHub/ConvertImageToBase64/ConvertImageToBase64.Tests/Images:/data" convertimagebase64

When prompted:

Enter the path of the image:
/data/image.png

πŸ›  Optional: Make G: auto-mount in the future

To avoid doing this manually every time:

  1. Run:

    sudo nano /etc/wsl.conf
  2. Add this:

    [automount]
    enabled = true
    mountFsTab = false
  3. Save, then restart WSL:

    wsl --shutdown

😎 Uploading your container image to Docker Hub

πŸ”§ Step 1: Tag your Docker image

Assuming your image is named convertimagebase64, you need to tag it with your Docker Hub username.

docker tag convertimagebase64 yourdockerhubusername/convertimagebase64:latest

Example (if your Docker Hub username is lucyberry):

docker tag convertimagebase64 lucyberry/convertimagebase64:latest

πŸ” Step 2: Login to Docker Hub

docker login

Enter your Docker Hub username and password (or personal access token if 2FA is enabled).


πŸ“€ Step 3: Push the image

docker push yourdockerhubusername/convertimagebase64:latest

It will upload the image layers to your Docker Hub repo.


βœ… Step 4: Done!

You can now use it anywhere with:

docker run yourdockerhubusername/convertimagebase64

πŸ’‘ Optional: Create a repo on Docker Hub

  • Go to https://hub.docker.com
  • Click on Repositories > Create Repository
  • Name it convertimagebase64
  • Leave visibility as public or private
  • Then push the image as shown above

Clone this wiki locally