Important: Please read the Qt Code of Conduct -

How do I run docker using QProcess?

  • This post is deleted!

  • Lifetime Qt Champion

    You should take a look at QProcess::setArguments():

  • Arguments <<"/k" <<"docker exec -ti freesurfer bash"; -> is Okay,


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


    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";


    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

    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

    I used the following code to run python interactively.
    (code from )

    #include <QProcess>
    #include <QString>
    #include <QStringList>
    #include "Windows.h"
    class QDetachableProcess 
            : public QProcess {
        QDetachableProcess(QObject *parent = 0) 
            : QProcess(parent) {
        void detach() {
    int main(int argc, char *argv[]) {
        QDetachableProcess process;
        QString program = "cmd.exe";
        QStringList arguments = QStringList() << "/K" << "python.exe";
                    [](QProcess::CreateProcessArguments *args) {
            args->flags |= CREATE_NEW_CONSOLE;
            args->startupInfo->dwFlags &=~ STARTF_USESTDHANDLES;
        process.start(program, arguments);
        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/" <<"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/"
      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 file and
    simply run that ?

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

  • Lifetime Qt Champion

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

    or rather, quite probably:

    docker exec -ti freesurfer "/bin/bash"

  • Lifetime Qt Champion


    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