Navigation

    Logo
    • Register
    • Login
    • Search
    • Recent
    • Tags
    • Unread
    • Categories
    • Unreplied
    • Popular
    • GitHub
    • Docu
    • Hilfe
    1. Home
    2. Deutsch
    3. Einsteigerfragen
    4. [gelöst] Docker (VM) Container mit ioB (LXC) restart

    NEWS

    • Wir empfehlen: Node.js 22.x

    • Neuer Blog: Fotos und Eindrücke aus Solingen

    • ioBroker goes Matter ... Matter Adapter in Stable

    [gelöst] Docker (VM) Container mit ioB (LXC) restart

    This topic has been deleted. Only users with topic management privileges can see it.
    • OliverIO
      OliverIO @australien last edited by OliverIO

      @australien

      portainer hat eine api mit der du alles steuern kannst dazu musst du die als erstes ein access token holen

      https://docs.portainer.io/api/examples#authenticate-against-the-api-using-the-admin-account

      mit dem folgenden url schema und dem access token bspw dann auch ein container neu starten

      POST 
      http://10.68.xx.zzz:9000/api/endpoints/1/docker/containers/<container_id>/restart
      Authorization: Bearer <jwt>
      

      portainer reicht sozusagen die docker api mit http://10.68.xx.zzz:9000/api/endpoints/1/docker/ durch
      https://docs.docker.com/reference/api/engine/version/v1.41/#tag/Container/operation/ContainerRestart

      A 1 Reply Last reply Reply Quote 0
      • A
        australien @OliverIO last edited by

        @oliverio jetzt habe ich mir das angeschaut, leider sehe ich nur einen großen Bahnhof.
        Wie kann ich das in ioB umsetzten?
        Genauer gesagt, ein Container in portainer.io schmiert hinundwieder ab, dieser soll dann neu gestartet werden. Ersichtlich ist das insovern, das eingewisser DP keinen gültigen Wert liefert.

        OliverIO 1 Reply Last reply Reply Quote 0
        • OliverIO
          OliverIO @australien last edited by

          @australien

          Zunächst, der Fehler im Container sollte eigentlich als erstes analysiert und behoben werden, so dass solche Schritte nicht notwendig sein müssten. Das ist ja mehr oder weniger nichts anderes wie der Fußtritt gegen das Gerät.

          Ich hab mal ChatGPT gefragt, eine fertige Funktion für dich zu formulieren:

          // Restart a Docker container via Portainer API
          // Requires Node 18+ (native fetch) or node-fetch polyfill
          
          const PORTAINER_URL = "http://portainer.example.com:9000"; // deine Portainer-URL
          const API_TOKEN = "xxx_your_portainer_api_token_xxx";       // dein API-Token
          const ENDPOINT_ID = 1;                                      // meistens 1
          const CONTAINER_ID = "mein_container";                      // Container Name oder ID
          
          async function restartContainer() {
            const url = `${PORTAINER_URL}/api/endpoints/${ENDPOINT_ID}/docker/containers/${CONTAINER_ID}/restart`;
          
            try {
              const res = await fetch(url, {
                method: "POST",
                headers: {
                  "Authorization": `Bearer ${API_TOKEN}`,
                  "Content-Type": "application/json"
                }
              });
          
              if (res.ok) {
                console.log(`✅ Container "${CONTAINER_ID}" wurde erfolgreich neu gestartet.`);
              } else {
                const text = await res.text();
                console.error(`❌ Fehler: ${res.status} - ${text}`);
              }
            } catch (err) {
              console.error("⚠️ Netzwerkfehler:", err);
            }
          }
          
          restartContainer();
          
          

          Die Funktion sieht gut aus, hab sie aber selbst nicht getestet.
          Du musst eigentlich nur die ersten paar Zeilen ausfüllen.

          PORTAINER_URL Sollte eigentlich klar sein, die URL, mit der du den Container bei dir aufrufst.
          API_TOKEN den Token musst du dir im portainer erzeugen unter oben rechts auf deinen Anmeldenamen, dann mein myaccount auswählen.
          Da gibt es dann einen Abschnitt, wo du dir das erzeugen lassen kannst.
          CONTAINER_ID diese findest du im portainer auf der Statusseite des Containers ganz oben

          A 1 Reply Last reply Reply Quote 0
          • A
            australien @OliverIO last edited by

            @oliverio danke für Deine Hilfe!

            Ich habe mich nun auch etwas mit KI und dem Thema beschäftigt.
            Leider kann ich meine EndpointID nirgends wo finden und wollte dies mit folgendem finden lassen:

            const https = require('https');
            
            // Konfiguration
            const PORTAINER_HOST = '10.68.xx.yy';
            const PORTAINER_PORT = 9443;
            const API_TOKEN = 'XxXxXxXx'; // Hier deinen gültigen Token eintragen
            const ENDPOINT_ID = 1;
            const CONTAINER_ID = 'Cont_ID';
            
            // Funktion, um Container neu zu starten
            function restartContainer() {
                const options = {
                    hostname: PORTAINER_HOST,
                    port: PORTAINER_PORT,
                    path: `/api/endpoints/${ENDPOINT_ID}/docker/containers/${CONTAINER_ID}/restart`,
                    method: 'POST',
                    headers: {
                        "Authorization": `Bearer ${API_TOKEN}`,
                        "Content-Type": "application/json"
                    },
                    rejectUnauthorized: false // Self-signed Zertifikat akzeptieren
                };
            
                const req = https.request(options, (res) => {
                    let data = '';
                    res.on('data', chunk => data += chunk);
                    res.on('end', () => {
                        if (res.statusCode >= 200 && res.statusCode < 300) {
                            console.log(`✅ Container "${CONTAINER_ID}" wurde erfolgreich neu gestartet.`);
                        } else {
                            console.error(`❌ Fehler beim Neustart: ${res.statusCode} - ${data}`);
                        }
                    });
                });
            
                req.on('error', (err) => {
                    console.error(`⚠️ Netzwerkfehler: ${err}`);
                });
            
                req.end();
            }
            
            // Direkt beim Start ausführen (optional)
            restartContainer();
            
            

            jetzt kann ich Tokens erstellen, aber diese werden sofort immer als falsch identifiziert.
            ich gehe folgenden Weg:
            User - my account - access token

            meine Portainer Vers: Community Edition 2.33.0.LTS

            OliverIO 1 Reply Last reply Reply Quote 0
            • OliverIO
              OliverIO @australien last edited by OliverIO

              @australien

              ja da hat sich die ki verschwurbelt
              hier eine neue skript version. diesmal habe ich sie selbst ausprobiert
              ich habe mal meine daten drin gelassen, damit du ein gefühl bekommst was da drin stehen könnte.
              das login wurde nun auf userid/passwort umgestellt.
              die endpoint id lass bitte auf 1, entweder ist das die api version oder die environment id, da du wahrscheinlich nur ein system mit portainer administriest, wird das auch bei dir 1 sein.

              // Nur Node http Modul, ganz einfach
              const http = require('http');
              
              // Konfiguration:
              const PORTAINER_HOST = '192.168.1.40';
              const PORTAINER_PORT = 9100;
              const PORTAINER_USERNAME = 'admin';
              const PORTAINER_PASSWORD = 'adminadmin';
              const PORTAINER_ENDPOINT_ID = '1';
              const PORTAINER_CONTAINER_ID = '681ada44f1d73fcc8645c8b623d7f724e860632ca9bc6bbb9e9bf1f06451e32d';
              const PORTAINER_RESTART_TIMEOUT = '10';
              
              function httpRequest(path, method, headers, body, callback) {
                  const options = {
                      hostname: PORTAINER_HOST,
                      port: PORTAINER_PORT,
                      path,
                      method,
                      headers,
                  };
              
                  const req = http.request(options, (res) => {
                      let data = '';
                      res.on('data', (chunk) => (data += chunk));
                      res.on('end', () => {
                          callback(null, { statusCode: res.statusCode, body: data });
                      });
                  });
              
                  req.on('error', (err) => callback(err));
              
                  if (body) req.write(body);
                  req.end();
              }
              
              function loginAndGetJWT(cb) {
                  const body = JSON.stringify({ Username: PORTAINER_USERNAME, Password: PORTAINER_PASSWORD });
                  httpRequest(
                      '/api/auth',
                      'POST',
                      {
                          'Content-Type': 'application/json',
                          'Content-Length': Buffer.byteLength(body),
                      },
                      body,
                      (err, res) => {
                          if (err) return cb(err);
                          if (res.statusCode !== 200) return cb(new Error('Login fehlgeschlagen: ' + res.body));
                          let data;
                          try {
                              data = JSON.parse(res.body);
                          } catch (e) {
                              return cb(e);
                          }
                          if (!data.jwt) return cb(new Error('Kein JWT im Response.'));
                          cb(null, data.jwt);
                      }
                  );
              }
              
              function restartContainer(jwt, cb) {
                  const path =
                      `/api/endpoints/${PORTAINER_ENDPOINT_ID}/docker/containers/${PORTAINER_CONTAINER_ID}/restart` +
                      `?t=${PORTAINER_RESTART_TIMEOUT}`;
              
                  httpRequest(
                      path,
                      'POST',
                      { Authorization: 'Bearer ' + jwt },
                      null,
                      (err, res) => {
                          if (err) return cb(err);
                          if (res.statusCode !== 204) return cb(new Error('Restart fehlgeschlagen: ' + res.body));
                          cb(null);
                      }
                  );
              }
              
              // Ablauf:
              loginAndGetJWT((err, jwt) => {
                  if (err) return console.error('Login Fehler:', err.message);
                  restartContainer(jwt, (err2) => {
                      if (err2) return console.error('Restart Fehler:', err2.message);
                      console.log(`✅ Container ${PORTAINER_CONTAINER_ID} neu gestartet.`);
                  });
              });
              
              
              
              A 1 Reply Last reply Reply Quote 0
              • A
                australien @OliverIO last edited by australien

                @oliverio Danke, jetzt hab ich es hin bekommen, obwohl es erst funktioniert hat, als ich als EndpointId 3 eingegeben hatte.

                Jetzt noch eine andere Frage dazu, gibt es einen Möglichkeit den Status (running) des containers abzufragen?

                OliverIO 1 Reply Last reply Reply Quote 0
                • OliverIO
                  OliverIO @australien last edited by

                  @australien

                  Portainer spiegelt dir die docker api nach draußen.
                  Daher kannst du alle api Endpunkte die docker bereitstellt in dieser Art abfragen.

                  Diese Information dürfte hier sein

                  https://docs.docker.com/reference/api/engine/version/v1.51/#tag/Container/operation/ContainerInspect

                  Das mit dem endpoint id ist interessant. Wusste ich auch nicht

                  A 1 Reply Last reply Reply Quote 0
                  • A
                    australien @OliverIO last edited by australien

                    @oliverio habe das jetzt so gelöst.
                    Wenn der Container nicht läuft, wird er restarted.

                    Danke nocheinmal für deine Hilfe!

                    const http = require('http');
                    
                    // Konfiguration
                    const PORTAINER_HOST = '10.68.xx.yy';
                    const PORTAINER_PORT = 9000;
                    const PORTAINER_USERNAME = 'admin';
                    const PORTAINER_PASSWORD = 'passwort';
                    const PORTAINER_ENDPOINT_ID = '3';      // standard sollte 1 sein, ersichtlich in der url yy:9000/#!/3/docker/containers
                    const PORTAINER_CONTAINER_ID = 'fbb9d130592c7edb96a4a71dc067721c1989bb3a27df';
                    const PORTAINER_RESTART_TIMEOUT = '10';
                    const INTERVAL_MINUTES = 10;
                    
                    const DEBUG = false; // <-- true = loggt alles, false = keine Logs
                    
                    function debugLog(...args) {
                        if (DEBUG) console.log(...args);
                    }
                    
                    function httpRequest(path, method, headers, body, callback) {
                        const options = {
                            hostname: PORTAINER_HOST,
                            port: PORTAINER_PORT,
                            path,
                            method,
                            headers,
                        };
                    
                        const req = http.request(options, (res) => {
                            let data = '';
                            res.on('data', chunk => data += chunk);
                            res.on('end', () => callback(null, { statusCode: res.statusCode, body: data }));
                        });
                    
                        req.on('error', err => callback(err));
                        if (body) req.write(body);
                        req.end();
                    }
                    
                    function loginAndGetJWT(cb) {
                        const body = JSON.stringify({ Username: PORTAINER_USERNAME, Password: PORTAINER_PASSWORD });
                        httpRequest('/api/auth', 'POST', {
                            'Content-Type': 'application/json',
                            'Content-Length': Buffer.byteLength(body),
                        }, body, (err, res) => {
                            if (err) return cb(err);
                            if (res.statusCode !== 200) return cb(new Error('Login fehlgeschlagen: ' + res.body));
                            let data;
                            try { data = JSON.parse(res.body); } catch (e) { return cb(e); }
                            if (!data.jwt) return cb(new Error('Kein JWT im Response.'));
                            debugLog('✅ Login erfolgreich, JWT erhalten.');
                            cb(null, data.jwt);
                        });
                    }
                    
                    function restartContainer(jwt, cb) {
                        const path = `/api/endpoints/${PORTAINER_ENDPOINT_ID}/docker/containers/${PORTAINER_CONTAINER_ID}/restart?t=${PORTAINER_RESTART_TIMEOUT}`;
                        httpRequest(path, 'POST', { Authorization: 'Bearer ' + jwt }, null, (err, res) => {
                            if (err) return cb(err);
                            if (res.statusCode !== 204) return cb(new Error('Restart fehlgeschlagen: ' + res.body));
                            debugLog(`✅ Container ${PORTAINER_CONTAINER_ID} neu gestartet.`);
                            cb(null);
                        });
                    }
                    
                    function checkAndRestartContainer(jwt) {
                        const path = `/api/endpoints/${PORTAINER_ENDPOINT_ID}/docker/containers/${PORTAINER_CONTAINER_ID}/json`;
                        debugLog('Requesting:', path);
                    
                        httpRequest(path, 'GET', { Authorization: 'Bearer ' + jwt }, null, (err, res) => {
                            if (err) return debugLog('Status Fehler:', err.message);
                    
                            let data;
                            try { data = JSON.parse(res.body); } 
                            catch (e) { return debugLog('Parsing Fehler:', e.message); }
                    
                            if (!data.State || data.State.Running !== true) {
                                debugLog('Container läuft nicht. Starte neu...');
                                restartContainer(jwt, (err2) => {
                                    if (err2) return debugLog('Restart Fehler:', err2.message);
                                });
                            } else {
                                debugLog(`✅ Container ${PORTAINER_CONTAINER_ID} läuft normal.`);
                            }
                        });
                    }
                    
                    // Ablauf alle 10 Minuten
                    loginAndGetJWT((err, jwt) => {
                        if (err) return console.error('Login Fehler:', err.message);
                    
                        // Sofort prüfen
                        checkAndRestartContainer(jwt);
                    
                        // Intervall setzen
                        setInterval(() => checkAndRestartContainer(jwt), INTERVAL_MINUTES * 60 * 1000);
                    });
                    
                    
                    OliverIO 2 Replies Last reply Reply Quote 0
                    • OliverIO
                      OliverIO @australien last edited by

                      @australien

                      super,
                      danke das du deinen code zum abschluss nochmal gepostet hast,
                      dann können andere den wiederverwenden und ggfs erweitern.

                      1 Reply Last reply Reply Quote 1
                      • OliverIO
                        OliverIO @australien last edited by

                        @australien
                        mach bitte bei endpoint id nochmal einen kommentar hin, das man das ausprobieren muss bzw evtl so nachgeschaut werden kann

                        ae8ef6e4-67a1-4be1-a350-e378a90b90fa-image.png

                        kannst du das bei dir verifizieren? da müsste dann bei dir eine 3 stehen

                        A 1 Reply Last reply Reply Quote 0
                        • A
                          australien @OliverIO last edited by australien

                          @oliverio bei mir steht tatsächlich 3

                          60:9000/#!/3/docker/containers
                          
                          OliverIO 1 Reply Last reply Reply Quote 0
                          • OliverIO
                            OliverIO @australien last edited by OliverIO

                            @australien

                            ok dann ist es die id des environments
                            also das was man am anfang, nach dem login im portainer auswählt.

                            A 1 Reply Last reply Reply Quote 0
                            • A
                              australien @OliverIO last edited by

                              @oliverio hab es als Kommentar hinzugefügt

                              1 Reply Last reply Reply Quote 1
                              • First post
                                Last post

                              Support us

                              ioBroker
                              Community Adapters
                              Donate

                              812
                              Online

                              32.0k
                              Users

                              80.5k
                              Topics

                              1.3m
                              Posts

                              3
                              15
                              202
                              Loading More Posts
                              • Oldest to Newest
                              • Newest to Oldest
                              • Most Votes
                              Reply
                              • Reply as topic
                              Log in to reply
                              Community
                              Impressum | Datenschutz-Bestimmungen | Nutzungsbedingungen
                              The ioBroker Community 2014-2023
                              logo