Search code examples
dockerubuntuopenmodelica

How could I Install openmodelica in my docker image?


I have set up a docker image and install ubuntu on it. Can you please tell me how can I install Openmodelica inside ubuntu to that docker image?

for example, if I want to install node.js on this docker image I could use this code:

apt install nodejs

so I need some codes like that to install open Modelica on my docker image. p.s: my docker image is an ubuntu image.


Solution

  • I happened to create a Docker image for OpenModelica to debug something, so I might add it here as well. We got this questions in the OpenModelica forum as well. While the answer of @sjoelund.se will stay up to date this one is a bit more explaining.

    Dockerfile

    FROM ubuntu:18.04
    
    # Export DISPLAY, so a XServer can display OMEdit
    ARG DEBIAN_FRONTEND=noninteractive
    ENV DISPLAY=host.docker.internal:0.0
    
    # Install wget, gnupg, lsb-release
    RUN apt-get update \
        && apt install -y wget gnupg lsb-release
    
    # Get the OpenModelica stable version
    RUN for deb in deb deb-src; do echo "$deb http://build.openmodelica.org/apt `lsb_release -cs` stable"; done | tee /etc/apt/sources.list.d/openmodelica.list
    RUN wget -q http://build.openmodelica.org/apt/openmodelica.asc -O- | apt-key add -
    
    # Install OpenModelica
    RUN apt-get update \
        && apt install -y openmodelica
    
    # Install OpenModelica libraries (like all of them)
    RUN for PKG in `apt-cache search "omlib-.*" | cut -d" " -f1`; do apt-get install -y "$PKG"; done
    
    # Add non-root user for security
    RUN useradd -m -s /bin/bash openmodelicausers
    USER openmodelicausers
    ENV HOME /home/openmodelicausers
    ENV USER openmodelicausers
    WORKDIR $HOME
    
    # Return omc version
    CMD ["omc", "--version"]
    

    Let's build and tag it:

    docker build --tag openmodelica:ubuntubionic .
    

    How to use omc from the docker image

    Let's create a small helloWorld.mo Modelica model:

    model helloWorld
      Real x(start=1.0, fixed=true);
    equations
      der(x) = 2.5*x;
    end helloWorld;
    

    and a MOS script which will simulate it, called runHelloWorld.mos

    loadFile("helloWorld.mo"); getErrorString();
    simulate(helloWorld); getErrorString();
    

    Now we can make our files accessible to the docker container with the -v flag and run our small example with:

    docker run \
      --rm \
      -v $(pwd):/home/openmodelicausers \
      openmodelica:ubuntubionic \
      omc runHelloWorld.mos
    

    Note that -v needs an absolute path. I added --rm to clean up.

    Using OMEdit with a GUI

    I'm using Windows + Docker with WSL2. So in order to get OMEdit running I need to have a XServer installed on my Windows host system. They are not trivial to set up, but I'm using VcXsrv and so far it is working for me. On Linux this is of course much simpler. I'm using this config to start XLaunch:

    <?xml version="1.0" encoding="UTF-8"?>
    <XLaunch WindowMode="MultiWindow" ClientMode="NoClient" LocalClient="False" Display="-1" LocalProgram="xcalc" RemoteProgram="xterm" RemotePassword="" PrivateKey="" RemoteHost="" RemoteUser="" XDMCPHost="" XDMCPBroadcast="False" XDMCPIndirect="False" Clipboard="True" ClipboardPrimary="True" ExtraParams="" Wgl="True" DisableAC="True" XDMCPTerminate="False"/>
    

    But when the XServer is running you can use OMEdit in nearly the same fashion you would from a Linux OS, just mount some directory with your files and that's it:

    docker run \
      --rm \
      -v $(pwd):/home/openmodelicausers \
      openmodelica:ubuntubionic \
      OMEdit