Begin to setup and configure docker networks for containers

This commit is contained in:
Brian Cain 2019-02-26 11:47:48 -08:00
parent 8013d8d5ef
commit e860c7709d
No known key found for this signature in database
GPG Key ID: 9FC4639B2E4510A0
2 changed files with 44 additions and 21 deletions

View File

@ -11,18 +11,27 @@ module VagrantPlugins
def call(env)
# If we are using a host VM, then don't worry about it
if env[:machine].provider.host_vm?
machine = env[:machine]
if machine.provider.host_vm?
@logger.debug("Not setting up networks because docker host_vm is in use")
return @app.call(env)
end
env[:machine].config.vm.networks.each do |type, options|
machine.config.vm.networks.each do |type, options|
# We only handle private and public networks
next if type != :private_network && type != :public_network
# TODO: Configure and set up network for each container that has a network
# machine.id == container id
# docker network name == env[:machine].name + "_vagrant_" + unique-sha
# Look at docker provider config for network options:
# - If not defined, create simple "folder environment" multi-network
# that all containers with this default option are attached to
# - If provider config option is defined, create the requested network
# and attach that container to it
network_name = "#{env[:root_path].basename.to_s}_network_#{machine.name}"
container_id = machine.id
# TODO: Need to check if network already exists and not error
machine.provider.driver.create_network(network_name)
options = ["--ip", options[:ip]]
machine.provider.driver.connect_network(network_name, container_id, options)
end
@app.call(env)

View File

@ -180,43 +180,57 @@ module VagrantPlugins
# @param[String] network - name of network to connect conatiner to
# @param[String] cid - container id
# @param[Array] opts - An array of flags used for listing networks
def connect_network(network, cid, opts=nil)
output = execute('docker', 'network', 'connect', network, cid, opts)
def connect_network(network, cid, *opts)
command = ['docker', 'network', 'connect', network, cid].concat(*opts)
output = execute(*command)
output
end
# @param[String] network - name of network to create
# @param[Array] opts - An array of flags used for listing networks
def create_network(network, opts=nil)
output = execute('docker', 'network', 'create', network, opts)
# @param[Array] opts - An array of flags used for listing networks
def create_network(network, *opts)
command = ['docker', 'network', 'create', network].concat(*opts)
output = execute(*command)
output
end
# @param[String] network - name of network to disconnect container from
# @param[String] cid - container id
# @param[Array] opts - An array of flags used for listing networks
def disconnect_network(network, cid, opts=nil)
output = execute('docker', 'network', 'disconnect', network, cid, opts)
# @param[Array] opts - An array of flags used for listing networks
def disconnect_network(network, cid, *opts)
command = ['docker', 'network', 'disconnect', network, cid].concat(*opts)
output = execute(*command)
output
end
# @param[Array] networks - list of networks to inspect
# @param[Array] opts - An array of flags used for listing networks
def inspect_network(networks, opts=nil)
output = execute('docker', 'network', 'inspect', networks, opts)
def inspect_network(networks, *opts)
command = ['docker', 'network', 'inspect', networks].concat(*opts)
output = execute(*command)
output
end
# @param[Array] opts - An array of flags used for listing networks
def list_network(opts=nil)
output = execute('docker', 'network', 'ls', opts)
def list_network(*opts)
command = ['docker', 'network', 'ls'].concat(*opts)
output = execute(*command)
output
end
# @param[Array] opts - An array of flags used for listing networks
def prune_network(opts=nil)
output = execute('docker', 'network', 'prune', '--force', opts)
def prune_network(*opts)
command = ['docker', 'network', 'prune', '--force'].concat(*opts)
output = execute(*command)
output
end
# @param[String] network - name of network to remove
# @param[Array] opts - An array of flags used for listing networks
def rm_network(networks, opts=nil)
output = execute('docker', 'network', 'rm', network, opts)
def rm_network(networks, *opts)
command = ['docker', 'network', 'rm', network].concat(*opts)
output = execute(*command)
output
end
# @param[Array] opts - An array of flags used for listing networks