Using Docker to run Manim in Jupyter notebooks

If you want to develop Manim animations easily the a Jupyter notebook makes as great working environment. However it can be fiddly to get set up.

Luckily there is a pre-built Docker image that can get you up and running with two simple commands:

cd <your working directory>
docker run --rm -it -p 8888:8888 -v "%cd%:/manim" manimcommunity/manim jupyter lab --ip=0.0.0.0

In the console you will then see a URL looking something like http://127.0.0.1:8888/?token=xxxxxxx. Cut and paste this into your browser and you are away laughing.

For Linux you will need to tweak the %cd% bit and use pwd instead so:

cd <your working directory>
docker run --rm -it -p 8888:8888 -v "$(pwd):/manim" manimcommunity/manim jupyter lab --ip=0.0.0.0

These commands mount the current directory into the docker container, so any notebooks or videos you create will be saved into that directory and won’t be lost when you stop the container.

Finally to create an animation put the following code into two separate cells in a new notebook:

from manim import *

and

%%manim SquareToCircle

class SquareToCircle(Scene):
    def construct(self):
        circle = Circle()  # create a circle
        circle.set_fill(PINK, opacity=0.5)  # set color and transparency

        square = Square()  # create a square
        square.rotate(PI / 4)  # rotate a certain amount

        self.play(Create(square))  # animate the creation of the square
        self.play(Transform(square, circle))  # interpolate the square into the circle
        self.play(FadeOut(square))  # fade out animation

When you run the notebook you will see the rendered animation (and it will be saved into a media sub-directory). The %%manim line is the magic instruction to trigger Manim to render the specified scene.

Using Docker to run Jupyter notebooks

If you are messing with data then, you can’t beat a Jupyter notebook as a working environment. Not only are they super easy to use but they are effective ways of sharing your workings and findings.

Even better, the Jupyter project have a created a bunch of Docker containers that contain all the tooling you need which, makes getting up and running as simple as running a two simple commands:

cd <your working directory>
docker run --rm -it -p 8888:8888 -v %cd%:/home/jovyan/work jupyter/datascience-notebook

In the console you will then see a URL looking something like http://127.0.0.1:8888/?token=xxxxxxx. Cut and paste this into your browser and you are away laughing.

For Linux you will need to tweak the %cd% bit and use pwd instead so:

cd <your working directory>
docker run --rm -it -p 8888:8888 -v $(pwd):/home/jovyan/work jupyter/datascience-notebook

If you need to sudo to install something you will need to run the container as root and set the GRANT_SUDO environment variable:

docker run --rm -it -p 8888:8888 -u root -e GRANT_SUDO="yes" -v %cd%:/home/jovyan/work jupyter/datascience-notebook

Lastly the jupyter/datascience-notebook container generally has all you need but if you are doing something specfic, there are a bunch of other options, including ones that contain R, Tensorflow, Scipy and Apache Spark support. See https://jupyter-docker-stacks.readthedocs.io/en/latest/using/selecting.html for more info