DEV Community

Cover image for Vlang as a scripting language for docker image entry point
Vincenzo Palazzo
Vincenzo Palazzo

Posted on

Vlang as a scripting language for docker image entry point

In this small blog post, I will describe how to use the V language (https://vlang.io/) as a scripting language to configure the entry point of your docker configuration.

What is V language?

V language is a Simple language for building maintainable programs and also if a user friendling language with a syntax similar to the Go lang language, but with some additional features like mutability and generics.

In addition, the v standard library contains some methods for writing a shell script in a comfortable when for people that are born in the new era and find the original shell language like bash completely messy.

Docker image entry point

The common usage of docker image is to isolate the environment and make a reproducible environment for an application.

However, there are applications, like command-line applications that required interaction with the docker daemon to share the command with the container and return the result of the container directly to the user.

This type of operation is usually done by a shell script runner by docker each time that the container is invoked. However, if the command line application is complex like Bitcoin Core, or Core lightning the bash script starts to be very complex and full of strange commands to check equality between strings to check what type of operation in the docker image the script needs to run.

In addition, if managing the persistent status of docker the bash script starts to be not manageable, as a not bash friendly person that needs to resolve this complex use case with the docker image, I decided to use the V language as script language, and build a very cool script that resolves my problems.

In fact, the v compiler installed in the docker image is very minimal, and usually doesn't require external dependencies, so the impact is very minimal, and the API to run the os command is very user-friendly.

I build my docker image available at the following link and the pattern used in this case was to build a simple entrypoint.sh file, that looks like the following code

#!/bin/sh
set -e
v run /opt/conf_env.vsh "$@"
Enter fullscreen mode Exit fullscreen mode

Where the conf_env.vsh looks like the following code

// V script to manage the envoirment lightning env
//
// author: https://github.com/vincenzopalazzo
import os

fn run_tor() {
    if os.exists('/home/clightning4j/.tor') {
        os.execute_or_panic('rm -r /home/clightning4j/.tor')
    }
    println(os.execute_or_panic('tor --runasdaemon 1').output)
    curl_res := os.execute_or_panic('curl --socks5 localhost:9050 --socks5-hostname localhost:9050 -s https://check.torproject.org/ | cat | grep -m 1 Congratulations | xargs')
    println(curl_res.output)
    os.execute_or_panic('chown -R clightning4j /home/clightning4j/.tor')
}

fn build_ln_directory() string {
    ln_dir := os.environ()['CLIGHTNING_DATA']
    if !os.exists(ln_dir) {
        os.mkdir(ln_dir) or { panic('dir at path $ln_dir not created') }
        os.execute_or_panic('cp /opt/config $ln_dir/config')
        os.execute_or_panic('mkdir $ln_dir/plugins')
        os.execute_or_panic('cp /opt/*.sh $ln_dir/plugins/')
        os.execute_or_panic('chown -R clightning4j $ln_dir')
    }
    run_tor()
    return ln_dir
}

mut args_str := ''
for idx in 1 .. os.args.len {
    arg := os.args[idx]
    args_str += arg + ' '
}

if args_str.contains('lightning-cli') || !args_str.contains('--') {
    println(os.execute_or_panic(args_str).output)
} else {
    ln_dir := build_ln_directory()
    os.execute_or_panic('lightningd --lightning-dir=$ln_dir $args_str')
}
Enter fullscreen mode Exit fullscreen mode

Closing Thoughts

If you are trying V language in with any other type of use case, please share the information with us, we are super happy to know how you are using the language. 

If you have any particular questions, leave a comment and follow me on Github https://github.com/vincenzopalazzo

Latest comments (0)