favicon here hometagsblog

Calling Executables Outside Distrobox That Are From Another Distrobox

#container #distrobox #podman

Soc Virnyl Estela | 2023-08-06 | updated: 2024-01-30 |reading time: ~5min

Update§

This old post is outdated and requires a small update. To run an executable from another container (here we use distrobox, a frontend for podman or docker), you just need to run this command inside the desired container where you want to run the executable.

sudo ln /usr/bin/distrobox-host-exec /usr/local/bin/my-executable

Make sure that the my-executable was already distrobox-exported to the host.

Old post§

To anyone that might have asked themselves

How do I call an executable from Y distro to the current X distro I am using in distrobox?

The answer is to create a script. But first you will have to use distrobox-host-exec. Create a symlink inside your distrobox. You can either declare an init-hook or do it manually. The command is

ln -sf distrobox-host-exec /usr/local/bin/podman

This will create a pseudo podman executable that will run the host system's podman, assuming you have that installed in your host system.

To check if it works, run

podman ps

This will give you a list of available containers that are active.

Example situation§

So let's assume you are in a weird situation. You want to use zig but the one on openSUSE Tumbleweed distrobox is 0.10.0 because it has an issue with glibc versions. But it builds correctly on openSUSE Leap 15.5! The next thing you did was to create your leap distrobox

distrobox-create -i leap:latest -n leap

And then you ran the following command inside your leap distrobox

sudo zypper addrepo https://download.opensuse.org/repositories/devel:tools:compiler/15.5/devel:tools:compiler.repo
sudo zypper refresh
sudo zypper install zig

So uh... how do I use zig from leap when I am in a tumbleweed distrobox?

By using distrobox-host-exec which calls your podman executable! Remember the symlink? Here is the idea

podman has an exec command. Running podman exec --help gives you the following output:

Run a process in a running container

Description:
  Execute the specified command inside a running container.


Usage:
  podman exec [options] CONTAINER [COMMAND [ARG...]]

Examples:
  podman exec -it ctrID ls
  podman exec -it -w /tmp myCtr pwd
  podman exec --user root ctrID ls

Options:
  -d, --detach               Run the exec session in detached mode (backgrounded)
      --detach-keys string   Select the key sequence for detaching a container. Format is a single character [a-Z] or ctrl-<value> where <value> is one of: a-z, @, ^, [, , or _ (default "ctrl-p,ctrl-q")
  -e, --env stringArray      Set environment variables
      --env-file strings     Read in a file of environment variables
  -i, --interactive          Keep STDIN open even if not attached
  -l, --latest               Act on the latest container podman is aware of
                             Not supported with the "--remote" flag
      --preserve-fds uint    Pass N additional file descriptors to the container
      --privileged           Give the process extended Linux capabilities inside the container.  The default is false
  -t, --tty                  Allocate a pseudo-TTY. The default is false
  -u, --user string          Sets the username or UID used and optionally the groupname or GID for the specified command
  -w, --workdir string       Working directory inside the container

Since it says here that we can run a process from a running container, we can create a script to run zig in your tumbleweed distrobox!

#!/bin/bash
/usr/local/bin/podman exec --user $USER -it -w $PWD leap zig $@

And save it to /usr/local/bin/zig and run sudo chmod +x /usr/local/bin/zig.

Testing your zig executable§

Inside your tumbleweed distrobox which now contains your pseudo zig executable, you can test if it works by doing the commands

md hello-zig/
cd $_
zig init-exe
zig build
./zig-out/hello-zig

The last command should output

All your codebase are belong to us.
Run `zig build test` to run the tests.

How it works§

We have distrobox-host-exec (which calls host-spawn in the background), and podman. By using distrobox-host-exec to run the host system podman, we can also check other running containers, not just from leap distrobox in the previous examples.

With podman, we can use its exec command to run executables from other containers. The important flags are

  • -w or --workdir. This is where you set $PWD
  • -i or --interactive. This allows interactivity
  • -t or --tty. This will allow it to work somewhat okay-ish in a terminal.

Plus $@ to add possible other subcommands of an executable e.g. build, test, --help.

The --user is set to $USER so it respects your user inside the container. Otherwise, it will become root which maybe is not what you want.

So the final and cool command for the pseudo zig executable is:

#!/bin/bash

# leap can be anything: container ID or container NAME
/usr/local/bin/podman exec --user $USER -it -w $PWD leap zig $@

More information§

You can find more information from the following links:

Articles from blogs I follow around the net

Perma-Vuln: D-Link DIR-859, CVE-2024-0769

Recently Sift caught an interesting payload. As it turns out, the exploit was CVE-2024-0769, which is now tagged here: D-Link DIR-859 Information Disclosure Attempt . This vulnerability is a path traversal leading to information disclosure. But, perhaps mo…

via GreyNoise LabsJune 25, 2024

Synergy Greg

Synergy Greg would like to see you in His office, it is the one down the hall, past the cubicles and dreary faces, uplifted only when He deigns to venture forth. You will know Him when you see Him, He is the one composed, of a thousand writhing forms,…

via LudicityJune 22, 2024

Status update, June 2024

Hi all! This status update will be shorter than usual because I had a lot less free time for my open-source projects than usual this month. Indeed, I recently joined SNCF Réseau (the company responsible for the French railway infrastructure) to work on OSR…

via emersionJune 18, 2024

Why People are Angry over Go 1.23 Iterators

NOTE: This is based on, but completely rewritten, from a Twitter post: https://x.com/TheGingerBill/status/1802645945642799423 TL;DR It makes Go feel too “functional” rather than being an unabashed imperative language. I recently saw a post on Twitter showi…

via Articles on gingerBillJune 17, 2024

My RSS feed has been upgraded ✨

I did some integration work to include posts written for other publications in my RSS feed. Apologies if you see some duplicated items! 📪

via Rob O'LearyJune 15, 2024

Programming at the edge with Fastly Compute

So you’ve heard about computing at the edge, and you’ve heard that Fastly let’s you run JavaScript, Go, Rust and any other language that compiles to Wasm at the edge… well, let’s take a look and while we’re at it let’s try and understand how caching works …

via Posts on integralistJune 12, 2024

Generated by openring-rs