Docker: create container before running image

Change-Id: Iea4a4ec446a2df6fc299be24615bca605fca83e9
Reviewed-by: hjk <hjk@qt.io>
This commit is contained in:
hjk
2021-09-29 17:09:45 +02:00
committed by David Schulz
parent 2a96c396f9
commit 033d2c5cc3

View File

@@ -76,8 +76,8 @@
#include <QRandomGenerator> #include <QRandomGenerator>
#include <QRegularExpression> #include <QRegularExpression>
#include <QTextBrowser> #include <QTextBrowser>
#include <QToolButton>
#include <QThread> #include <QThread>
#include <QToolButton>
#include <numeric> #include <numeric>
@@ -815,25 +815,19 @@ static QString getLocalIPv4Address()
void DockerDevicePrivate::startContainer() void DockerDevicePrivate::startContainer()
{ {
QString tempFileName;
{
TemporaryFile temp("qtc-docker-XXXXXX");
temp.open();
tempFileName = temp.fileName();
}
const QString display = HostOsInfo::isWindowsHost() ? QString(getLocalIPv4Address() + ":0.0") const QString display = HostOsInfo::isWindowsHost() ? QString(getLocalIPv4Address() + ":0.0")
: QString(":0"); : QString(":0");
CommandLine dockerRun{"docker", {"run", "-i", "--cidfile=" + tempFileName, CommandLine dockerCreate{"docker", {"create",
"--rm", "-i",
"-e", QString("DISPLAY=%1").arg(display), "--rm",
"-e", "XAUTHORITY=/.Xauthority", "-e", QString("DISPLAY=%1").arg(display),
"--net", "host"}}; "-e", "XAUTHORITY=/.Xauthority",
"--net", "host"}};
#ifdef Q_OS_UNIX #ifdef Q_OS_UNIX
// no getuid() and getgid() on Windows.
if (m_data.useLocalUidGid) if (m_data.useLocalUidGid)
dockerRun.addArgs({"-u", QString("%1:%2").arg(getuid()).arg(getgid())}); dockerCreate.addArgs({"-u", QString("%1:%2").arg(getuid()).arg(getgid())});
#endif #endif
for (QString mount : qAsConst(m_data.mounts)) { for (QString mount : qAsConst(m_data.mounts)) {
@@ -847,11 +841,25 @@ void DockerDevicePrivate::startContainer()
const FilePath path = FilePath::fromUserInput(mountPath.path().mid(2)); // strip C: const FilePath path = FilePath::fromUserInput(mountPath.path().mid(2)); // strip C:
mount = '/' + lowerDriveLetter + path.path(); mount = '/' + lowerDriveLetter + path.path();
} }
dockerRun.addArgs({"-v", mount + ':' + mount}); dockerCreate.addArgs({"-v", mount + ':' + mount});
} }
dockerRun.addArgs({"--entrypoint", "/bin/sh", m_data.imageId}); dockerCreate.addArgs({"--entrypoint", "/bin/sh", m_data.imageId});
LOG("RUNNING: " << dockerCreate.toUserOutput());
QtcProcess createProcess;
createProcess.setCommand(dockerCreate);
createProcess.runBlocking();
if (createProcess.result() != QtcProcess::FinishedWithSuccess)
return;
m_container = createProcess.stdOut().trimmed();
if (m_container.isEmpty())
return;
LOG("Container via process: " << m_container);
CommandLine dockerRun{"docker", {"container" , "start", "-i", "-a", m_container}};
LOG("RUNNING: " << dockerRun.toUserOutput()); LOG("RUNNING: " << dockerRun.toUserOutput());
QPointer<QtcProcess> shell = new QtcProcess(ProcessMode::Writer); QPointer<QtcProcess> shell = new QtcProcess(ProcessMode::Writer);
connect(shell, &QtcProcess::finished, this, [this, shell] { connect(shell, &QtcProcess::finished, this, [this, shell] {
@@ -886,23 +894,6 @@ void DockerDevicePrivate::startContainer()
return; return;
} }
LOG("CHECKING: " << tempFileName);
for (int i = 0; i <= 20; ++i) {
QFile file(tempFileName);
if (file.open(QIODevice::ReadOnly)) {
m_container = QString::fromUtf8(file.readAll()).trimmed();
if (!m_container.isEmpty()) {
LOG("Container: " << m_container);
break;
}
}
if (i == 20 || !DockerPlugin::isDaemonRunning().value_or(true)) {
qWarning("Docker cid file empty.");
return; // No
}
qApp->processEvents(); // FIXME turn this for-loop into
QThread::msleep(100);
}
DockerPlugin::setGlobalDaemonState(true); DockerPlugin::setGlobalDaemonState(true);
} }