favicon here hometagsblogmicrobio cvtech cvgpg keys

Calling Executables Outside Distrobox That Are From Another Distrobox

#container #distrobox #podman

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

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

Some Thoughts on the Twitter Mass Exodus

Another wave of Twitter users are jettisoning the social media website in favor of alternatives. Some are landing in the Fediverse (Mastodon and other ActivityPub-enabled software). Others are going to BlueSky. Some are just outright abandoning social medi…

via Dhole MomentsNovember 19, 2024

hyper in curl Needs a Champion

tl;dr - hyper in curl is nearly complete, but it needs a champion. Without a partner actively engaged that wants to enable and ship, it’s now on the path for being deprecated and removed. It needs a champion, a backing vendor or distro. Will that be you? …

via seanmonstarNovember 19, 2024

Swift observations from a reluctant Rustacean

Recently I've been thinking about Swift in terms of Rust, & have appreciated anew some of the choices made. In Rust. There's been a proliferation of X vs Y posts on the web, especially since the advent of LLM AI, so I try to resist the format. It's often e…

via Mike KreuzerNovember 16, 2024

Go Concurrency Patterns

Goroutines Channels Select Statement Wait Groups Mutex Conditions Atomic Operations Once Context Map Real Examples Goroutines The go keyword is used to start a goroutine. A goroutine is a lightweight, managed thread used by the Go runtime to run functions …

via Posts on integralistNovember 15, 2024

anarchism starts in the now: hope for a better future

there is still time

via maia blogNovember 14, 2024

Why I Will Always Be Angry About Software Engineering

Why do I bother getting angry about software? When I started writing, it came from a place of ennui — absolute despair at the amount of waste I was seeing in the technology sector since leaving university. I was paid spectacularly well, but nothing I produ…

via LudicityNovember 12, 2024

Generated by openring-rs

favicon here hometagsblogmicrobio cvtech cvgpg keys