Jerome
Jerome

Reputation: 1192

How to manage Docker container with dockerode

I'm trying to do a Meteor app to start/stop my Docker containers. I founded dockerode

So I have installed it with meteor add ongoworks:dockerode and with meteor npm install dockerode and if I check in the package file there is dockerode.

But I'm facing a problem and I think it's pretty basic.
I open a consol and I start my 2 containers with:

docker start 6684787338a7 (it contains a Meteor blog running on localhost:3000)
docker start 513cfc090925 (it contains MongoDB)

Then on my computer I start my Meteor app "controlContainers" on localhost:3001 and I this is my JS + HTML.

Am I creating the docker object wrong ? Or someone could help me to figure out what I'm doing wrong ? Because nothing happend when I start or stop I can do docker ps -aand there still has my 2 containers..

import { Template } from 'meteor/templating';

import './controlPanel.html';



Template.controlPanel.onCreated(function controlPanelOnCreated() {
    console.log("controlPanel is ready to work for you Mister Wayne");
    Docker = require('dockerode');
/* create Docker object */
   docker = new Docker({host: 'http://127.0.0.1', port: 3000});
/* create 2 var that are my containers running in docker */
   meteorCtn = docker.getContainer('6684787338a7');
   mongoCtn = docker.getContainer('513cfc090925');
/* I don't really understand but it's on the Dockerode page */
   meteorCtn.defaultOptions.start.Binds = ["/tmp:/tmp:rw"];
   mongoCtn.defaultOptions.start.Binds = ["/tmp:/tmp:rw"];
});


Template.controlPanel.events({
  'click #stopAll'(event){
    /* Display object containers */
      console.log("docker:"); console.log(docker);
      console.log("meteorCtn:"); console.log(meteorCtn);
/* list all container and show the length  */
    docker.listContainers(function (err, containers) {
  /*    containers.forEach(function (containerInfo) {
        docker.getContainer(containerInfo.Id).stop(cb);
      }); */
      console.log("containers:  " + containers.length);
      console.log("err: " + err);
    });
  },
});
<template name="controlPanel">
  <p>Start, stop and pause containers from this website</p>
  <input type="button" id="stopAll" value="Stop all containers">
</template>

Upvotes: 0

Views: 1144

Answers (1)

Jerome
Jerome

Reputation: 1192

So if someone was working on it or has problems like mine the solution is:

Be sure that the Dockerode code run on the SERVER SIDE ! (it was my mistake)

And you need to mount a volume volumes:     - /var/run/docker.sock:/var/run/docker.sock then when you create the docker object you can do : docker = new Docker({socketPath: '/var/run/docker.sock'});

Upvotes: 1

Related Questions