How to Add a Network Mode in Docker Compose

  1. Gather Network Information in Docker
  2. Configure Network Mode in Docker Compose
  3. Configure Network Mode in Docker Swarm
How to Add a Network Mode in Docker Compose

By default, a single network is created by Docker Compose in our application(s) and adds each container there as a service. Every container on the network can be accessed and found by containers on a single network.

We can configure our networks by defining a network mode in Docker Compose. This article will discuss how we can define and set up our networking using network mode in Docker Compose.

Gather Network Information in Docker

If we would like to configure a networking mode that will pass through a specific container or service, we will need to gather additional information first. Running the command docker network ls will enable us to list our current Docker networks.

It should look similar to the following output below.

Output:

NETWORK ID          NAME                         DRIVER
17cc61428fef        bridge                       bridge
098522f7fce0        sample_default               bridge
1ce3c472afc6        test_default                 bridge
8fd07e456e6c        host                         host
3b5787919641        none                         null

This command will be handy if we must be aware of the name or ID of our container, service, or network via the previous command presented above.

Configure Network Mode in Docker Compose

In Docker Compose version 3, we can use the network mode in our YAML file by providing the network_mode: parameter and its value. Therefore, make sure that you specify the version of Docker compose we are using with the version: parameter and the value 3 or 3.0.

Example:

version: "3"
services:
  app:
    network_mode: "host"

In the above example, we used the example host as our network mode, but we can specify a container or a service instead. From the Docker official documentation, here are the multiple values that we can use when defining the network mode of the service:

Example:

network_mode: "bridge"
network_mode: "host"
network_mode: "none"
network_mode: "service:[service name]"
network_mode: "container:[container name/id]"

Since we previously gathered information, we can use the network name or ID when defining our network mode.

Example:

network_mode: "container:sample_default"
network_mode: "container:1ce3c472afc6"

Configure Network Mode in Docker Swarm

A collection of physical or virtual machines running the Docker application is called a Docker Swarm. A container orchestration solution called Docker Swarm lets users control many containers spread across various host machines.

Once a group of machines has been clustered together, we can still run the Docker commands we are used to, but the devices in our cluster will now carry them out.

Unfortunately, Docker Compose and Swarm do not mix well. So, instead of using the network_mode parameter, we will need to define the network manually using the networks parameter, which should look like this.

Example:

version: "3.0"
services:
    app:
        networks:
            - host

networks:
    host:
        name: [Your Network Mode Value]
        external: true
Marion Paul Kenneth Mendoza avatar Marion Paul Kenneth Mendoza avatar

Marion specializes in anything Microsoft-related and always tries to work and apply code in an IT infrastructure.

LinkedIn

Related Article - Docker Compose