Skip to content

Instantly share code, notes, and snippets.

@sourishkrout
Created April 22, 2024 23:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sourishkrout/5129f07b3acc00f44b7b04e741308298 to your computer and use it in GitHub Desktop.
Save sourishkrout/5129f07b3acc00f44b7b04e741308298 to your computer and use it in GitHub Desktop.

File name: gist.md

Session: 01HW40094TDKV7141FP619GGHG


▶️ Generated by Runme

Share your own terminal sessions, it's free and open source. Click here to learn more.

runme
id version document session
01HW3ZXNBSHJ4G57A97DC98XGE
v3
relativePath
gist.md
id updated
01HW40094TDKV7141FP619GGHG
2024-04-22 18:51:13 -0400

Runme Language Support

By default Runme can run everything that is also installed on your machine.

Shebang is a versatile tool designed to execute scripts written in various scripting languages including Shell, Perl, Python, Ruby, Node.js, and more. Runme integrates Shebang to enable users to run the script of their choice directly from the Markdown file in their preferred programming language.

Let's learn how to use multiple programming languages to interact with your containers!

In this example we will write a simple script in different programming languages that lists your running containers.

💡 Before starting, ensure you have the following installed in your machine:

  • Docker 🐳
  • Python 🐍 (for Python example)
  • GCP ☁️

Ensure docker is up and running

Run the following check, just to ensure you have Docker up and running

#!/bin/bash

# Check if Docker is installed
if ! command -v docker &> /dev/null
then
    echo "Docker is not installed."
    exit
fi

# Check if Docker daemon is running
if ! docker info &> /dev/null
then
    echo "Docker daemon is not running. ❌"
    exit
fi

echo "Docker is installed and running. ✅"


# Ran on 2024-04-22 18:50:01-04:00 for 418ms exited with 0
Docker is installed and running. ✅

Ensure you have a list one container to list, if you don't have one, you can start a nginx container by running the following command:

docker rm -f my_runme_demo_container
docker run -d --name my_runme_demo_container -p 8080:80 nginx

# Ran on 2024-04-22 18:50:05-04:00 for 653ms exited with 0
my_runme_demo_container
7832eb3024194ee5470d1368efaee699305785ccdb63f37977bb36badb8dc527

Python 🐍

Requirements

  • Ensure you have python installed
  • Create a virtual env
  • Install the docker and prettytable packages
py***n3 -m venv .venv
source .venv/bin/activate
pip3 install docker prettytable

# Ran on 2024-04-22 18:50:08-04:00 for 1.57s exited with 0
Requirement already satisfied: docker in ./.venv/lib/py******12/site-packages (7.0.0)
Requirement already satisfied: prettytable in ./.venv/lib/py******12/site-packages (3.10.0)
Requirement already satisfied: pa*****ng>=14.0 in ./.venv/lib/py******12/site-packages (from docker) (24.0)
Requirement already satisfied: re****ts>=2.26.0 in ./.venv/lib/py******12/site-packages (from docker) (2.31.0)
Requirement already satisfied: ur***b3>=1.26.0 in ./.venv/lib/py******12/site-packages (from docker) (2.2.1)
Requirement already satisfied: wcwidth in ./.venv/lib/py******12/site-packages (from prettytable) (0.2.13)
Requirement already satisfied: ch**************er<4,>=2 in ./.venv/lib/py******12/site-packages (from re****ts>=2.**.0->docker) (3.3.2)
Requirement already satisfied: idna<4,>=2.5 in ./.venv/lib/py******12/site-packages (from re****ts>=2.**.0->docker) (3.7)
Requirement already satisfied: ce***fi>=2017.4.17 in ./.venv/lib/py******12/site-packages (from re****ts>=2.**.0->docker) (2024.2.2)

Now you have all the requirements ready, run the following Python script to get a list of running containers in a nice table format.

import docker
from prettytable import PrettyTable

def list_running_containers():
    client = docker.from_env()
    containers = client.containers.list()

    if containers:
        table = PrettyTable(["Container ID", "Name", "Image", "Status"])
        for container in containers:
            ta*********ow([co********id[:12], container.name, container.attrs['Config']['Image'], container.status])
        print("Running containers:")
        print(table)
    else:
        print("No running containers found.")

if __name__ == "__main__":
    list_running_containers()

# Ran on 2024-04-22 18:50:13-04:00 for 258ms exited with 0
Running containers:
+--------------+--------------------------------+-----------------------------------+---------+
| Container ID |              Name              |               Image               |  Status |
+--------------+--------------------------------+-----------------------------------+---------+
| 78********19 |    my_runme_demo_container     |               nginx               | running |
| 27********a4 | da**************************6b | re**************io/en**ne:v0***.0 | running |
+--------------+--------------------------------+-----------------------------------+---------+

GCP ☁️

gcloud compute ssh --zone "us*********-c" "gk***********************l-78*********ab" --project "runme-ci"

# Ran on 2024-04-22 18:50:28-04:00 for 2.**4s
so***********************2-de**********************ab ~ $ free -m
               total        used        free      shared  buff/cache   available
Mem:            39************************************54        1240        3104
Swap:              0           0           0
so***********************2-de**********************ab ~ $ 
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment