Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

How do I run docker using QProcess?



  • This post is deleted!

  • Qt Champions 2019

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



  • 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??



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



  • @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?



  • @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).



  • @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.


  • Lifetime Qt Champion

    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)



  • @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.



  • @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.



  • @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!

  • Lifetime Qt Champion

    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 ?



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


  • Lifetime Qt Champion

    @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.



  • @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"
    

  • Lifetime Qt Champion

    Hi,

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


Log in to reply