Docker Setup

Docker on the Educative Platform

We use Docker to allow authors to create their own custom environments to accommodate any language that is not supported by our platform.

Authors can create custom docker images that deploy on the Google Cloud and allow end-users to directly have access to these customized environments through our platform.

The Jargon

Base Image: An OS userspace.

Image: A read-only template with instructions for creating a Docker container.

Dockerfile: A simple file with no extension, named as Dockerfile is used to builds a Docker image, and specifies everything that will go in the application’s environment setup.

Container: An image when run, builds a container. This contains your application environment, basically where your application will be run.

Tarball

-tarball
--Dockerfile
--demo

The above shows the directory structure of our tarball.

The command to create this tarball is as follows:

tar -czvf maven.tar.gz Dockerfile demo

As you can see, the Dockerfile must be nested directly inside the tarball (not inside any child folder).

This allows Educative to access the Dockerfile directly and create a docker container for our application’s environment.

Apart from Dockerfile, there is a folder called demo which contains the code.

Points to Note:

  • All files can be found for download in the Appendix of this lesson.
  • Only ONE tarball with ONE Dockerfile can be uploaded and used at a time. Uploading a new tarball will replace any previously uploaded.

Dockerfile

Press + to interact
FROM ubuntu:latest
ARG DEBIAN_FRONTEND=noninteractive
ENV TZ=Europe/Moscow
# Install Java JDK
RUN apt-get update && apt-get install -y gnupg openjdk-11-jdk ca-certificates-java curl wget git vim sudo postgresql postgresql-contrib tzdata && \
apt-get clean && \
update-ca-certificates -f;
ENV JAVA_HOME /usr/lib/jvm/java-11-openjdk-amd64/
RUN export JAVA_HOME && service postgresql start && su - postgres -c "psql -U postgres -d postgres -c \"alter user postgres with password 'postgres';\"" \
&& su - postgres -c "psql -U postgres -d postgres -c \"Create Database demo_test;\"" && su - postgres -c "psql -U postgres -d postgres -c \"Create Database demo_test_1;\"" && service postgresql stop
# Install MySQL and set up the databases and tables we will be interacting with
RUN apt-get update && apt-get install -y mysql-server mysql-client && service mysql start && mysql -u root -e "Create DATABASE test0;" && mysql -u root -e "Create DATABASE test1;" && mysql -u root -e "use test0; Create Table user0(id bigint, name VARCHAR(50), age int, email varchar(50)); Create Table user1(id bigint, name VARCHAR(50), age int, email varchar(50)); Create Table user2(id bigint, name VARCHAR(50), age int, email varchar(50));" && \
mysql -u root -e "use test1; Create Table user0(id bigint, name VARCHAR(50), age int, email varchar(50)); Create Table user1(id bigint, name VARCHAR(50), age int, email varchar(50)); Create Table user2(id bigint, name VARCHAR(50), age int, email varchar(50));" &&\
mysql -u root -e "Create User 'user'@'localhost' identified by 'user'; GRANT ALL PRIVILEGES ON *.* TO 'user'@'localhost'; Flush Privileges;" && service mysql stop
#### Install Maven
RUN apt-get install -y maven
## Install Spring Boot (2.3.0)
RUN wget https://repo.spring.io/release/org/springframework/boot/spring-boot-cli/2.3.0.RELEASE/spring-boot-cli-2.3.0.RELEASE-bin.tar.gz
RUN tar -xzf spring-boot-cli-2.3.0.RELEASE-bin.tar.gz
RUN mv spring-2.3.0.RELEASE /opt/
RUN ln -s /opt/spring-2.3.0.RELEASE/bin/spring /usr/bin/spring
RUN ln -s /opt/spring-boot-cli-2.3.0.RELEASE/shell-completion/bash/spring /etc/bash_completion.d/spring
# Install node first to run npm just in case
RUN curl -sL https://deb.nodesource.com/setup_14.x -o nodesource_setup.sh &&\
bash nodesource_setup.sh &&\
apt install -y nodejs

Let’s discuss the contents of this file one by one.

FROM

The FROM command sets the Base Image for the rest of the instructions. This command must be on top of the Dockerfile.

In this example, we are starting with the base image of ubuntu:latest. This will allow us to install Python and NodeJS side by side. Make sure you use ubuntu:latest version in your Dockerfile if you are making a LiveVM

You can use a specific version of your base image, by appending : and the version_name at the end of the image name. In case of LiveVM setup, whatever base image you use, you need to install ubuntu:20.04 via Dockerfile for the SPA + LiveVM to work.

For code widget this is not necessary.

RUN

RUN is used to execute commands that build up the Docker image, adding to the build time.

Note: Each RUN command builds a layer on top of the base image, which results in a longer build time. Hence, chaining your commands in a single run makes things faster.

We are performing the following tasks with the Run command:

  • Installing the required packages/services
  • Creating database and tables
  • Exporting environment variables
  • Installing maven
  • Installing Springboot
  • Installing node

ENV

ENV is used to set environment variables.

ARG

ARG is used to define parameters.

Once we have created a Dockerfile we need to upload it on the Educative platform.

Docker Configuration

On the Collection Editor page, scroll down to find the “Docker Container” section. This is where most of the setup takes place.

Uploading the Dockerfile

To upload your Dockerfile, compress it into a tarball first. In the terminal, move to the directory where the Dockerfile is present.

Note: The Dockerfile needs to be at the root of this directory.

Building the tarball

Once the tarball has been uploaded, 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. You will see the following loading screen:

If the image is built successfully, a message will be prompted on the screen:

If there is an error in the Dockerfile, the image building process will fail. This can happen due to a number of reasons. To check the details of the issue, click the “Download Build Logs” button:

You can download the tarball that we created for this tutorial from the widget below:

Appendix

File to download

The file attached below can be used to set enviroment on Educative’s Platform.

maven.tar.gz