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.
Custom base layer (your chosen OS)
Secondary layer (MATLAB® Runtime)
Application layer (your standalone application)
Prerequisites
Verify that you have MATLAB Compiler SDK™ installed on the development machine.
Verify that you have Docker installed and configured on the development machine by typing
[~,msg] = system("docker version")in a MATLAB command window.Note
If you are using WSL, use
[~,msg] = system("wsl docker version")instead.If you do not have Docker installed, follow the instructions on the Docker website to install and set up Docker.
docs.docker.com/engine/install/To build microservice images on Windows®, you must install either Docker Desktop or Docker on Windows Subsystem for Linux v2 (WSL2).
To install Docker Desktop, see
docs.docker.com/desktop/setup/install/windows-install/.To install Docker on WSL2, see
https://www.mathworks.com/matlabcentral/answers/1758410-how-do-i-install-docker-on-wsl2.
The computer you are using must be connected to the Internet.
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.confYou can also install additional dependencies to fulfill any requirements for your deployment.
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
compiler.build.standaloneApplication | compiler.runtime.createDockerImage (MATLAB Compiler SDK) | compiler.package.docker
