Jeremy Wall 0053afbe0c DOCS: Add a reference for converters to the docsite and some examples.
Also update the README to point to the Docsite as well.
2018-11-17 22:11:52 -06:00

2.2 KiB

+++ title = "Creating a launch script for a docker container" weight = 1 sort_by = "weight" in_search_index = true +++ Let's say we wanted to use ucg generate a launch script for a local jupyter datascience notebook container. This HowTo walks you through creating a ucg file that can generate a script to launch the container.

First lets define some docker configuration values that we'll use later.

let image_conf = {
    port = 8888,
    hostMount = "~/iJulia/notebooks",
    mountPoint = "/var/iJulia/notebooks",
};

Then we can define a helper macro for creating our host and port mappings

// A little helper macro for creating our host and port mappings.
let map_to_container = macro (host, container) => {
    result = "@:@" % (host, container)
};

let publish = map_to_container(docker_conf.port, docker_conf.port);
let volumes = map_to_container(docker_conf.hostMount, docker_conf.mountPoint);

Now we set up our docker run flags.

let docker_flags = {
    // d because we want this container to daemonize.
    d = NULL, // no value for this one.
    // We provide the container name from the environment.
    name = env.CONTAINER_NAME,
    // We use our generated publish and volume mappings from above.
    publish = publish.result,
    volume = volumes.result,
};

Finally we are ready to define our jupyter specific flags.

let jupyter_flags = {
    notebook-dir = docker_conf.mountPoint
};

Finally we tie it all together into a script and output it as a bash script using the exec converter.

let script = {
    command = "docker",
    args = [
        "run", "--rm", docker_flags,
        "jupyter/datascience-notebook",
        "jupyter", "notebook", jupyter_flags,
    ],
};

out exec script;

The script tuple above will generate the following bash script:

#!/usr/bin/env bash
# Turn on unofficial Bash-Strict-Mode
set -euo pipefail

exec docker run --rm -d --name '<from-env>' --publish '8888:8888' --volume '~/iJulia/notebooks:/var/iJulia/notebooks' jupyter/datascience-notebook jupyter notebook --notebook-dir '/var/iJulia/notebooks'

The items in the args should be either strings or tuples. The tuples are turned into flags using the builtin flag converter.