主要内容

Create Docker Image Using Custom Base Layer

Supported Platforms: Linux®

This example shows how to create a Docker® image using a customized base layer. Creating a custom base layer allows you to use your preferred version of Linux or other operating systems as the foundation and run commands or install additional dependencies.

Docker container images are composed of layers. In this example, you create three layers, each containing a set of file-system changes that build on top of the previous layers.

  1. Custom base layer (your chosen OS)

  2. Secondary layer (MATLAB® Runtime)

  3. Application layer (your standalone application)

Prerequisites

Note

Some deployable archives, such as Simulink® Compiler™ artifacts, are not cross-platform compatible and must be built on Linux to use with Docker. For more information, see Limitations for MATLAB Compiler (MATLAB Compiler SDK).

Create Standalone Application Using compiler.build.standaloneApplication

In MATLAB, locate the MATLAB function that you want to deploy as a microservice. For this example, create the function mirror.m using the following code.

function out = mirror(in)
out = in;

Create a standalone application using compiler.build.standaloneApplication.

buildResults = compiler.build.standaloneApplication("mirror.m","Verbose",true);

Build Custom Base Layer Using Docker

You can customize the base layer using a Dockerfile.

For this example, create a new file named Dockerfile.customdeps. The file should specify the Linux distribution and install all of the dependencies required for MATLAB Runtime.

You can add commands to the Dockerfile to customize the behavior of the base layer. For example, add proxy information to the apt configuration file.

RUN echo 'Acquire::http::Proxy "http://yourproxyaddress:proxyport";' >> /etc/apt/apt.conf

You can also install additional dependencies to fulfill any requirements for your deployment.

  Example Dockerfile.customdeps

Packages are sourced from https://github.com/mathworks-ref-arch/container-images/tree/main/matlab-runtime-deps/ for Ubuntu 24.04. If your internal OS image is derived from any other Linux distribution, you must recreate the list of dependencies for that OS.

At the MATLAB command window, build the custom base layer image using Docker and the Dockerfile you created.

depsImageName = "mycompanybase:r2024b";
system("docker build -f Dockerfile.customdeps -t " + depsImageName + " .");

Note

If you are using WSL, type wsl docker build instead.

Create Secondary Layer with MATLAB Runtime

Create a MATLAB Runtime Docker image layer that uses the custom base layer you created. Use name-value arguments to specify the base layer image and the MATLAB Runtime image name.

runtimeImageName = "custom-matlabruntime:r2024b";
compiler.runtime.createDockerImage(buildResults, ...
    "BaseImage",depsImageName, ...
    "ImageName",runtimeImageName)

Create Application Layer by Packaging Standalone Application

Create a Docker image for the standalone application. Specify the name of the MATLAB Runtime Docker image you created as an input to the compiler.package.docker function.

compiler.package.docker(buildResults, ...
    "ImageName","mirror-micro","RuntimeImage",runtimeImageName);

Deploy the image using Docker. For details about pushing your image to Docker Hub or your private registry, consult the Docker documentation.

See Also

| (MATLAB Compiler SDK) |

Topics