主要内容

Access Files with Containers

MATLAB® applications and microservices running inside Docker® containers access data files by using mounted volumes that bridge the container's isolated file system with the host system's directories. Files in a Docker container are stored either:

  • In host folders mounted into the container as Docker volumes using the -v flag with docker run

  • Inside the container's internal file system (not persistent after container stops)

File Access Methods

Use CaseBest PracticeNotes
Persistent storageMount Docker volume with -vData remains after stop/restart
Temporary container storageUse /tmp or other non-mounted folderData lost after stop/restart
Access packaged filesUse which or absolute path in container imageFiles included during packaging are embedded in the CTF
File upload or download to microserviceUse API and mounted volume with -vSend and receive files over HTTP

Mount Folders for Persistent Storage

The file system of a Docker container is isolated from its host system by design, and a container's internal file system is not persistent after the container stops. To share data with a container, you mount folders in the host system to the container using the -v option when you execute the docker run command. Specify absolute paths for host paths and container paths or else the folders will not mount. Note that modifying files from within the container overwrites the files on the host.

Always ensure the mapped target folder has writable permission for the user running MATLAB code in the container. Containers generally allow writing to mapped volumes, but restricted Linux systems may only allow writing to directories such as /tmp.

If you write to a non-mounted folder, data persists only until the container is stopped.

Access Packaged Files from Deployed MATLAB Code

In your deployed MATLAB code, you can access files that were packaged with the code using the which function. For example, if you included the data file mydata.mat using the 'AdditionalFiles' option of compiler.build.standaloneApplication before packaging with compiler.package.docker, the following code obtains the location of the file.

if isdeployed
    locate_externapp = which('mydata.mat');
end

For more details, see Include and Access Files in Packaged Applications.

You can also access files using absolute paths that reference the mounted volume inside the container. For example:​

  • Reading files:

    data = fread(fopen('/container/path/myfile.mat', 'r'));

  • Writing files:

    imwrite(img, '/container/path/output.png');
    save('/container/path/newdata.mat', 'var');

Relative paths (i.e. writing to the current folder) may not work reliably and could cause permission errors, especially if the container's working directory is not mapped or does not have write permissions.​

If files need to persist or be accessed externally, mount folders with the -v option.

Upload or Download Files to Microservice

Microservice MATLAB applications with HTTP endpoints allow clients to upload or download files using API requests. The service can save uploaded files to a mounted folder using absolute paths or read files from a mounted folder for download. This workflow lets files persist outside the container and be shared across restarts.

See Also

Topics