I'm building a playbook where I want to deploy a container to a host network. The Ansible docs mention two options: network_mode
and networks
. If I want to connect my container to the default host network, is the option (syntax as in Ansible playbooks):
network_mode: host
equivalent to:
networks:
name: host
?
If not, what would be the network to which the container connects in both cases (if it connects at all?)
The URL or Unix socket path used to connect to the Docker API. To connect to a remote host, provide the TCP connection string. For example, 'tcp://192.0.2.23:2376'. If TLS is used to encrypt the connection, the module will automatically replace 'tcp' in the connection URL with 'https'.
docker_container – manage docker containers — Ansible Documentation. For community users, you are reading an unmaintained version of the Ansible documentation. Unmaintained Ansible versions can contain unfixed security vulnerabilities (CVE). Please upgrade to a maintained version.
This is more of a Docker question than an Ansible question. You can find additional details beyond what I've put here in my answer by reading through, for example, the Network settings section of the docker run
documentation.
The networks
key allows you to connect your container to a named network.
The network_mode
key allows you to select the type of network to create for your container (host
, bridge
, none
, or the network environment of a specific container). It doesn't allow you to connect directly to a named network.
So if you have a network named host
available that uses host
networking mode, then:
network_mode: host
Is equivalent to:
networks:
- name: host
But in general, they let you do distinct things.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With