...
/Running the Apache ShardingSphere JDBC with MySQL
Running the Apache ShardingSphere JDBC with MySQL
In the last lesson, we created and uploaded the tarball on the Educative platform.
Now, let’s look into how we can use the SPA widget and run the application in that.
Create a Docker Job
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 very important to get this step right.
To create a job, click the “Add Job” button:
A menu will be displayed where you can set the details of the job:
Select Docker Job Type
You can use this to select your desired job type. You can choose from the following options depending upon your needs:
- Default
- Live - Important for our use case.
- GUI
We have set the docker job type to
live
, as this is the one required for our use case.
Job Name
This is simply the name of the job. This name will be used to select the job in a code widget or SPA (more on this later).
Input File Name
The input file is used in the Code widget to determine which file has to be executed.
We can write a dummy file name here as this is irrelevant for our use case.
Run Script
In a Default type Docker job, this script will be run inside your container to execute the input file. Think of it as a set of commands that are run in the terminal of your container.
On the first run, the Run Script is executed before the Start Script, and after the first run, every subsequent run will only execute the commands written in this field (not the Start Script).
The code present in the SPA widget’s editor lives in a directory called /usercode
, which is present in your container’s root directory by default.
If you are serving your application from some other directory rather than the usercode
, for the first time, we can copy it using the Start Script. But, for subsequent runs, you will have to copy the project files to that specific directory from the /usercode
directory using the Run Script.
We will serve our application from the /usercode
directory.
Remember the SPA size limitation, that’s how we can work around this. We upload the files that the user can play around with within the SPA widget, while the rest of the dependencies are in the directory.
ls
Note: In this case, Run Script can be anything because it will not have any affect.
Application port
This is the port where your server will be listening. In our case, it’s 8080.
HTTPS Strict Mode
If your server is HTTPS, tick this field.
Force Relaunch On Widget Switch
With this option enabled, if you use the same job on different widgets, a container will be started on each switch. However, on the same widget, the session won’t restart. Check this box if you want the container to restart on every switch.
We do not need the Force Relaunch on Run for this tutorial. We can set it to its default value, i.e.,
false
.
Start Script
This script runs the first time you click the ‘RUN’ button in a SPA. It runs only once, as the first command whenever your container starts. Typically, this script would be used to 'start’ the server.
The code present in the SPA widget’s editor lives in a directory called /usercode
.
So if the user makes any changes to application.yaml
(which is in /usercode
), they will be automatically updated in the /usercode
directory on pressing Run again. We will run our server from this directory.
You can run the server from some other project directory as well. But if you want the user’s changes to take effect, you will have to copy the updated files from the
/usercode
directory to the appropriate project directory.
Our Start Script would look something like this:
service mysql startcd /usercode && mvn spring-boot:run
Your container is persistently active for 15 minutes. Hence, the Start Script is executed once the container times out.
Our final Docker job will look something like this:
Now that our Docker job is complete, we can run our app right away.
Select the Docker Job in the SPA
Navigate to your course’s lesson and create a new Single Page Application (SPA) widget. You can find the option in the lesson by clicking on the +
sign.
Once created, select the docker job that you created from the Docker (beta) dropdown menu:
Try executing the application below: