Skip to content

Matlab on Docker and EC2

Ben Heasly edited this page Jul 17, 2015 · 6 revisions

Here is an account of how I installed Matlab inside a Docker container running in an Amazon EC2 instance.

If you wish to do the same, please make your own arrangements with Mathworks and licensing.

The process was not as linear as it looks here. But I think the steps below are pretty accurate.

Launch an EC2 instance.

EC2 Dashboard, Brainard lab account

I used:

  • ubuntu
  • t2.micro
  • 100GB storage
  • allow all TCP, but only from my workstation IP
  • Brainard Lab "render-toolbox" keypair

The instance IP happened to be 54.68.1.92, which appears below.

ssh to EC2 instance and install Docker with support for large containers

terminal A

ssh -i render-tooblox.pem ubuntu@54.68.1.92

sudo apt-get update
sudo apt-get -y install linux-image-extra-$(uname -r)
sudo sh -c "wget -qO- https://get.docker.io/gpg | apt-key add -"
sudo sh -c "echo deb http://get.docker.io/ubuntu docker main\ > /etc/apt/sources.list.d/docker.list"
sudo apt-get update
sudo apt-get -y install lxc-docker
sudo docker info

You should see Storage Driver: aufs.

Use a Docker image with handy ssh stuff set up

Thanks to Roberto Hashioka

terminal A
sudo docker build -t ninjaben/docker-desktop git://github.com/rogaha/docker-desktop.git
sudo docker images

You should see ninjaben/docker-desktop

CONTAINER_ID=$(sudo docker run --mac-address=e4:ce:8f:60:8f:0a -d -P ninjaben/docker-desktop)
echo $CONTAINER_ID

It was d38c012e46045cd3ff8b05033e2362a842c0f94b94e247e55760618a3e1989a1.

echo $(sudo docker logs $CONTAINER_ID | sed -n 1p)

It was oeShoh2eis6j.

sudo docker port $CONTAINER_ID 22

It was 32768.

Push the Matlab installer and your license file directly to the Docker container

First you should download the installer and license file to your workstation.

I have a license for host id e4:ce:8f:60:8f:0a, which appears below.

terminal B
scp -P 32768 ~/Downloads/matlab_R2015a_glnxa64.zip docker@54.68.1.92:/home/docker/matlab_R2015a_glnxa64.zip
scp -P 32768 ~/Downloads/license.lic docker@54.68.1.92:/home/docker/license.lic

ssh directly to the docker container and verify X forwarding and free disk space

terminal B
ssh -Y docker@54.68.1.92 -p 32768
sudo apt-get install gedit
gedit

Should get a gedit window on your workstation (ignore warnings, wait several seconds)

sudo df -h

Should show about 77GB free space on rootfs.

Run the Matlab installer!

I did the interactive install. I think a "silent" install would also be possible but I didn't work that out.

terminal B
cd /home/docker
unzip matlab_R2015a_glnxa64.zip -d /home/docker/matlab-installer
cd matlab-installer
sudo ./install

I used:

  • 'dhb' account
  • designated computer license
  • default install folder
  • all products
  • yes, symbolic links
  • let 'er rip!

Upon completion:

  • Don't activate yet (for some reason an option we need will be missing).
  • sudo matlab
  • Should prompt for activation (with additional options).
  • Activate manually with /home/docker/license.lic.
  • Try playing Matlab.
  • 'matlab -nodesktop -nosplash -r "eye(42),quit"'

Commit a Docker image

So now Matlab is installed in a Docker container. Commit a Docker image so we can reuse this. Also push the image to Docker Hub so we can reuse this from anywhere. Use a private Docker Hub repo because Matlab is non-free.

terminal A

sudo docker commit $CONTAINER_ID ninjaben/matlab:activated
sudo docker push ninjaben/matlab:activated

After several minutes, the docker commit worked for me.

After many minutes, the docker push seemed to finish uploading. But then it failed at the last minute with an error much like this one. Alas!

The image is large, about 12GB, and most of that is in one Docker "layer". I think this contributes to the error.

Save what worked as an Amazon Machine Image

Even if the docker push fails, we still have a useful Docker image stored locally in our EC2 instance. We can save this progress in an AMI.

EC2 dashboard

Save an AMI of this EC2 instance for reuse. Then terminate this EC2 instance to avoid ongoing billing.

Later, we should be able to launch a new instance based on this AMI. Then we can ssh in and launch the Docker container with:

sudo docker run -t -i ninjaben/matlab:activated "/bin/bash"