upserve / docker-api

A lightweight Ruby client for the Docker Remote API
MIT License
1.09k stars 289 forks source link

Mount docker socket #540

Closed adamdavis40208 closed 5 years ago

adamdavis40208 commented 5 years ago

What's the correct syntax for mounting the docker socket?

Example of what I'm talking about: docker run -it --rm -v /var/run/docker.sock:/var/run/docker.sock my_image

When I create a container with docker-cli and use the remote API to inspect the container the volumes look like:

  "Volumes"=>{
    "/var/lib/docker"=>"/var/lib/docker/volumes/32ae165895c5971aaff7342a4650f50fda69cd8a41ce5c0e5481f749c60645c8/_data",
    "/var/run/docker.sock"=>"/var/run/docker.sock"
  },

Trying this from the Ruby API gives a Go Struct error json: cannot unmarshal string into Go struct field ContainerConfigWrapper.Volumes of type struct {}

  @container = @image.run("#{command}", {
      'Volumes' => {
          "/var/run/docker.sock" => "/var/run/docker.sock"
      }
  })
tlunter commented 5 years ago

@adamdavis40208 Take a look here: https://github.com/swipely/docker-api/issues/256#issuecomment-75851503

adamdavis40208 commented 5 years ago

Heck yeah! Binds did it. @tlunter you're a beautiful wizard

For those in the future, this is what I came up with:

  @container = @image.run("#{command}", {
      'HostConfig' => { 
            "Binds" => ["/var/run/docker.sock:/var/run/docker.sock"]
      },
      'Volumes' => {
          "/var/run/docker.sock" => {"/var/run/docker.sock" => ""}
      }
  })