Get files while Docker

Viewed 2763

I have a simple ASP.NET Core application and it access files from another project. Here are my code:

var currentDirectory = Directory.GetCurrentDirectory();
            string directory = Path.GetFullPath(Path.Combine(currentDirectory, @"..\Data\SqlScripts"));
string[] fileArray = Directory.GetFiles(directory, "*.sql");
eturn fileArray.SingleOrDefault(f => f.Contains(filename));

If I run the application in Docker for Linux I get an exceptions saying the files are not found. When I debug the currentDirectory is set to /app instead of the actually physical directory.

How can I get the files also while running in Docker?

1 Answers

How can I get the files also while running in Docker?

Much of the value that docker provides is realized by creating an isolated environment in the container. This provides all the benefits that make container appealing (network isolation, process isolation, etc) but it does mean the container is completely isolated in general (there are exceptions, like volume mounts). So trying to modify a container so it can access things outside the container can be a good idea (for example database) or a fairly bad idea (local file access).

What do your files contain? Are they just static data for your application? If so, I would suggest putting all the files inside your container too. If you can do that, then your container actually becomes an isolated application that can run not just on your computer but any computer.

If the files are actually conceptually separate from the application, then they need to be handled some other way. For example a database needs to be a separate container. If the application expects to talk to a file server, then you should probably use a volume mount.

Think about how your container will be deployed. Will it be a standalone thing? What are its dependencies when you deploy?

Related