Docker in the Code widget
Overview
The main purpose of using docker in the code widget is to provide support for:
- A language that is not supported by our platform
- A package that is not installed in a supported language
For example, Educative’s Python distribution may not have the module you need, or you may want to execute C++20 code on the platform. This is where docker comes into the picture and provides the functionality we need.
You can find our pre-configured docker environments here. Each docker environment contains a sample job description. If we do not have the pre-made environment you need, follow the directions below or email answers@educative.io.
Example: mlxtend
in Python
Suppose we want to use the mlxtend Python module in our code. We will code something like this:
from mlxtend.data import iris_datadata = iris_data()print(data)
You can observe that the code above is compiled with an error: ImportError: No module named 'mlxtend'
.
Even though our platform supports Python 3, we were unable to use this module since it is not installed by default. We can solve this problem through docker.
Objective
We must create a Docker container in which Python3 is installed with the mlxtend module.
Tip: It is a good practice to formulate what you want the container to do. This will help you identify all the things that need to be installed.
Step 1: Create a dockerfile
- Let’s begin by making a dockerfile.
- As discussed in the previous lesson, we need to use a base image to define the container’s OS. This base image is provided in the FROM command.
- Next, we need to install Python 3 in our container. This can be done using the RUN command.
- Finally, we will install mlxtend using pip.
The final dockerfile is shown below.
# Base ImageFROM ubuntu:focal# Installing different modulesRUN apt update && apt install -y software-properties-common &&\add-apt-repository ppa:deadsnakes/ppa &&\apt-get -y install curl &&\apt install -y python3.8 &&\apt-get install -y python3-distutils python3-apt &&\curl https://bootstrap.pypa.io/get-pip.py -o get-pip.py && python3 get-pip.py &&\pip3 install mlxtend
- Our base image is Ubuntu focal (line 2).
- Python 3.8, and pip are installed using
apt-get
andcurl
(lines 8 to 10). mlxtend
is installed usingpip3
(line 11).
As you can see, the commands listed in RUN
are identical to what you would do when installing Python 3 through the Command Prompt on a Ubuntu system.
Step 2: Upload the Tarball
Compress the dockerfile into a tarball. In the command below, we have named our tarball mltxend.tar.gz
:
tar -czvf mlxtend.tar.gz Dockerfile
- Upload this file in the Docker Container section of the Course Editor page.
- Click “Save” to build the image. If everything went well, the image will be built.
Step 3: Create the docker job
For the code widget, docker jobs are fairly simple. All we have to do is provide the job type, file name, and the instructions for executing it. Create a new job by pressing. the ‘+’ button.
The job for running Python code in our container is shown below:
In this job, we are telling the container to retrieve main.py
(Input File Name) and execute it (Run Script). The name of this job is Mlxtend and the job type is Default.
With our Docker job finalized, we can use the container to run mlxtend
in the Code widget.
How to use the docker job
We must select the created job in our code widget. This links the code to our docker container. To select a job:
- Create a code widget
- Write the code you want to execute
- Make the widget executable
- Select the job name from the Docker (Beta) menu in the panel
Now, you can see the purpose of defining the job name. It helps us select the job from the docker (beta) dropdown menu. We have multiple jobs which you’ll see later in the chapter. For now, we are ready to run mlxtend
code.
You can find our pre-configured docker environments here. Each docker environment contains a sample job description.
And that’s it! With Docker, we were able to use a Python module that was not supported on our platform. You can use this method to provide functionality for any language.
Tips
- Whenever you need to use docker to simply run a language, think about using the code widget with the default job type.
- When an image is built, wait for some time before selecting a job in your lesson and running the code. This is because the environment is still being set up on Educative’s backend. Running the code before this time period will cause an error. We recommend waiting for the same amount of time it took for the image to be built.
- The code widget may compile slowly the first time around. Once that is done, compilation time should be back to normal for future executions.
- If you alter a docker job, you will have to reselect it in the widget. The changes will not be reflected by default. Keep this in mind if you’re using the same job in multiple places. Each widget will have to be individually addressed.