dockerode icon indicating copy to clipboard operation
dockerode copied to clipboard

Running image in detached mode

Open Tzvetelin88 opened this issue 3 years ago • 2 comments

Hello Team, what is equivalent to: docker run -d -p 8000:8000 node-docker

I have tried:

const [data, container] = await docker.run(
      'node-docker',
      [],
      [stdoutStream, stderrStream],
      {
        Tty: false,
        WorkingDir: '/usr/app',
        name: 'test_container_1',
        Volumes: {
          '/usr/app': {},
        },
        ExposedPorts: {
          '8000/tcp': {}
        },
        HostConfig: {
          PortBindings: {
            '8000/tcp': [{
              HostPort: '8000',
            }],
          },
          AutoRemove: false,
        },
      }
    );

I read these: https://github.com/apocas/dockerode/issues/106 https://github.com/apocas/dockerode/issues/188 , but it's not clear where and how to specify detach mode?

What I'm trying to do is to run a container from an existing image, and return a response that container is running. Currently request hands on " await docker.run".

Thanks!

Tzvetelin88 avatar Aug 08 '22 14:08 Tzvetelin88

What I did further more is to use docker.createContainer. Is this the path to go in my case? Example:

 // Create temp dir to store stdout and stderr thru the life time of the container.
  const containerTempDir = fs.mkdtempSync(
    path.join(nconf.get('DATA_DIR'), 'container-')
  );
  logger.debug(`containerTempDir ${containerTempDir}`);

  // Create temp files as Write Stream to store stdout and stderr outputs.
  const tempStdout = path.join(containerTempDir, `${hat()}.stdout`);
  const tempStderr = path.join(containerTempDir, `${hat()}.stderr`);
  const stdoutStream = fs.createWriteStream(tempStdout);
  const stderrStream = fs.createWriteStream(tempStderr);

  // Give container temp unique name
  const randomName = crypto.randomBytes(8).toString('hex');
  const containerName = 'test_me_' + randomName;

  logger.debug(`Creating container with name  ${containerName}`);
  const container = await docker.createContainer(
    {
      Image: image,
      Cmd: [],
      name: containerName,
      Tty: false,
      WorkingDir: '/usr/app',
      Volumes: {
        '/usr/app': {},
      },
      ExposedPorts: {
        '443/tcp': {},
      },
      HostConfig: {
        PortBindings: {
          '443/tcp': [
            {
              HostPort: '0',
            },
          ],
        },
        AutoRemove: false,
      },
    }
  );
  logger.debug(`Container with name  ${containerName} created.`);

  logger.debug(`Staring container ${containerName}...`);
  await container.start();
  logger.debug(`Container with ${containerName} and ID: ${container.id} started!`);

  logger.debug(`Attaching stream to  ${containerName}...`);
  container.attach({stream: true, stdout: true, stderr: true}, function (err: any, stream: any) {
    container.modem.demuxStream(stream, stdoutStream, stderrStream);
  });

Tzvetelin88 avatar Aug 08 '22 19:08 Tzvetelin88

That's where I am and it's not clear to me either. For me, a call back when the container stops would be enough.

benji56 avatar Aug 14 '22 18:08 benji56