1

this is the documentation for the API: https://docs.docker.com/engine/api/v1.24/#containers where you'll see that for the create container method a section of the JSON labelled "NetworkingConfig" is displayed

this section is not described at all and I can't quite figure out how to express what I need, which is the equivalent of a docker-compose:

services:
  myservice:
    networks:
      - mynetwork
networks:
  mynetwork:
    external: true

how can I do the above programatically?

ekkis
  • 9,804
  • 13
  • 55
  • 105
  • When you call `POST containers/create` you pass `NetworkingConfig ` with the network configuration but before you must create the network `POST /networks/create` https://docs.docker.com/engine/api/v1.24/#networks – Raphael May 05 '17 at 01:43
  • ja, I know. what I don't know are the details (there's no docs for it) so do I just declare the network name e.g. `NetworkingConfig: {EndpointsConfig: {'my-network-name': {}}}`? – ekkis May 06 '17 at 01:56
  • Check my link and search for 'NetworkingConfig' you'll see a json example. I`m guiding myself with this docker api impl in python: http://docker-py.readthedocs.io/en/stable/api.html along with the official docs to see which parameters are Optional. – Raphael May 06 '17 at 03:49

1 Answers1

1

so the answer is: it's more complicated than just setting the NetworkMode key (which works but only for one network)

to accomplish this: get the network, get the container, bind them together. something like this (works with dockerode v3.2.4+):

const Docker = require('dockerode');
const docker = new Docker();

var net = docker.getNetwork('your-external-network-name');
var cnt = docker.getContainer('your-container-name');

net.connect({Container: cnt.id}, function(err, data) {
  if (!err) console.log('we're good');
})
ekkis
  • 9,804
  • 13
  • 55
  • 105