Terminus by Warp
Compose Create Networks In Docker Compose

Compose Create Networks In Docker Compose

 Mansi Manhas
Mansi Manhas

The short answer

In Docker Compose, a network refers to a layer allowing the containers of the services defined in the [.inline-code]compose.yaml[.inline-code] file to communicate with each other.

To create a network, you can set the [.inline-code]networks[.inline-code] attribute and assign it to multiple services in the Compose file as follows: 

 services:
  db:
    image: postgres
    networks:
      - <network_name>

networks:
  <network_name>:

Where:

  • The top-level [.inline-code]networks[.inline-code] attribute creates the network named [.inline-code]network_name[.inline-code].
  • The [.inline-code]networks[.inline-code] attribute in specific services refers to using the aforementioned [.inline-code]network_name[.inline-code].

For example:

 services:
  db:
    image: postgres
    networks:
      - db_network

  web:
    image: webapp
    networks:
      - db_network
      - app_network

   frontend:
     image: frontend
     networks: 
        - app_network

networks:
  app_network:
  db_network:

In this example, when you run the [.inline-code]docker compose up[.inline-code] command:

  • Two networks named [.inline-code]db_network[.inline-code] and [.inline-code]app_network[.inline-code] will be created.
  • The containers of the [.inline-code]db[.inline-code] service will be accessible within the [.inline-code]db_network[.inline-code] network.
  • The containers of the [.inline-code]frontend[.inline-code] service will be accessible within the [.inline-code]app_network[.inline-code] network.
  • The containers of the [.inline-code]web[.inline-code] service will be accessible in both the [.inline-code]db_network[.inline-code] and [.inline-code]app_network[.inline-code] networks.
  • The service [.inline-code]db[.inline-code] is isolated from the [.inline-code]frontend[.inline-code] service as they do not share a common network.

