Enhancing Container Networking with Docker Compose: A Guide to Multiple Networks and External Connections

Docker’s networking capabilities are a cornerstone of its utility, enabling containers to communicate internally and with the outside world. An advanced feature that Docker and Docker Compose support is connecting a container to multiple networks, including external ones. This functionality can be pivotal for complex applications requiring distinct communication paths, such as isolation between components, communication with external services, or different network policies. This guide will walk you through connecting a Docker container to multiple networks, including external ones, using Docker Compose, and will explore the different network types available in Docker.

Understanding the Need for Multiple and External Networks

Multiple Networks: Connecting a container to more than one network can drastically improve your application architecture. It allows for better isolation between services, simplifies inter-service communication by segregating backend and frontend networks, and offers more granular control over network policies and access.

External Networks: Sometimes, your containers need to communicate with services or networks that are not managed by Docker, such as a company-wide network or a third-party service. In these cases, you can connect your containers to these external networks.

Network Types in Docker

Before diving into the configuration, it’s essential to understand the types of networks in Docker:

  • Bridge: The default network type. If you don’t specify a network, your containers are attached to a default bridge network, allowing communication between containers on the same host.
  • Host: removes network isolation between the container and the Docker host, and uses the host’s networking directly.
  • Overlay: Enables network communication between containers across different Docker hosts, used in Docker Swarm.
  • Macvlan: Allows assigning a MAC address to a container, making it appear as a physical device in your network.
  • None: Disables all networking for the container.

Incorporating External Networks

To utilize an external network in Docker Compose, you first need to declare it in your docker-compose.yml file as an external network. This tells Docker Compose that the network is managed outside of Docker Compose. Here’s how you can define and use external networks:

version: '3.8'
services:
  myservice:
    image: myimage
    networks:
      - internal
      - my_external_network

networks:
  internal:
  my_external_network:
    external: true

In this configuration, myservice is connected to one internal Docker network (internal) and one external network (my_external_network). Docker Compose expects my_external_network to already exist in your environment, as it is marked as external.

Verify Network Connections

Check your container’s network connections with docker network inspect to confirm it’s connected to the specified networks.

If you’re using CasaOS and want to add or change networks, you can use BigBearScript to edit the Docker Compose file manually. Currently, CasaOS does not work well with changing networks with the UI.

Conclusion

Docker’s networking features, especially when utilized through Docker Compose, offer powerful capabilities to design complex, interconnected container setups. By understanding and using multiple internal and external networks, you can architect your containerized applications with greater flexibility, security, and efficiency. Whether isolating components within your application, integrating with existing network infrastructures, or designing multi-host Docker Swarm deployments, leveraging Docker’s networking options will significantly benefit your projects.

1 Like

I did try to manage MACVLAN on CasaOS for some container… It didnt work at al :frowning: I think that GUI is bugged and even if I tried your script and change network - it didnt work as well :slight_smile: I was trying to set KASM on different Vlan to isolate whole service.

I appreciate mentioning that casaos does not work well with changing networks in the UI. I’ve been trying that route for different apps for a while and always get weird errors. But because of my lack of understanding of how docker networks are used I just shrugged my shoulders and moved on to something else. Back to gluetun, thanks!

Could you post your Docker Compose? Also, did you only use the script to edit Docker Compose because if you edit the network from the UI at all, it will error out. Could you give me more detail on any errors or steps that you have tried?

You’re welcome and I’m glad I could help!