Dockerode: Docker run and detach

Created on 21 Nov 2014  路  4Comments  路  Source: apocas/dockerode

Hi,

I am trying to docker.run an image in detached mode. In docker command I am using:

docker run --detach=true  --name api -t registry:5000/api:latest

I can then see in "STATUS" column (docker ps -a on the host) that the container is Up and running.

Trying to do the same thing with the API

docker.run("registry:5000/api:latest",null , null, 
    {   
        detach: true,
        name: "api"
    }, 
    function (err, data, container) {
            console.log("callback!")
    }
);

When I check with docker ps -a the container is up and running but It never goes in the callback even though detach = true.

I tried to add the option Tty: false to the list of options above
It goes in the callback but the status of the container is "Exited (0) 4 seconds ago "

Any idea to keep my container up and running in detach mode with the API ?

Thanks

question

Most helpful comment

Sure there is.
Just shouldn't be using run for this use case, run method has a very specific container lifecycle.

Do your own container lifecycle.
Basically:

  1. Create container
  2. Start container (use the callback to know when it started)
  3. wait for it to end and so on...

All 4 comments

It seems that the Docker.prototype.run (from docker.js) method attach to the new created container and thus, wait until the container exit which is not my expected behavior. When a container is started, I need to be notified it has started, that's it.

As a work around, I have commented out all "container.attach" method line 390. Is there a better approach ?

     docker.run(requestedImage, null, null, {Tty: false },
         function(err, data, container) {
        }).on('container', function(container) {
            var options = {
                PortBindings: JSON.stringify(target.service.ports.PortBindings),
                Binds: JSON.stringify(target.service.volumes)               
            }
            container.start(options, function (err, data) {
                    if(!err) {
                        console.log("container started")

                    } else {
                        console.log("container error")              
                    }
                }   
        })

Thank you

src: http://stackoverflow.com/questions/17776415/what-is-equivalent-remote-api-command-to-docker-run-d

Sure there is.
Just shouldn't be using run for this use case, run method has a very specific container lifecycle.

Do your own container lifecycle.
Basically:

  1. Create container
  2. Start container (use the callback to know when it started)
  3. wait for it to end and so on...

Perfect thank you very much

Was this page helpful?
0 / 5 - 0 ratings