[#easily-recall-with-ai] Easily retrieve this syntax using Warp’s AI [#easily-recall-with-ai]

If you’re using Warp as your terminal, you can easily retrieve this syntax using the Warp AI feature:

Entering [.inline-code]docker compose networks[.inline-code] in the AI question input will prompt a human-readable step by step guide including code snippets.

[#specify-a-driver-for-networks] Specifying a driver for networks [#specify-a-driver-for-networks]

In Compose, network drivers control how containers communicate by setting up connections with user-defined networks, external networks, help accessing shared storage resources, and more.

To use a driver for networks, you can specify the [.inline-code]driver[.inline-code] attribute in the top-level [.inline-code]networks[.inline-code] section as follows:

 services:
  app:
    image: app
    networks:
      - my_network

networks:
  my_network:
    driver: <driver_name>

Where:

  • [.inline-code]driver_name[.inline-code] refers to the specific driver you want to use, like [.inline-code]host[.inline-code], [.inline-code]overlay[.inline-code], [.inline-code]bridge[.inline-code], etc. 

For example: 

 services:
  app:
    image: app
    networks:
      - my_network

networks:
  my_network:
    driver: overlay

In this example, the network named [.inline-code]my_network[.inline-code] is configured to use a driver named [.inline-code]overlay[.inline-code], which allows the containers of the [.inline-code]app[.inline-code] service to communicate with other containers located on different hosts.

Note that, Compose will return an error if the specified driver is unavailable on the target platform. You can refer to the official documentation to learn more about Docker’s network drivers.

[#overriding-the-default-driver] Overriding the default network driver [#overriding-the-default-driver]

By default, when no specific network is specified in the [.inline-code]compose.yaml[.inline-code] file, Compose creates a default network using the default driver [.inline-code]bridge[.inline-code], which allows communication between the containers on the same host.

To override the default driver, you can specify the [.inline-code]default[.inline-code] keyword in the top-level [.inline-code]networks[.inline-code] attribute as follows:

 # project directory name “myapp”
services:
  web:
    image: web
  db:
    image: postgres

networks:
  default:    
    driver: custom-driver

In this example, when you run the [.inline-code]docker compose up[.inline-code] command:

  • A default network named [.inline-code]myapp_default[.inline-code] will be created, which is based on your project directory’s name [.inline-code]myapp[.inline-code]. 
  • The [.inline-code]myapp_default[.inline-code] network will be configured to use the custom driver named [.inline-code]custom-driver[.inline-code] and is accessible to the containers of both services [.inline-code]web[.inline-code] and [.inline-code]db[.inline-code].

[#specifying-ipam-configurations] Specifying a custom IPAM configuration [#specifying-ipam-configurations]

In Compose, the IP Address Management (IPAM) configures the network, controlling how IP addresses are assigned to the containers within the network to prevent conflicts or connectivity issues.

To specify a custom IPAM configuration, you can use the [.inline-code]ipam[.inline-code] attribute in your [.inline-code]compose.yaml[.inline-code] file as follows:

 services:
  webapp:
    image: webapp
    networks:
      - my_network

networks:
  my_network:
    ipam:
      driver: <driver>
      config:
         - subnet: <subnet>
           ip_range: <ip_range>
           gateway: <gateway>
           aux_addresses:
              <hostname>: <value>
       options:
          <key>: <value>

Where:

  • The [.inline-code]driver[.inline-code] attribute (optional) refers to a custom IPAM driver. 
  • Under the [.inline-code]config[.inline-code] attribute (optional), you can specify one or more configurations:
  • [.inline-code]subnet[.inline-code] refers to a Subnet in CIDR (Classless Inter-Domain Routing) format.[.inline-code]ip_range[.inline-code] refers to the range of IPs used to allocate container IPs. [.inline-code]gateway[.inline-code] refers to the IPv4 or IPv6 gateway for the specified Subnets. [.inline-code]aux_addresses[.inline-code] refers to auxiliary IPv4 or IPv6 addresses, where the [.inline-code]value[.inline-code] represents the auxiliary IP address assigned to the corresponding host identified by [.inline-code]hostname[.inline-code].
  • The [.inline-code]options[.inline-code] are driver-specific configurations provided as a [.inline-code]key[.inline-code]-[.inline-code]value[.inline-code] mapping.

For example:

 services:
  webapp:
    image: webapp
    networks:
      - my_network

networks:
  my_network:
    ipam:
      driver: custom-driver
      config:
        - subnet: 172.28.0.0/16
          ip_range: 172.28.5.0/24
          gateway: 172.28.5.254
          aux_addresses:
            host1: 172.28.1.5
            host2: 172.28.1.6
      options:
        level: 0

In this example, a custom IPAM configuration for the network named [.inline-code]my_network[.inline-code] is specified:

  • The IPAM [.inline-code]driver[.inline-code] is set to [.inline-code]custom-driver[.inline-code].
  • The configuration is set as follows:
  • The [.inline-code]subnet[.inline-code] is set to [.inline-code]172.28.0.0/16[.inline-code], indicating that the network can accommodate IP addresses ranging from [.inline-code]172.28.0.0[.inline-code] to [.inline-code]172.28.255.255[.inline-code].
  • The [.inline-code]ip_range[.inline-code] is set to [.inline-code]172.28.5.0/24[.inline-code] specifying the range of IPs for the containers. 
  • The [.inline-code]gateway[.inline-code] is set to [.inline-code]172.28.5.254[.inline-code], indicating that the containers can use this IP address as a default gateway for the specified Subnets.
  • The [.inline-code]aux_addresses[.inline-code] indicates that the [.inline-code]host1[.inline-code] and [.inline-code]host2[.inline-code] are assigned the auxiliary addresses [.inline-code]172.28.1.5[.inline-code] and [.inline-code]172.28.1.6[.inline-code] respectively. 
  • The [.inline-code]options[.inline-code] are driver-specific, and the key [.inline-code]label[.inline-code] with value [.inline-code]0[.inline-code] will be used for the specified driver [.inline-code]custom-driver[.inline-code]. 

[#use-static-ip-addresses] Setting a static IP address [#use-static-ip-addresses]

To set a static IP address, you can use the [.inline-code]ipv4_address[.inline-code] or [.inline-code]ipv6_address[.inline-code] attributes as follows:

 services:
  webapp:
    image:webapp
    networks:
      my-network:
        ipv4_address: <ipv4_address>
        ipv6_address: <ipv6_address>

networks:
  my-network:

Where:

  • [.inline-code]ipv4_address[.inline-code] or [.inline-code]ipv6_address[.inline-code] is the desired IPv4 or IPv6 address.

For example:

 services:
  webapp:
    image: webapp
    networks:
      my-network:
        ipv4_address: 172.16.238.10
        ipv6_address: 2001:3984:3989::10

networks:
  my-network:
    ipam:
      config:
        - subnet: "172.16.238.0/24"
        - subnet: "2001:3984:3989::/64"

In this example:

  • The [.inline-code]webapp[.inline-code] service is assigned static IPv4 address [.inline-code]172.16.238.10[.inline-code] and static IPv6 address [.inline-code]2001:3984:3989::10[.inline-code].
  • The [.inline-code]ipam[.inline-code] attribute with the [.inline-code]subnet[.inline-code] configuration in the top-level [.inline-code]networks[.inline-code] section specifies the address ranges. 

[#connect-to-external-networks] Connecting to external networks [#connect-to-external-networks]

To connect the containers of your current Compose file with ones outside that file, you can use the [.inline-code]external[.inline-code] attribute under the top-level [.inline-code]networks[.inline-code] attribute, and connect to networks defined in another Compose file as follows:

 services:
  app:
    image: app
    networks:
      - my_network

networks:
  my_network:
    name: <external_network>
    external: true

Where:

  • [.inline-code]external_network[.inline-code] is the network’s name outside the scope of the Compose file.

For example:

 # Redis
services:
  db:
    image: redis:latest
    networks:
      - my_network

networks:
  my_network:
    driver: bridge
    name: redis_network

# Web application
services:
  my_app:
    image: web-app:latest
    networks:
      - my-app

networks:
  my-app:
    name: redis_network
    external: true

In this example, we have created a shared network for a Redis cache and a web application:

  • The Compose file for the Redis application defines a network named [.inline-code]redis_network[.inline-code] with a [.inline-code]bridge[.inline-code] driver, making it available for the containers of the [.inline-code]db[.inline-code] service. 
  • The Compose file for the web application uses the [.inline-code]external[.inline-code] attribute with a value of [.inline-code]true[.inline-code] to establish a connection with the network created outside the scope of the Compose file, in this case the [.inline-code]redis_network[.inline-code].

As another example, you can connect with the Docker’s built-in [.inline-code]host[.inline-code] or [.inline-code]none[.inline-code] network as follows:

 # host
services:
  app:
    image: app
    networks:
      - hostnet:

networks:
  hostnet:
    external: true
    name: host
# none
services:
  app:
    image: app
    networks:
      nonet: 

networks:
  nonet:
    external: true
    name: none

In these examples:

  • The [.inline-code]app[.inline-code] service is connected to a Docker’s built-in network named [.inline-code]host[.inline-code] and [.inline-code]none[.inline-code], which are outside the scope of the Compose files. 
  • The [.inline-code]host[.inline-code] network specifies that containers share the network directly with the host and is helpful for high-performance applications where you need to handle large volumes of data, such as real-time analytics. 
  • The [.inline-code]none[.inline-code] network turns off the networking and specifies that containers run entirely in isolation from the host and other containers. 
  • The alias [.inline-code]hostnet[.inline-code] and [.inline-code]nonet[.inline-code] are used explicitly for Docker's built-in [.inline-code]host[.inline-code] and [.inline-code]none[.inline-code] networks.

Experience the power of Warp

  • Write with an IDE-style editor
  • Easily navigate through output
  • Save commands to reuse later
  • Ask Warp AI to explain or debug
  • Customize keybindings and launch configs
  • Pick from preloaded themes or design your own
brew install --cask warp
Copied!
Join the Windows waitlist:
Success! You will receive an email from Warp when the release is available to download.
Oops! Something went wrong while submitting the form.
Join the Linux waitlist:
Success! You will receive an email from Warp when the release is available to download.
Oops! Something went wrong while submitting the form.
Join the Linux waitlist or join the Windows waitlist
Join the Windows waitlist:
Success! You will receive an email from Warp when the release is available to download.
Oops! Something went wrong while submitting the form.