Elevating Your Workflow: Dockerizing Mojo for Seamless Development

Siva
3 min readMay 16, 2024

--

Introduction:

In the dynamic landscape of data science and machine learning, efficiency and flexibility are paramount. Tools that streamline development workflows not only save time but also enhance collaboration and reproducibility. Mojo, a Python library designed for building modular, composable, and reusable machine learning pipelines, is one such tool that empowers data scientists and machine learning engineers to unleash their creativity without being bogged down by infrastructure complexities.

In this comprehensive guide, we’ll walk through the process of Dockerizing Mojo, enabling you to harness its power within a Docker container. Docker provides a lightweight and portable environment, ensuring consistency across different platforms and simplifying deployment. By encapsulating Mojo within a Docker container, you can easily develop, test, and deploy machine learning pipelines in diverse environments, from your local machine to cloud-based servers.

Step 1: Cloning the Mojo Repository

Let’s kickstart our journey by cloning the Mojo repository from GitHub. Open your terminal and run the following command:

git clone https://github.com/modularml/mojo/

This command will clone the Mojo repository into a directory named mojo within your current working directory.

Step 2: Navigating to the Docker Example

Navigate into the docker directory within the Mojo repository:

cd mojo/examples/docker

Here, you’ll find the necessary files for Dockerizing Mojo.

Step 3: Exploring the Contents

Take a moment to explore the contents of the docker directory:

ls

build-image.sh docker-compose.yml Dockerfile.mojosdk

You’ll see several files, including build-image.sh, docker-compose.yml, and Dockerfile.mojosdk.

Step 4: Making the Build Script Executable

Before we proceed, let’s ensure that the build script is executable:

chmod +x build-image.sh

This command grants execution permissions to the build-image.sh script, allowing us to run it in the next step.

Step 5: Building the Docker Image

Now, let’s build the Docker image using the provided script. We’ll generate a random UUID to serve as the authentication key:

# Generating a Random UUID
RANDOM_UUID=$(uuidgen)

# Building the Docker Image
./build-image.sh --auth-key $RANDOM_UUID

This script initiates the Docker image build process, pulling in all necessary dependencies and configurations.

Step 6: Verifying the Image Build

Once the build process completes, verify that the Docker image was created successfully by listing the available images:

docker images | head

REPOSITORY TAG IMAGE ID CREATED SIZE
modular/mojo-v0.3-20241205-9358 latest 8a6e8c71d2fa 29 minutes ago 2.84GB

You should see an entry for modular/mojo-v0.3-<timestamp> among the listed images.

Step 7: Running Mojo in a Docker Container

Now, let’s spin up a Docker container and access Mojo within it:

docker run -it modular/mojo-v0.3-<timestamp> bash

(base) root@8113h2165c12:/# mojo
Welcome to Mojo! 🔥

Expressions are delimited by a blank line.
Type `:quit` to exit the REPL and `:mojo help` for further assistance.

This command launches a new Docker container based on the Mojo image, providing us with an interactive shell session.

Step 8: Testing Mojo

Once inside the Docker container, let’s test Mojo by running a simple Mojo script. Navigate to a directory where you’d like to work:

cd home/ && mkdir ex && cd ex

Create a sample Mojo script named hello.mojo and add the following content:

def main():
# CHECK: Hello Mojo 🔥!
print("Hello Mojo 🔥!")
for x in range(9, 0, -3):
print(x)

Save the file and exit the text editor.

Step 9: Running the Mojo Script

Execute the Mojo script using the mojo command:

mojo hello.mojo

You should see the following output:

Hello Mojo 🔥!
9
6
3

Conclusion

Congratulations! You’ve successfully Dockerized Mojo and run a Mojo script within a Docker container.

With Mojo Dockerized, you can now develop, test, and deploy your machine learning pipelines with ease, ensuring consistency across different environments. Whether you’re working on your local machine or scaling up in the cloud, Mojo inside Docker has got you covered.

Unleash the full potential of Mojo and Docker, and elevate your machine learning workflow to new heights. Happy modeling! 🚀

Reference:

  1. https://github.com/modularml/mojo
  2. https://docs.modular.com/mojo
  3. https://github.com/modularml/mojo/tree/main/examples/docker
  4. https://github.com/modularml/mojo/blob/main/examples/hello.🔥
  5. https://github.com/modularml/mojo/issues/2265#issuecomment-2114744134

--

--