# ImageSharp with Docker

<datetime class="hidden">2024-08-01T01:00</datetime>

<!--category-- Docker, ImageSharp -->

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:

```dockerfile
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 :))

```shell
chmod  777 -p /mnt/imagecache
```

In my program.cs I have this line:

```csharp
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.