ImageSharp with Docker (English)

ImageSharp with Docker

Comments

NOTE: Apart from English (and even then it's questionable, I'm Scottish). These are machine translated in languages I don't read. If they're terrible please contact me.
You can see how this translation was done in this article.

Thursday, 01 August 2024

//

1 minute read

ImageSharp is a great library for working with images in .NET. It's fast, easy to use, and has a lot of features. In this post, I'll show you how to use ImageSharp with Docker to create a simple image processing service.

What is ImageSharp?

ImageSharp enables me to seamlessly work with images in .NET. It's a cross-platform library that supports a wide range of image formats and provides a simple API for image processing. It's fast, efficient, and easy to use.

However there's an issue in my setup using docker and ImageSharp. When I try to load an image from a file, I get the following error: 'Access Denied to the path /wwroot/cache/ etc...' This is caused by Docker ASP.NET installations not allowing write access to the cache directory ImageSharp uses to store temporary files.

Solution

The solution is to mount a volume in the docker container that points to a directory on the host machine. This way, the ImageSharp library can write to the cache directory without any issues.

Here's how to do it:

mostlylucid:
image: scottgal/mostlylucid:latest
volumes:
- /mnt/imagecache:/app/wwwroot/cache

Here you see I map the /app/wwwroot/cache file to a local directory on my host machine. This way, ImageSharp can write to the cache directory without any issues.

On my Ubuntu machine I created a directory /mnt/imagecache and then ran the folowing command to make it writeable (by anyone, I know this is not secure but I'm no Linux guru :))

chmod  777 -p /mnt/imagecache

In my program.cs I have this line:

builder.Services.AddImageSharp().Configure<PhysicalFileSystemCacheOptions>(options => options.CacheFolder = "cache");

As the cacheroot defaults to wwwroot this will now write to the /mnt/imagecache directory on the host machine.

logo

©2024 Scott Galloway