README_ver2_java-hello-world.md
-
KIEU Thi phuong authoredKIEU Thi phuong authored
- How to run a Java program in DevContainer on the LIS Cluster
- Getting started
- Step by step
- Installation
- Install Docker
- Install Squashfs tools
- Install Devcontainer Command Line Interface (DCLI)
- Create Java project and Write dockerfile
- main.java
- Dockerfile
- Create docker image
- Run the Docker Container
- Initialize a project using a customized Devcontainer configuration
- Test running the DevContainer
- Push DevContainer to the LIS Cluster
- 1. Export the DevContainer
- 2. Push your Devcontainer on LIS Cluster
- Run your DevContainer on LIS Cluster
- Appendix
How to run a Java program in DevContainer on the LIS Cluster
Getting started
Prerequisite: Ubuntu Terminal
Tutorial source:
- “Manuals.” Docker Documentation. https://docs.docker.com/manuals/. Published March 5, 2025. Accessed March 18, 2025.
- Fabrice Daian. "Create, manage and deploy DevContainers | DevContainer Formation." LIS GitLab. https://gitlab.lis-lab.fr/sicomp/devcontainer_formation. Published December 5, 2024. Accessed March 18, 2025.
Step by step
Installation
Install Docker
-
Uninstalling any potential conflicting packages already installed on your computer:
for pkg in docker.io docker-doc docker-compose docker-compose-v2 podman-docker containerd runc; do sudo apt-get remove $pkg; done
-
Set up Docker's apt repository:
# Add Docker's official GPG key: sudo apt-get update sudo apt-get install ca-certificates curl sudo install -m 0755 -d /etc/apt/keyrings sudo curl -fsSL https://download.docker.com/linux/ubuntu/gpg -o /etc/apt/keyrings/docker.asc sudo chmod a+r /etc/apt/keyrings/docker.asc # Add the repository to Apt sources: echo \ "deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.asc] https://download.docker.com/linux/ubuntu \ $(. /etc/os-release && echo "$VERSION_CODENAME") stable" | \ sudo tee /etc/apt/sources.list.d/docker.list > /dev/null sudo apt-get update
-
Install the Docker packages:
sudo apt-get install docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin
-
Configure docker in rootless mode
sudo groupadd docker sudo usermod -aG docker $USER
-
Verify that the installation is successful by running the hello-world image:
docker run hello-world
This command downloads a test image and runs it in a container. When the container runs, it prints a confirmation message and exits.
Hello from Docker! This message shows that your installation appears to be working correctly. ...
Install Squashfs tools
sudo apt-get update && sudo apt-get install squashfs-tools
Install Devcontainer Command Line Interface (DCLI)
DCLI is a tool which will help you manage DevContainers (https://gitlab.lis-lab.fr/sicomp/dcli)
-
If you don't already have git installed, type:
sudo apt install git
-
Once done clone the DCLI directory:
git clone https://gitlab.lis-lab.fr/sicomp/dcli
-
Proceed to the install:
cd dcli chmod +x install.sh ./install.sh
-
Once installation is complete, you have to source your bashrc:
source ~/.bashrc
-
Check that dcli is working properly by typing:
dcli --version
Create Java project and Write dockerfile
Now, we will start with a simple Java program: Hello World. The file structure and content are organized as follows:
hello-world/
│── src/
│ └── main.java
└── Dockerfile
main.java
public class main {
public static void main(String[] args) {
System.out.println("Hello, World!");
}
}
Dockerfile
Create a Dockerfile
in the hello-world directory:
FROM openjdk
COPY src hello-world/src
WORKDIR hello-world
RUN mkdir -p bin
RUN javac -d bin ./src/main.java
WORKDIR bin
CMD ["java", "main"]
Note: Make sure that the scripts is named exactly as Dockerfile
or dockerfile
.
Create docker image
To create a Docker image from the Dockerfile, run the following command in the hello-world
directory:
docker build -t hello:test .
with hello:test
assigns the image a name hello
and a tag test
.
Run the Docker Container
Once the image is built, you can run the container using:
docker run -it hello:test
Initialize a project using a customized Devcontainer configuration
On Terminal, type:
dcli init
Insert image name of docker created:
Image Name: hello:test
Test running the DevContainer
Type :
dcli start
You will see the VSCode workspace opened. Test running the java program inside the DevContainer, type:
java main.java
Stop the container:
dcli stop
Push DevContainer to the LIS Cluster
1. Export the DevContainer
Type:
dcli export
Once finished, you should see a squashfs file created.
Note: Ignore the error on permission denied, or type:
dcli export | chmod -R 777 tmp
2. Push your Devcontainer on LIS Cluster
On the first teminal window, connect to the cluster:
ssh thi-phuong.kieu@sms
Then create new folders:
mkdir devcontainer_images # for the squashfs file
mkdir hello-world # for your soucre code
On the second window, type:
dcli config
Please provide the following information to create the .cluster_config file.
Cluster server name/address [sms]: sms
LIS cluster username []: thi-phuong.kieu
Devcontainer workspace directory path [/workspace]: /workspace
Project directory (absolute path) to mount inside the devcontainer located on the cluster []: /home/thi-phuong.kieu/hello-world/
Directory containing squashfs images (absolute path) located on the cluster []: /home/thi-phuong.kieu/devcontainer_images/
.cluster_config file created successfully in the .devcontainer directory.
Now, push the squashfs file to the cluster push your squashfs fil inside the cluster:
dcli push
Then, push your source code to the cluster. E.g:
scp -r -o ProxyJump="thi-phuong.kieu@139.124.22.4" ./src/main.java thi-phuong.kieu@sms:/home/thi-phuong.kieu/hello-world/src/main.java
Run your DevContainer on LIS Cluster
On the termianl, type:
dcli irun
Once connect successfully, type:
java main.java
Note: To run manually, connect to the cluster first, then type:
srun --container-image=/home/thi-phuong.kieu/devcontainer_images//dcli_hello-world.squashfs --container-mounts=/home/thi-phuong.kieu/hello-world/:/workspace --container-workdir=/workspace --cpus-per-task=12 --pty bash
Appendix
With the DEv-CF project, the dockerfile is written as follow:
FROM openjdk:17
# Copy all necessary project folders into the container
COPY src/ /devs-dclitest/src/
COPY lib/ /devs-dclitest/lib/
COPY input/ /devs-dclitest/input/
COPY output/ /devs-dclitest/output/
WORKDIR /devs-dclitest
RUN mkdir -p /devs-dclitest/bin
# Compile Java source files with the required library
RUN javac -cp "/devs-dclitest/lib/fwkdevs-v0.7" -d /devs-dclitest/bin /devs-dclitest/src/*.java
# Run the Java application with the bin and lib path
CMD ["java", "-cp", "/devs-dclitest/bin:/devs-dclitest/lib/fwkdevs-v0.7", "Simulator"]