Project Runtime
Learn all about the custom environments provided in projects.
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:
Authors can create custom Docker images and allow end-users to access these customized environments through our platform directly. In comparison, the Docker environment on our platform is slightly different from running it locally. Let’s create a custom environment for 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) and set up instructions for the applications and the frameworks that we want in our environment.
Note: The file
Dockerfile
has no extension and can be created using any available text editor.
For instance, let’s create a custom environment that supports Python 3.9 with the required machine learning libraries as follows:
FROM ubuntu:20.04RUN 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
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 withPython 3.10.1
installed on it.node:14.18
—A Linux environment withNode 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 to set up our environment, starting from our base image. These commands are executed only once when we are building the docker image. Think of it as similar to setting up your custom environment on top of a fresh system containing just the basic OS (the base Docker image), using a terminal like the one we have in our Unix
/Linux
-based operating systems. Results of the RUN
commands persist and add a layer on top of the Docker image created thus far.
For instance, the above-given code snippet installs and updates the required packages.
We can add multiple
RUN
commands in our Dockerfile. But every additionalRUN
command will increase the size of a Docker image, which will take longer to build. Hence, a good practice is to use a singleRUN
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. We can now build the Docker image as follows:
- Upload the prepared
.tar.gz
. - Click on the
Save
button in the top-right corner to build the image.
Note: This step may take a while, especially if the image is large.
We will get the following loading screen:
Adding the Docker Jobs
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 on the “+” icon with the “Add Job” text on hover, 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 on our needs:
- Default
- Live
- GUI
- ANDROID
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.
Note: Select a descriptive job name that can be identified easily. For example, see how we are naming one in adding a docker job section of one of our demo projects.
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 the 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 multiple docker jobs as we may want the container to perform different tasks (specified in te Run Script) in each job. This allows us to make a single Docker image and use it in different ways to perform different tasks.
Docker Use-cases
We can now create our custom environments using Dockerfile
, building the Docker image, and by using the Docker jobs. There are three (03) main widgets in projects where Docker can be used:
- SPA widget
- Workspace widget
- Code Evaluation widget
Note: For the projects our main focus is Workspace widget.
A detailed tour of all of the available widgets can be obtained in an upcoming lesson: Available Widgets.