Docker environment

Let’s discuss the custom environment options that Educative provides to its authors. Following the previous lesson, when we scroll down a little, we’ll get a block named DOCKER CONTAINER as annotated below:

Docker !!!

Yes, we use Docker to allow our authors to create their custom environments to accommodate a diverse range of programming languages and frameworks on our platform.

If the word Docker sounds new to you, consider going through the following tutorial:

What is Docker and how it works?

Authors can create custom Docker images and allow end-users to access these customized environments through our platform directly. In comparison, Docker environment on our platform is slightly different from running it locally. Let’s create a custom environment of our needs on Educative’s platform.

Creating the Dockerfile

The first step in setting up a Docker environment is creating a Dockerfile. This file specifies the base operating system (aka OS), the application(s), and the framework(s) that we want in our environment.

Remember that the Dockerfile has no extension, and it can be created using any available text editor.

For instance, let’s create a custom environment that supports Python 3.9 with required machine learning libraries as follows:

FROM ubuntu:20.04
RUN apt-get update && apt-get install software-properties-common -y \
&& add-apt-repository ppa:deadsnakes/ppa && apt-get update \
&& apt-get install python3.9 python3-pip -y \
&& update-alternatives --install /usr/bin/python python /usr/bin/python3.9 1 \
&& python -m pip install -U pip numpy pandas matplotlib seaborn sklearn \
&& rm -rf /var/lib/apt/lists/* && apt-get autoclean -y
Custom environment of Python 3.9 with required machine learning libraries

FROM

The FROM keyword is used to specify the base image. The base image defines the OS of our Docker environment.

In our Dockerfile, we used FROM ubuntu:20.04. This is the Ubuntu 20.04 LTS base image.

Other standard base images include:

  • python:3.10.1-alpine3.14 - An Alpine Linux environment with Python 3.10.1 installed on it.
  • node:14.18 - A Linux environment with Node 14.18 installed on it.

We strongly recommend and support Ubuntu-based images on our platform. Therefore;

Line 1: FROM ubuntu:20.04 suggests that our container is running Linux, we can use Linux-based terminal commands.

RUN

The RUN keyword specifies the commands we want to run inside the container. These commands are executed right after our container is instantiated. It’s just like the terminal we have on our Unix/Linux-based operating systems.

For instance, the above-given code snippet installs and updates the required packages.

We can add multiple RUN commands in our Dockerfile. But every additional RUN command will increase the size of a Docker image, which will take longer to build. Hence, a good practice is to use a single RUN as we did in our example and chain all commands using &&. At the same time, the \ in the above-given example indicates that the command is being continued in the following line.

Uploading the Dockerfile

To upload our Dockerfile, compress it into a tarball first. Move to the directory where the Dockerfile is present in the terminal. Type the following command:

tar -czvf myDockerfile.tar.gz Dockerfile

You can rename tarball to whatever you prefer, e.g., ANY_NAME.tar.gz.

Building the Docker Image

A Docker image is generally a large file containing all the data required to instantiate a container, and we have a Dockerfile with all our instructions. The image can now be built as follows:

  • Upload the prepared .tar.gz.
  • Click on the Save button in the top-right corner to build the image.

This step may take a while, especially if the image is large.

We will get the following loading screen:

Adding the Docker Job

With the Docker image built, the next step is to add a Docker job for the image. The Docker job is responsible for creating the actual container. Hence, it is crucial to get this step right.

To create a job, click the Add Job button, and a menu will be displayed where we can set the details of the job:

Select Docker Job Type

We can use this to select our desired job type. We can choose from the following options depending upon our needs:

  • Default
  • Live
  • GUI

For the time being, we are only utilizing the Live job type in all of our standalone projects.

Job Name

This is simply the name of the job. This name will select the job in a code or any available widget.

Select a descriptive job name that can be identified easily. For example, we are adding a job for our project and utilizing it by selecting it in our live demo.

Input File Name

This is the file which the container will run. The widget uses the input file to determine which file must be executed. By default, the main file in the widget is called main. However, if you provide a different name in the Input File Name field, that file will replace the main. For example, we are naming one in adding a docker job section of one of our projects.

Run Script

This script will be run inside our container to execute the input file. Think of it as a set of commands that are run in the terminal of our container. For example, the following run script will run the available app.py file.

python app.py

Multiple Docker Jobs

We can add as many jobs as we may want the container to perform different tasks (specified in the Run Script) in each job. This allows us to make a single Docker image and perform various tasks within the same container.

Docker Use-cases

We can create our environments since we are familiar with the image building process and the Docker job. There are three (03) main scenarios in a project where Docker can be used:

  • SPA widget
  • Workspace widget
  • Code Evaluation widget

But for the projects our main focus is Workspace widget.

Whereas the detailed view of all the available widgets can be obtained in our upcoming lesson: Available Widgets.