Title: | Talking to 'Docker' and 'Singularity' Containers |
---|---|
Description: | Provides a unified interface to interact with 'docker' and 'singularity' containers. You can execute a command inside a container, mount a volume or copy a file. |
Authors: | Robrecht Cannoodt [aut, cre] (<https://orcid.org/0000-0003-3641-729X>, rcannood), Wouter Saelens [aut] (<https://orcid.org/0000-0002-7114-6248>, zouter), Joel Nitta [aut] (<https://orcid.org/0000-0003-4719-7472>, joelnitta) |
Maintainer: | Robrecht Cannoodt <[email protected]> |
License: | MIT + file LICENSE |
Version: | 1.2.0 |
Built: | 2024-11-05 02:40:58 UTC |
Source: | https://github.com/dynverse/babelwhale |
Provides a unified interface to interact with docker' and 'singularity' containers. You can execute a command inside a container, mount a volume or copy a file.
Copy a file from a container to the host system
copy_file(container_id, path_container, path_local)
copy_file(container_id, path_container, path_local)
container_id |
The name of the container, usually the repository name on dockerhub. |
path_container |
The path of the file inside the container |
path_local |
The path of the file on the host system |
if (test_docker_installation()) { set_default_config(create_docker_config(), permanent = FALSE) copy_file("alpine", "/bin/date", tempfile()) }
if (test_docker_installation()) { set_default_config(create_docker_config(), permanent = FALSE) copy_file("alpine", "/bin/date", tempfile()) }
It is advised to define the "BABELWHALE_BACKEND"
environment variable as "docker"
or "singularity"
.
When using singularity, also define the "SINGULARITY_CACHEDIR"
environment variable,
which is the folder where the singularity images will be cached.
When using apptainer, also define the "APPTAINER_CACHEDIR"
environment variable,
which is the folder where the singularity images will be cached.
Each TI method will require about 1GB of space.
Alternatively, you can create a config and save it using set_default_config()
.
create_config( backend = get_env_or_null("BABELWHALE_BACKEND") %||% detect_backend() ) create_docker_config(environment_variables = character(0)) create_singularity_config( cache_dir = get_env_or_null("SINGULARITY_CACHEDIR") %||% get_env_or_null("APPTAINER_CACHEDIR") %||% ".singularity/", environment_variables = character(0) ) get_default_config() set_default_config(config, permanent = TRUE)
create_config( backend = get_env_or_null("BABELWHALE_BACKEND") %||% detect_backend() ) create_docker_config(environment_variables = character(0)) create_singularity_config( cache_dir = get_env_or_null("SINGULARITY_CACHEDIR") %||% get_env_or_null("APPTAINER_CACHEDIR") %||% ".singularity/", environment_variables = character(0) ) get_default_config() set_default_config(config, permanent = TRUE)
backend |
Which backend to use. Can be either |
environment_variables |
A character vector of environment variables. Format: |
cache_dir |
A folder in which to store the singularity images. A container typically requires 100MB to 2GB. |
config |
A config to save as default. |
permanent |
Whether or not to save the config file permanently |
config <- create_docker_config() set_default_config(config, permanent = FALSE) config <- create_singularity_config( # ideally, this would be set to a non-temporary directory cache_dir = tempdir() ) set_default_config(config, permanent = FALSE)
config <- create_docker_config() set_default_config(config, permanent = FALSE) config <- create_singularity_config( # ideally, this would be set to a non-temporary directory cache_dir = tempdir() ) set_default_config(config, permanent = FALSE)
List docker containers
list_docker_images(container_id = NULL)
list_docker_images(container_id = NULL)
container_id |
An optional container id |
if (test_docker_installation()) { set_default_config(create_docker_config(), permanent = FALSE) list_docker_images() }
if (test_docker_installation()) { set_default_config(create_docker_config(), permanent = FALSE) list_docker_images() }
Pull a container from dockerhub
pull_container(container_id)
pull_container(container_id)
container_id |
The name of the container, usually the repository name on dockerhub. |
if (test_docker_installation()) { pull_container("alpine") }
if (test_docker_installation()) { pull_container("alpine") }
Read a file from a container
read_file(container_id, path_container)
read_file(container_id, path_container)
container_id |
The name of the container, usually the repository name on dockerhub. |
path_container |
The path of the file inside the container |
if (test_docker_installation()) { set_default_config(create_docker_config(), permanent = FALSE) read_file("alpine", "/etc/hosts") }
if (test_docker_installation()) { set_default_config(create_docker_config(), permanent = FALSE) read_file("alpine", "/etc/hosts") }
Run a containerised command, and wait until finished
run( container_id, command, args = NULL, volumes = NULL, workspace = NULL, environment_variables = NULL, debug = FALSE, verbose = FALSE, stdout = "|", stderr = "|" )
run( container_id, command, args = NULL, volumes = NULL, workspace = NULL, environment_variables = NULL, debug = FALSE, verbose = FALSE, stdout = "|", stderr = "|" )
container_id |
The name of the container, usually the repository name on dockerhub. |
command |
Character scalar, the command to run. If you are
running |
args |
Character vector, arguments to the command. |
volumes |
Which volumes to be mounted. Format: a character vector, with each element containing the source path and container path concatenated with a ":". For example: |
workspace |
Which working directory to run the command in. |
environment_variables |
A character vector of environment variables. Format: |
debug |
If |
verbose |
Whether or not to print output |
stdout |
What to do with standard output of the command. Default ( |
stderr |
What to do with standard error of the command. Default ("|") means to include it as an item in the results list.
If it is the empty string ( |
if (test_docker_installation()) { set_default_config(create_docker_config(), permanent = FALSE) # running a command run("alpine", "echo", c("hello")) # mounting a folder folder <- tempdir() write("i'm a mounted file", paste0(folder, "/file.txt")) run("alpine", "cat", c("/mounted_folder/file.txt"), volumes = paste0(folder, "/:/mounted_folder")) }
if (test_docker_installation()) { set_default_config(create_docker_config(), permanent = FALSE) # running a command run("alpine", "echo", c("hello")) # mounting a folder folder <- tempdir() write("i'm a mounted file", paste0(folder, "/file.txt")) run("alpine", "cat", c("/mounted_folder/file.txt"), volumes = paste0(folder, "/:/mounted_folder")) }
Similar to run()
, but automatically mounts files (and directories) so the
user doesn't have to keep track of volumes.
run_auto_mount( container_id, command, args = NULL, wd = NULL, wd_in_container = NULL, environment_variables = NULL, debug = FALSE, verbose = FALSE, stdout = "|", stderr = "|" )
run_auto_mount( container_id, command, args = NULL, wd = NULL, wd_in_container = NULL, environment_variables = NULL, debug = FALSE, verbose = FALSE, stdout = "|", stderr = "|" )
container_id |
The name of the container, usually the repository name on dockerhub. |
command |
Character scalar, the command to run. If you are
running |
args |
Character vector, arguments to the command. Any files or directories that should be mounted must be named "file" (see example). |
wd |
Local working directory to run command. If specified, the working directory will be mounted to the docker container. |
wd_in_container |
Working directory to run command in
the container. Defaults to the working directory mounted to the container
( |
environment_variables |
A character vector of environment variables. Format: |
debug |
If |
verbose |
Whether or not to print output |
stdout |
What to do with standard output of the command. Default ( |
stderr |
What to do with standard error of the command. Default ("|") means to include it as an item in the results list.
If it is the empty string ( |
The main difference to run()
is that the use of names for the args
; any
file (or directory) that should be mounted inside the container must be named
file
. The other elements (arguments) don't need to be named. Note that it
is fine to have multiple elements with the same name (file
).
This should generally work as long as the command accepts absolute paths
for file input. If that is not the case, use run()
instead and specify
paths and mounting manually.
List, formatted as output from processx::run()
if (test_docker_installation()) { # Count the number of lines in the DESCRIPTION and LICENSE # files of this package run_auto_mount( container_id = "alpine", command = "wc", args = c("-l", file = system.file("DESCRIPTION", package = "babelwhale"), file = system.file("LICENSE", package = "babelwhale") ) ) }
if (test_docker_installation()) { # Count the number of lines in the DESCRIPTION and LICENSE # files of this package run_auto_mount( container_id = "alpine", command = "wc", args = c("-l", file = system.file("DESCRIPTION", package = "babelwhale"), file = system.file("LICENSE", package = "babelwhale") ) ) }
Determine the cached path of singularity images
singularity_image_path(container_id)
singularity_image_path(container_id)
container_id |
The name of the container, usually the repository name on dockerhub. |
Tests whether docker is correctly installed and available
test_docker_installation(detailed = FALSE)
test_docker_installation(detailed = FALSE)
detailed |
Whether top do a detailed check |
test_docker_installation() if (test_docker_installation()) { test_docker_installation(detailed = TRUE) }
test_docker_installation() if (test_docker_installation()) { test_docker_installation(detailed = TRUE) }
Tests whether singularity is correctly installed and available
test_singularity_installation(detailed = FALSE)
test_singularity_installation(detailed = FALSE)
detailed |
Whether top do a detailed check |
test_singularity_installation() if (test_singularity_installation()) { test_singularity_installation(detailed = TRUE) }
test_singularity_installation() if (test_singularity_installation()) { test_singularity_installation(detailed = TRUE) }