Skip to content

This command is a shortcut to klee container exec.

Run a command in a container

Usage

$ klee exec [OPTIONS] CONTAINER [COMMAND]...

Refer to the options section for an overview of available OPTIONS for this command.

Description

Run a new command in a running container.

A command executed with klee container exec is not restarted if the container is restarted.

COMMAND must be an executable and a chained command needs to be quoted. For example, klee container exec my_container sh -c "echo a && echo b" works, but klee container exec my_container "echo a && echo b" does not.

For example uses of this command, refer to the examples section below.

Options

Name, shorthand Description
--detach, -d Do not output STDOUT/STDERR to the terminal. If this is set, Klee will exit and return the container ID when the container has started.
--interactive, -i Send terminal input to container's STDIN. If set, detach will be ignored.
--tty, -t Allocate a pseudo-TTY
--env, -e Set environment variables (e.g. --env FIRST=value1 --env SECOND=value2)
--user, -u Username or UID of the user running the process
--help Show this message and exit.

Examples

Run klee container exec on a running container

First, start a container.

$ klee run --name mycontainer FreeBSD:latest /bin/sh /etc/rc
ad813478a0ec
created execution instance e9e43e57162d
ELF ldconfig path: /lib /usr/lib /usr/lib/compat
32-bit compatibility ldconfig path: /usr/lib32
Updating motd:.
Creating and/or trimming log files.
Clearing /tmp (X related).
Updating /var/run/os-release done.
Starting syslogd.
/etc/rc: WARNING: failed to start syslogd
Starting sendmail_submit.
554 5.3.0 host "localhost" unknown: Protocol not supported
/etc/rc: WARNING: failed to start sendmail_submit
Starting sendmail_msp_queue.
Starting cron.

Tue Nov  7 09:24:44 UTC 2023

e9e43e57162d has exited with exit-code 0

This creates and starts a container named mycontainer and runs the init script /bin/sh /etc/rc. Once the container has been initialized, the process exists and the container runs in the background.

Next, execute a non-interactive command on the container:

$ klee container exec mycontainer touch /tmp/execWorks

This creates a new file /tmp/execWorks inside the running container, and exits.

Next, start an interactive shell in the container:

$ klee container exec -it mycontainer /bin/sh

This starts a shell session in the container.

Next, set environment variables in a new shell session.

klee container exec runs with a minimal amount of environment variables by default. Use the --env (or -e) to set additional environment variables for the process.

Below, a new shell session in the container mycontainer is created with environment variables $VAR_A and $VAR_B set to "1" and "2" respectively. The environment variables are only valid for the process started by the command that defined the variables.

$ klee exec -e VAR_A=1 -e VAR_B=2 mycontainer env
created execution instance 84effca72ec8
VAR_B=2
VAR_A=1
SHELL=/bin/sh
HOME=/root
USER=root
PATH=/sbin:/bin:/usr/sbin:/usr/bin:/usr/local/bin

84effca72ec8 has exited with exit-code 0

Note on PATH and non-root users: kleened constructs the environment itself: PATH, HOME, USER and SHELL default to the configured user's values, and variables from the image configuration or --env are added on top (an explicit PATH overrides the default). For root (the default) the injected variables are exactly what the process sees. When the container or exec runs as a non-root user, the process is selected through FreeBSD's setusercontext(3), which re-builds PATH from the target user's login class — so a non-root process can end up with a different PATH than the one injected, and other variables still pass through. If a specific PATH matters for a non-root user, verify with env what the process actually sees.