Docker in the Code Widget
Learn how Docker works in the Code widget and what features it provides.
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.
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 above code 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.
Here is the final Dockerfile:
# Base ImageFROM ubuntu:20.04# 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 20.04 (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
Setting the name to ‘Dockerfile’ is a must.
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.
Using 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.
The full demonstration can be found below:
from mlxtend.data import iris_datadata = iris_data()print(data)
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.