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

AttachStdout not working in StartContainer #310

Closed
ebuchman opened this issue Jun 24, 2015 · 2 comments
Closed

AttachStdout not working in StartContainer #310

ebuchman opened this issue Jun 24, 2015 · 2 comments

Comments

@ebuchman
Copy link

I have an existing data-container. I'd like to create a new container with volumes from the data-container and stomp around in it interactively, or at the very least run a command and get to see Stdout. Unfortunately attaching Stdout, or anything else I've tried, doesn't work. Some code:

         // vars that exist ...
        // var interactive bool
        // var args []string
        // var DockerClient *docker.Client
        volumesFrom := "existing_data_container"
        opts := docker.CreateContainerOptions{
                Name: "exec_" + volumesFrom,
                Config: &docker.Config{
                        Image:        "myimage",
                        User:         "root",
                        AttachStdout: true,
                        AttachStderr:    true,
                        NetworkDisabled: true,
                },
                HostConfig: &docker.HostConfig{
                        VolumesFrom: []string{volumesFrom},
                },
        }
        if interactive {
                opts.Config.AttachStdin = true
                opts.Config.OpenStdin = true
                opts.Config.Cmd = []string{"/bin/bash"}
        } else {
                opts.Config.Cmd = args
        }
        cont, err := DockerClient.CreateContainer(opts)
        if err != nil {
                return err 
        }   
        id_main := cont.ID
        logger.Infoln("Data Container ID: " + id_main)

        // start the container
        if err := DockerClient.StartContainer(id, opts.HostConfig); err != nil {
                return err 
        }   

        logger.Infof("Waiting for %s to exit so we can remove the container\n", id_main)
        if err := waitContainer(id_main); err != nil {
                return err 
        }   

        logger.Infof("Removing container %s\n", id_main)
        if err := removeContainer(id_main); err != nil {
                return err 
        }   
        return nil 

If I pass some command, say args = []string{"ls"}, I get something like:

Data Container ID: cb9913cd3d89bd8a303475acf96c6f83cdbae01e40da6b5d9c9c218aa6101575
Waiting for cb9913cd3d89bd8a303475acf96c6f83cdbae01e40da6b5d9c9c218aa6101575 to exit so we can remove the container
Removing container cb9913cd3d89bd8a303475acf96c6f83cdbae01e40da6b5d9c9c218aa6101575

So it works but no output from ls.

If I avoid removing the container and check docker logs, the output of ls is there and correct, but it doesn't make it to my terminal's stdout. And if I try to do the interactive thing, it just hangs.

Of course it works just fine from the cli:

docker run --volumes-from existing_data_container myimage ls

Any idea what I'm doing wrong?

@pwaller
Copy link

pwaller commented Nov 14, 2015

Hi @ebuchman. You also have to attach to the container to obtain the stream, and copy that to os.Stdout, something like this: https://gist.github.com/pwaller/314c7591a8bfd32f8999#file-dockerclient-bug-go-L60

I think the bug here is that the documentation is possibly not clear enough as to what the meaning of AttachStdout is. For that you have to look at for the docker docs themselves, but those aren't very clear either: http://docs.docker.com/engine/reference/api/docker_remote_api_v1.20/#create-a-container. It just says "Attaches stdout" which I take to mean "don't send stdout to /dev/null". To actually obtain the stream you need to run a separate attach command. Beware though, that this isn't easy!

@fsouza
Copy link
Owner

fsouza commented Nov 14, 2015

@pwaller thanks for helping him!

@ebuchman does it work for you?

Do you both think that improving the Docker would be enough here. Maybe providing a godoc example along with the field description?

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

No branches or pull requests

3 participants