Networking
Vagrant uses the docker network
command under the hood to create and manage
networks for containers. Vagrant will do its best to create and manage networks
for any containers configured inside the Vagrantfile. Each docker network is grouped
by the subnet used for a requested ip address.
For each newly unique network, Vagrant will run the docker network create
subcommand
with the provided options from the network config inside your Vagrantfile. If multiple
networks share the same subnet, Vagrant will reuse that existing network for multiple
containers. Once these networks have been created, Vagrant will attach these
networks to the requested containers using the docker network connect
for each
network.
Vagrant names the networks inside docker as vagrant_network
or vagrant_network_<subnet here>
where <subnet_here>
is the subnet for the network if defined by the user. An
example of these networks is shown later in this page. If no subnet is requested
for the network, Vagrant will connect the vagrant_network
to the container.
When destroying containers through Vagrant, Vagrant will clean up the network if there are no more containers using the network.
Docker Network Options
Most of the options work similar to other Vagrant providers. Defining either an
ip or using type: 'dhcp'
will give you a network on your container.
If you want to set something specific with a new network you can use scoped options
which align with the command line flags for the docker network create
command. If there are any specific options you want to enable from the docker network create
command, you can specify them like this:
This will enable the internal
option for the network when created with docker network create
.
Where option
corresponds to the given flag that will be provided to the docker network create
command. Similarly, if there is a value you wish to enable when connecting a container
to a given network, you can use the following value in your network config:
When the docker provider creates a new network a netmask is required. If the netmask
is not provided, Vagrant will default to a /24
for IPv4 and /64
for IPv6. To provide
a different mask, set it using the netmask
option:
For networks which set the type to "dhcp", it is also possible to specify a specific
subnet for the network connection. This allows containers to connect to networks other
than the default vagrant_network
network. The docker provider supports specifying
the desired subnet in two ways. The first is by using the ip
and netmask
options:
The second is by using the subnet
option:
Public Networks
The Vagrant docker provider also supports defining public networks. The easiest way
to define a public network is by setting the type
option to "dhcp":
A bridge interface is required when setting up a public network. When no bridge
device name is provided, Vagrant will prompt for the appropriate device to use. This
can also be set using the bridge
option:
The bridge
option also supports a list of interfaces which can be used for
setting up the network. Vagrant will inspect the defined interfaces and use
the first active interface when setting up the network:
The available IP range for the bridge interface must be known when setting up
the docker network. Even though a DHCP service may be available on the public
network, docker will manage IP addresses provided to containers. This means
that the subnet provided when defining the available IP range for the network
should not be included within the subnet managed by the DHCP service. Vagrant
will prompt for the available IP range information, however, it can also be
provided in the Vagrantfile using the docker_network__ip_range
option:
Finally, the gateway for the interface is required during setup. The docker
provider will default the gateway address to the first address available for
the subnet of the bridge device. Vagrant will prompt for confirmation to use
the default address. The address can also be manually set in the Vagrantfile
using the docker_network__gateway
option:
More examples are shared below which demonstrate creating a few common network interfaces.
Docker Network Example
The following Vagrantfile will generate these networks for a container:
- A IPv4 IP address assigned by DHCP
- A IPv4 IP address 172.20.128.2 on a network with subnet 172.20.0.0/16
- A IPv6 IP address assigned by DHCP on subnet 2a02:6b8:b010:9020:1::/80
You can test that your container has the proper configured networks by looking
at the result of running ip addr
, for example:
You can also connect your containers to a docker network that was created outside of Vagrant:
Vagrant will not delete or modify these outside networks when deleting the container, however.
Useful Debugging Tips
The docker network
command provides some helpful insights to what might be going
on with the networks Vagrant creates. For example, if you want to know what networks
you currently have running on your machine, you can run the docker network ls
command:
You can also inspect any network for more information:
Caveats
For now, Vagrant only looks at the subnet when figuring out if it should create a new network for a guest container. If you bring up a container with a network, and then change or add some new options (but leave the subnet the same), it will not apply those changes or create a new network.
Because the --link
flag for the docker network connect
command is considered
legacy, Vagrant does not support that option when creating containers and connecting
networks.
More Information
For more information on how docker manages its networks, please refer to their documentation: