Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. How do I run docker using QProcess?
Forum Updated to NodeBB v4.3 + New Features

How do I run docker using QProcess?

Scheduled Pinned Locked Moved Unsolved General and Desktop
16 Posts 6 Posters 3.1k Views 2 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • K Offline
    K Offline
    KKyul
    wrote on last edited by
    #1
    This post is deleted!
    1 Reply Last reply
    0
    • Christian EhrlicherC Online
      Christian EhrlicherC Online
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on last edited by
      #2

      You should take a look at QProcess::setArguments(): http://doc.qt.io/qt-5/qprocess.html#setArguments

      Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
      Visit the Qt Academy at https://academy.qt.io/catalog

      1 Reply Last reply
      3
      • K Offline
        K Offline
        KKyul
        wrote on last edited by
        #3

        Arguments <<"/k" <<"docker exec -ti freesurfer bash"; -> is Okay,
        0_1527078625095_85a35c56-e72b-4057-942b-c8b6b0c0ef9d-image.png

        but

        Arguments <<"/k" <<"docker exec -ti freesurfer bash << "ls";

        0_1527078684015_67b2c38c-cd55-4072-8fd4-51a5dd69afa9-image.png

        I want the user to be able to interact with the command prompt window and send their own commands is it possible??

        Pablo J. RoginaP 1 Reply Last reply
        0
        • K KKyul

          Arguments <<"/k" <<"docker exec -ti freesurfer bash"; -> is Okay,
          0_1527078625095_85a35c56-e72b-4057-942b-c8b6b0c0ef9d-image.png

          but

          Arguments <<"/k" <<"docker exec -ti freesurfer bash << "ls";

          0_1527078684015_67b2c38c-cd55-4072-8fd4-51a5dd69afa9-image.png

          I want the user to be able to interact with the command prompt window and send their own commands is it possible??

          Pablo J. RoginaP Offline
          Pablo J. RoginaP Offline
          Pablo J. Rogina
          wrote on last edited by
          #4

          @KKyul could this old post still apply to your issue?

          Upvote the answer(s) that helped you solve the issue
          Use "Topic Tools" button to mark your post as Solved
          Add screenshots via postimage.org
          Don't ask support requests via chat/PM. Please use the forum so others can benefit from the solution in the future

          JonBJ K 2 Replies Last reply
          0
          • Pablo J. RoginaP Pablo J. Rogina

            @KKyul could this old post still apply to your issue?

            JonBJ Online
            JonBJ Online
            JonB
            wrote on last edited by
            #5

            @Pablo-J.-Rogina
            I don't really get when those authors expect that to work. They clone the calling process' stdin to pass on to the child (instead of usual redirection). But assuming your Qt app is GUI, and say launched from a desktop shortcut, it won't start with any (open) stdin to clone, will it?

            K 1 Reply Last reply
            0
            • JonBJ JonB

              @Pablo-J.-Rogina
              I don't really get when those authors expect that to work. They clone the calling process' stdin to pass on to the child (instead of usual redirection). But assuming your Qt app is GUI, and say launched from a desktop shortcut, it won't start with any (open) stdin to clone, will it?

              K Offline
              K Offline
              KKyul
              wrote on last edited by
              #6

              @JonB

              Arguments <<"/k" <<"docker exec -ti freesurfer ls -la /opt/freesurfer";

              0_1527082353776_632581e4-6803-485d-be8d-b8d54caa5fd6-image.png

              I conformed, above arguments doing well. that means, above arguments run a docker.

              "docker exec -ti freesurfer ls -la /opt/freesurfer"; -> this arguments is composed of 3 windows? command line
              (docker exec -ti freesurfer) + (opt/freesurfer) + (ls).

              1 Reply Last reply
              1
              • Pablo J. RoginaP Pablo J. Rogina

                @KKyul could this old post still apply to your issue?

                K Offline
                K Offline
                KKyul
                wrote on last edited by
                #7

                @Pablo-J.-Rogina
                Pablo-J.-Rogina

                I learned Qt 3 days....

                Arguments <<"/k" <<"docker exec -ti freesurfer" <<"export " <<"source " <<"export " <<"convert";
                CommandPrompt.startDetached("cmd", Arguments);

                Do you think is it possible syntax?? to send multiple arguments.

                JonBJ 1 Reply Last reply
                0
                • mrjjM Offline
                  mrjjM Offline
                  mrjj
                  Lifetime Qt Champion
                  wrote on last edited by
                  #8

                  Hi
                  I used the following code to run python interactively.
                  (code from https://stackoverflow.com/questions/48518442/qt-start-process-in-interactive-shell )

                  #include <QProcess>
                  #include <QString>
                  #include <QStringList>
                  #include "Windows.h"
                  
                  class QDetachableProcess 
                          : public QProcess {
                  public:
                      QDetachableProcess(QObject *parent = 0) 
                          : QProcess(parent) {
                      }
                      void detach() {
                         waitForStarted();
                         setProcessState(QProcess::NotRunning);
                      }
                  };
                  
                  int main(int argc, char *argv[]) {
                      QDetachableProcess process;
                      QString program = "cmd.exe";
                      QStringList arguments = QStringList() << "/K" << "python.exe";
                      process.setCreateProcessArgumentsModifier(
                                  [](QProcess::CreateProcessArguments *args) {
                          args->flags |= CREATE_NEW_CONSOLE;
                          args->startupInfo->dwFlags &=~ STARTF_USESTDHANDLES;
                      });
                      process.start(program, arguments);
                      process.detach();
                      return 0;
                  }
                  

                  However, im not sure how docker works as it seems it starts a linux bash inside ?
                  So im wondering which of the shells/command promts you want to be interactive.
                  The windows one, or the linux one. ( if it works thats way)

                  K 1 Reply Last reply
                  0
                  • K KKyul

                    @Pablo-J.-Rogina
                    Pablo-J.-Rogina

                    I learned Qt 3 days....

                    Arguments <<"/k" <<"docker exec -ti freesurfer" <<"export " <<"source " <<"export " <<"convert";
                    CommandPrompt.startDetached("cmd", Arguments);

                    Do you think is it possible syntax?? to send multiple arguments.

                    JonBJ Online
                    JonBJ Online
                    JonB
                    wrote on last edited by
                    #9

                    @KKyul
                    Please just tell us exactly what you would type in a terminal to run the command. For example, is it perhaps:

                    docker exec -ti freesurfer ls -la /opt/freesurfer
                    

                    or does it have quotes or what?

                    You keep writing:
                    Arguments <<"/k" ...
                    what is that /k about, and what program is this Arguments for?

                    I want to execute multiple window cmd command using a QProcess? or other function

                    What is "multiple window" all about? It means nothing to me.

                    K 1 Reply Last reply
                    1
                    • mrjjM mrjj

                      Hi
                      I used the following code to run python interactively.
                      (code from https://stackoverflow.com/questions/48518442/qt-start-process-in-interactive-shell )

                      #include <QProcess>
                      #include <QString>
                      #include <QStringList>
                      #include "Windows.h"
                      
                      class QDetachableProcess 
                              : public QProcess {
                      public:
                          QDetachableProcess(QObject *parent = 0) 
                              : QProcess(parent) {
                          }
                          void detach() {
                             waitForStarted();
                             setProcessState(QProcess::NotRunning);
                          }
                      };
                      
                      int main(int argc, char *argv[]) {
                          QDetachableProcess process;
                          QString program = "cmd.exe";
                          QStringList arguments = QStringList() << "/K" << "python.exe";
                          process.setCreateProcessArgumentsModifier(
                                      [](QProcess::CreateProcessArguments *args) {
                              args->flags |= CREATE_NEW_CONSOLE;
                              args->startupInfo->dwFlags &=~ STARTF_USESTDHANDLES;
                          });
                          process.start(program, arguments);
                          process.detach();
                          return 0;
                      }
                      

                      However, im not sure how docker works as it seems it starts a linux bash inside ?
                      So im wondering which of the shells/command promts you want to be interactive.
                      The windows one, or the linux one. ( if it works thats way)

                      K Offline
                      K Offline
                      KKyul
                      wrote on last edited by
                      #10

                      @mrjj said in How do I run docker using QProcess?:

                      The windows one, or the linux one. ( if it works thats way)

                      exactly... I want to interactive with linux shells command.

                      following code

                      Arguments <<"/k" <<"docker exec -ti freesurfer" <<"export FREESURFER_HOME=/opt/freesurfer" <<"source $FREESURFER_HOME/SetUpFreeSurfer.sh" <<"export SUBJECTS_DIR=/opt/freesurfer" <<"mri_convert sample-001.mgz sample-001.nii.gz";

                      I want above codes are processed at once.

                      1 Reply Last reply
                      0
                      • JonBJ JonB

                        @KKyul
                        Please just tell us exactly what you would type in a terminal to run the command. For example, is it perhaps:

                        docker exec -ti freesurfer ls -la /opt/freesurfer
                        

                        or does it have quotes or what?

                        You keep writing:
                        Arguments <<"/k" ...
                        what is that /k about, and what program is this Arguments for?

                        I want to execute multiple window cmd command using a QProcess? or other function

                        What is "multiple window" all about? It means nothing to me.

                        K Offline
                        K Offline
                        KKyul
                        wrote on last edited by
                        #11

                        @JonB said in How do I run docker using QProcess?:

                        I want to execute multiple window cmd command using a QProcess? or other function

                        I honestly do not know about / k It's code from the Internet....

                        I want to interactive with linux shell command.

                        1. "docker exec -ti freesurfer"
                          2)"export FREESURFER_HOME=/opt/freesurfer"
                          3)"source $FREESURFER_HOME/SetUpFreeSurfer.sh"
                          4)"export SUBJECTS_DIR=/opt/freesurfer"
                          5)"mri_convert sample-001.mgz sample-001.nii.gz";
                          I want to execute above those command at once!
                        mrjjM JonBJ 2 Replies Last reply
                        0
                        • K KKyul

                          @JonB said in How do I run docker using QProcess?:

                          I want to execute multiple window cmd command using a QProcess? or other function

                          I honestly do not know about / k It's code from the Internet....

                          I want to interactive with linux shell command.

                          1. "docker exec -ti freesurfer"
                            2)"export FREESURFER_HOME=/opt/freesurfer"
                            3)"source $FREESURFER_HOME/SetUpFreeSurfer.sh"
                            4)"export SUBJECTS_DIR=/opt/freesurfer"
                            5)"mri_convert sample-001.mgz sample-001.nii.gz";
                            I want to execute above those command at once!
                          mrjjM Offline
                          mrjjM Offline
                          mrjj
                          Lifetime Qt Champion
                          wrote on last edited by
                          #12

                          Hmm but the
                          export statements are really not parameters. its bash stuff.
                          Would it be an option to put in all in convert.sh file and
                          simply run that ?

                          K 1 Reply Last reply
                          0
                          • mrjjM mrjj

                            Hmm but the
                            export statements are really not parameters. its bash stuff.
                            Would it be an option to put in all in convert.sh file and
                            simply run that ?

                            K Offline
                            K Offline
                            KKyul
                            wrote on last edited by
                            #13

                            @mrjj
                            okay! I will do make a .sh file!! thanks alot !!

                            mrjjM 1 Reply Last reply
                            0
                            • K KKyul

                              @mrjj
                              okay! I will do make a .sh file!! thanks alot !!

                              mrjjM Offline
                              mrjjM Offline
                              mrjj
                              Lifetime Qt Champion
                              wrote on last edited by
                              #14

                              @KKyul
                              I think it will work faster as im not sure if the
                              bash inside docker will see the export correctly with QProcess.
                              However, inside .sh file, it will just work as it should be in context of the bash if i read the docs right.
                              I really ought to try Docker on windows. Pretty cool tech.

                              1 Reply Last reply
                              0
                              • K KKyul

                                @JonB said in How do I run docker using QProcess?:

                                I want to execute multiple window cmd command using a QProcess? or other function

                                I honestly do not know about / k It's code from the Internet....

                                I want to interactive with linux shell command.

                                1. "docker exec -ti freesurfer"
                                  2)"export FREESURFER_HOME=/opt/freesurfer"
                                  3)"source $FREESURFER_HOME/SetUpFreeSurfer.sh"
                                  4)"export SUBJECTS_DIR=/opt/freesurfer"
                                  5)"mri_convert sample-001.mgz sample-001.nii.gz";
                                  I want to execute above those command at once!
                                JonBJ Online
                                JonBJ Online
                                JonB
                                wrote on last edited by JonB
                                #15

                                @KKyul

                                I honestly do not know about / k It's code from the Internet....

                                OK. I'm going to guess what I think you're looking at and want.

                                You are under Windows (your Qt app, where you want to run Docker out of). The Windows shell, cmd.exe. That accepts a /k argument to remain around (as a console) after executing a command line. The command line treats the rest of the line as the command to execute first. @mrjj's code you show explicitly creates a new console in a window for the sub-process.

                                So your code would be:

                                QStringList arguments = QStringList() << "/K" << "docker exec -ti freesurfer";
                                

                                You are putting the export etc. statements in a convert.sh file (they cannot be put on the command-line). This needs executing by the docker. You have to tell it to execute that. So does docker accept a command-line e.g.

                                docker exec -ti freesurfer convert.sh
                                

                                or rather, quite probably:

                                docker exec -ti freesurfer "/bin/bash convert.sh"
                                
                                1 Reply Last reply
                                2
                                • SGaistS Offline
                                  SGaistS Offline
                                  SGaist
                                  Lifetime Qt Champion
                                  wrote on last edited by
                                  #16

                                  Hi,

                                  As an additional test, since you have an interactive session, why not use QProcess::write with the lines you would like to execute ?

                                  Interested in AI ? www.idiap.ch
                                  Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                                  1 Reply Last reply
                                  0

                                  • Login

                                  • Login or register to search.
                                  • First post
                                    Last post
                                  0
                                  • Categories
                                  • Recent
                                  • Tags
                                  • Popular
                                  • Users
                                  • Groups
                                  • Search
                                  • Get Qt Extensions
                                  • Unsolved