Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Docker run and detach #106

Closed
perpective2410 opened this issue Nov 21, 2014 · 5 comments
Closed

Docker run and detach #106

perpective2410 opened this issue Nov 21, 2014 · 5 comments
Labels

Comments

@perpective2410
Copy link

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

@perpective2410
Copy link
Author

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

@apocas
Copy link
Owner

apocas commented Nov 27, 2014

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

@apocas apocas closed this as completed Nov 27, 2014
@apocas
Copy link
Owner

apocas commented Nov 27, 2014

@perpective2410
Copy link
Author

Perfect thank you very much

@BelkeriSai
Copy link

Check the examples, like: https://github.com/apocas/dockerode/blob/master/examples/exec_running_container.js

How can we return the Container ID if the whole code in the above example was part of a function and that function is called to Spin up a container?

If not the Container Id, Then at least some status code would do.

Please help.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants