Getting Started with Docker
Let's learn how to leverage Docker environments in the Educative platform.
What is Docker?
Docker is a container management service that allows developers to build applications, ship them into containers and run them anywhere. Hence, you can create your own custom environments which can include a variety of Operating Systems and developing frameworks.
Docker containers are pretty lightweight and can be deployed on any physical and virtual machines and even on the cloud.
The Jargon
- Base Image: An OS userspace.
- Image: A read-only template with instructions for creating a Docker container. Often, an image is based on another image, with some additional customization.
- Container: A runnable instance of an image.
Here are two basic tutorials for beginners. They can help you out in getting started on Docker locally:
Docker on Educative
We use Docker to allow you to create your own custom environments to accommodate any language/framework that is not supported by our platform.
Creating a Dockerfile
The first step in setting up a Docker environment is creating a Dockerfile. This is a text file in which we specify the OS and the applications/frameworks/servers that we want in our environment. The Dockerfile has no file extension.
You can use any text editor to make your Dockerfile. Just save it as
Dockerfile
(without any extension).
The Dockerfile is used to create a Docker image. A container of your environment will then be instantiated based on the image.
Suppose we want to use the mlxtend
Python module in our code. This module is not available in the code widget as shown below.
from mlxtend.data import iris_datadata = iris_data()print(data)
We can solve this problem through Docker. We will start by making a simple Dockerfile.
# Base ImageFROM gcr.io/educative-exec-env/educative:latest# Installing different modulesRUN apt update && apt install -y software-properties-common curl &&\add-apt-repository ppa:deadsnakes/ppa &&\apt-get update &&\apt install -y python3.8 &&\apt-get install -y python3.8-distutils python3-apt &&\curl https://bootstrap.pypa.io/get-pip.py -o get-pip.py && python3.8 get-pip.py &&\pip3 install mlxtend
- We use Educative’s base image (line 2).
- Python 3.8, and pip are installed using
apt-get
andcurl
(lines 8 to 10). mlxtend
is installed using pip3 (line 11).
You can visit this link for more information on creating a Dockerfile and its different components.
Create the Tarball
Compress the Dockerfile into a tarball. In the terminal, move to the directory where the Dockerfile is present.
In the command below, we have named our tarball mltxend.tar.gz
:
tar -czvf mlxtend.tar.gz Dockerfile
Note: The Dockerfile needs to be at the root of this directory.
Upload the tarball
On the Collection Editor page, scroll down to find the Docker Container section. This is where most of the setup takes place.
Upload this file in the Docker Container section. Click Save to build the image. If everything went well, the image will be built:
Caveat
Note: If the docker image doesn’t build under 10 minutes, it would timeout.
Create the Docker Job
or 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.
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.
Optimization
We want the uptime of a widget to be minimum to provide a good user experience. Therefore avoid run time installations (in the docker job) and move them to inside the docker image.
Also, optimize your docker image as much as possible.
Docker with different widgets
In order to realise the full potential of Docker when coupled with the Educative widget, explore the following links:
- Docker in the Code Widget
- Docker in the SPA Widget
- Docker in the Terminal Widget
- Docker with the GUI App Widget
- Running Python Jupyter Notebooks Using Live VM
- Docker Setup Guides for Different Platforms
The next lesson talks about testing the learner.