Register for the iXsystems Community to get an ad-free experience

accessing a container from the cli?

Western Digital Drives - The Preferred Drives of FreeNAS and TrueNAS CORE

nktech1135

Dabbler
Joined
Feb 26, 2021
Messages
18
Hi all.
I was wondering how one would go about accessing a container via the cli if logged in via ssh?
Is there a cubernetes command for this? or some other method?

Thanks.
 

sretalla

Powered by Neutrality
Moderator
Joined
Jan 1, 2016
Messages
7,729
https://www.truenas.com/docs/scale/apps/usingapps/ said:
Accessing the Shell in an Active Container
To access the shell in an active container, first identify the namespace and pod for the container. In the Scale UI, go to System Settings > Shell to begin entering commands:

  1. View container namespaces: k3s kubectl get namespaces.
  2. View pods by namespace: k3s kubectl get -n <NAMESPACE> pods.
  3. Access container shell: k3s kubectl exec -n <NAMESPACE> --stdin --tty <POD> -- /bin/bash.
 

nktech1135

Dabbler
Joined
Feb 26, 2021
Messages
18
Thanks. that's what i needed. I'm running these commands via ssh because the shell in the UI doesn't work with screen readers. Plus, i generally prefer ssh anyway.
 

Heavy

Explorer
Joined
Aug 12, 2021
Messages
55
Sorry for re-opening this but I updated heavyscript to handle the process of finding your container(s) and opening a shell, as explained by @sretalla I thought it would come in handy for those who are not well versed in kubernetes..

Source code:

Instructions

1. Type in
Code:
bash heavy_script.sh


1660948176665.png


2. Select Option 9
1660948545739.png



3. Type the number that corresponds with the application you want to shell into
1660948322668.png



4. If there are multiple containers for that application, it will open another menu to select which one you want, otherwise it will default to the only container available
1660948379386.png


5. Choose either option 1 or 2
1660948420954.png


And that is it, if there is a shell available, it will try /bin/bash first, then fallback to /bin/sh if bash is not available.
Afterwards, type exit, to exit the shell.
1660948473788.png
 

alugowski

Dabbler
Joined
May 8, 2019
Messages
32
I wrote a quick script based on strella's link that works for single-container pods:

Code:
#!/usr/bin/env bash

# Get the container namespace.
# This command lists all namespaces:
# k3s kubectl get namespaces
# TrueNAS namespaces for Docker containers are just the container name prefixed with 'ix-'
NAMESPACE="ix-$1"
shift

# view pods in namespace:
# k3s kubectl get -n <NAMESPACE> pods
# returns a header line then the container pod is in the first column of the next line. Assume there is only one pod.
POD="$(k3s kubectl get -n $NAMESPACE pods | tail -n 1 | awk '{print $1}')"

# Run a shell in the container:
if [[ $# -eq 0 ]]; then
    COMMAND="/bin/sh"
    k3s kubectl exec -n $NAMESPACE --stdin --tty $POD -- $COMMAND
else
    # Forward remaining parameters to the command
    k3s kubectl exec -n $NAMESPACE --stdin --tty $POD -- $@
fi


For a shell (/bin/sh):
Code:
sudo ./container-shell.sh <appname>


or any arbitrary command, including a different shell:
Code:
sudo ./container-shell.sh <appname> cmd arg1 arg2 arg3 etc
 
Top