Dockerode: documentation for port bindings

Created on 19 Feb 2014  路  4Comments  路  Source: apocas/dockerode

There doesn't appear to be any documentation or tests for binding container ports to host.

Most helpful comment

for reference here is how I did it (it would be good to document this since the answers wasn't obvious)

 options =
        Image       : repoTag
        AttachStdin : false,
        AttachStdout: true,
        AttachStderr: true,
        Tty         : true,
        Cmd         : null
        OpenStdin   : false,
        StdinOnce   : false
        ExposedPorts: {'3000/tcp': {} }
        PortBindings: {'3000/tcp': [{ 'HostPort': port.str() }] },

      docker.createContainer options, (err, container)->

here is the full test

  describe 'bsimm-graphs tests', ->

    it 'pull diniscruz/bsimm-graphs', (done)->
      @.timeout 60000
      repoTag = 'diniscruz/bsimm-graphs:latest'
      docker = create_Docker()

      onFinished = (err, output) ->
        assert_Is_Null err
        console.log output.last()
        output.last().status.assert_Contains repoTag
        done()

      onProgress = (event) ->
        if ['Already exists', 'Waiting', 'Extracting', 'Pull complete', 'Downloading', 'Download complete'].not_Contains event.status
          if event.id
            console.log "#{event.status} - #{event.id}"
          else
            console.log #{event.status}

      docker.pull repoTag, (err, stream) ->
        docker.modem.followProgress(stream, onFinished, onProgress)

    it 'run and remove BSIMM Graph', (done)->
      @.timeout 5000
      docker = create_Docker()
      repoTag = 'diniscruz/bsimm-graphs:latest'
      port = 30000 + 5000.random()
      options =
        Image       : repoTag
        AttachStdin : false,
        AttachStdout: true,
        AttachStderr: true,
        Tty         : true,
        Cmd         : null
        OpenStdin   : false,
        StdinOnce   : false
        ExposedPorts: {'3000/tcp': {} }
        PortBindings: {'3000/tcp': [{ 'HostPort': port.str() }] },

      docker.createContainer options, (err, container)->

        if err
          console.log err
          return done(err)

        container.start (err, abc)->
          console.log '>>>>   here'
          2000.wait ->
            url = "http://192.168.99.100:#{port}"
            url.GET (data)=>
              console.log '>>>>   got data'
              console.log data
              if data
                data.assert_Is 'Found. Redirecting to d3-radar'

              container.stop (err)->
                assert_Is_Null err
                container.remove (err)->
                  assert_Is_Null err
                  done()

All 4 comments

I think you can take a look at the Docker Remote API in regards to port binding as dockerode is simply sending requests to the Docker API.

Exactly, there's nothing specific about that in dockerode :)
You really should look for that in Docker's Remote API documentation.

http://docs.docker.io/en/latest/reference/api/docker_remote_api/

for reference here is how I did it (it would be good to document this since the answers wasn't obvious)

 options =
        Image       : repoTag
        AttachStdin : false,
        AttachStdout: true,
        AttachStderr: true,
        Tty         : true,
        Cmd         : null
        OpenStdin   : false,
        StdinOnce   : false
        ExposedPorts: {'3000/tcp': {} }
        PortBindings: {'3000/tcp': [{ 'HostPort': port.str() }] },

      docker.createContainer options, (err, container)->

here is the full test

  describe 'bsimm-graphs tests', ->

    it 'pull diniscruz/bsimm-graphs', (done)->
      @.timeout 60000
      repoTag = 'diniscruz/bsimm-graphs:latest'
      docker = create_Docker()

      onFinished = (err, output) ->
        assert_Is_Null err
        console.log output.last()
        output.last().status.assert_Contains repoTag
        done()

      onProgress = (event) ->
        if ['Already exists', 'Waiting', 'Extracting', 'Pull complete', 'Downloading', 'Download complete'].not_Contains event.status
          if event.id
            console.log "#{event.status} - #{event.id}"
          else
            console.log #{event.status}

      docker.pull repoTag, (err, stream) ->
        docker.modem.followProgress(stream, onFinished, onProgress)

    it 'run and remove BSIMM Graph', (done)->
      @.timeout 5000
      docker = create_Docker()
      repoTag = 'diniscruz/bsimm-graphs:latest'
      port = 30000 + 5000.random()
      options =
        Image       : repoTag
        AttachStdin : false,
        AttachStdout: true,
        AttachStderr: true,
        Tty         : true,
        Cmd         : null
        OpenStdin   : false,
        StdinOnce   : false
        ExposedPorts: {'3000/tcp': {} }
        PortBindings: {'3000/tcp': [{ 'HostPort': port.str() }] },

      docker.createContainer options, (err, container)->

        if err
          console.log err
          return done(err)

        container.start (err, abc)->
          console.log '>>>>   here'
          2000.wait ->
            url = "http://192.168.99.100:#{port}"
            url.GET (data)=>
              console.log '>>>>   got data'
              console.log data
              if data
                data.assert_Is 'Found. Redirecting to d3-radar'

              container.stop (err)->
                assert_Is_Null err
                container.remove (err)->
                  assert_Is_Null err
                  done()
Was this page helpful?
0 / 5 - 0 ratings

Related issues

AlexanderCollins picture AlexanderCollins  路  5Comments

tkarls picture tkarls  路  6Comments

florentvaldelievre picture florentvaldelievre  路  4Comments

jankeromnes picture jankeromnes  路  5Comments

knight42 picture knight42  路  7Comments