accessing a container from the cli?

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
9,702
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
57
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
 

Aviatorpaal

Dabbler
Joined
Mar 2, 2022
Messages
21
How do I run a command as a specific user inside a K3S Kubernetes container (all the layers :eek:)? These two commands are doing exactly what I want to be done when run individually, in sequence:

Code:
k3s kubectl exec -n ix-nextcloud -i -t nextcloud-6956776979-grgvw -c nextcloud -- /bin/bash
su -s /bin/bash -c "./occ files:scan --all" www-data


However, I would like to automate this, and therefore need to combine them into one script for TrueNAS Scale to run
 

Arwen

MVP
Joined
May 17, 2014
Messages
3,600
Short answer: HereIS

Longer answer, script with this:

Code:
#!/bin/bash
k3s kubectl exec -n ix-nextcloud -i -t nextcloud-6956776979-grgvw -c nextcloud -- /bin/bash <<HereIS
su -s /bin/bash -c "./occ files:scan --all" www-data
new command...
next command...
exit
HereIS
exit

The word "HereIS" can be anything unique. It is basically a wrapper for standard input to the following commands.

The first "exit" is exiting out of the container, and the second is exiting the SCALE bash script.

It's been a while since I've coded something like this... so test with something harmless at first.
 

Aviatorpaal

Dabbler
Joined
Mar 2, 2022
Messages
21
Short answer: HereIS

Longer answer, script with this:

Code:
#!/bin/bash
k3s kubectl exec -n ix-nextcloud -i -t nextcloud-6956776979-grgvw -c nextcloud -- /bin/bash <<HereIS
su -s /bin/bash -c "./occ files:scan --all" www-data
new command...
next command...
exit
HereIS
exit

The word "HereIS" can be anything unique. It is basically a wrapper for standard input to the following commands.

The first "exit" is exiting out of the container, and the second is exiting the SCALE bash script.

It's been a while since I've coded something like this... so test with something harmless at first.
Awesome - thanks for your time, much appreciated.

It appears to be working, but the following warning/error is thrown:
Unable to use a TTY - input is not a terminal or the right kind of file

Edit: rookie mistake - I was requesting an interactive TTY. Removed the "-t" and it now works as intended. Awesome!
 
Last edited:

Arwen

MVP
Joined
May 17, 2014
Messages
3,600
Awesome - thanks for your time, much appreciated.

It appears to be working, but the following warning/error is thrown:
Unable to use a TTY - input is not a terminal or the right kind of file

Edit: rookie mistake - I was requesting an interactive TTY. Removed the "-t" and it now works as intended. Awesome!
Glad I was able to help.

Unix is extremely flexible and has a lot of odd things embedded in it.
 
Top