FoxuTech

Docker Compose Networking – Explained

Docker Compose Networking

Docker Compose sets up a single network for your application(s) by default, adding each container for a service to the default network. Containers on a single network can reach and discover every other container on the network.

Networking Basics

Running the command docker network ls will list out your current Docker networks; it should look similar to the following:

# docker network ls

NETWORK ID          NAME                           DRIVER              SCOPE

f7294467814c        bridge                         bridge              local

87dada510c46        Docker_compose_test            bridge              local

06551c66f206        host                           host                local

27de0cb13467        my_first_ever_bridge_network   bridge              local

9ef81245913e        none                           null                local

You can modify the network name with the -p or –project-name flags or the COMPOSE_PROJECT_NAME environment variable. (In the event you need to run multiple projects on a single host, it’s recommended to set project names via the flag.)

In our docker_compose example, web can access the MySQL database from postgres://postgres:5432. We can access web from the outside world via port 8000 on the Docker host (only because the web service explicitly maps port 8000.

Updating Containers on the Network

You can change service configurations via the Docker Compose file. When you run docker-compose up to update the containers, compose removes the old container and inserts a new one. The new container has a different IP address than the old one, but they have the same name. Containers with open connections to the old container close those connections, look up the new container by its name, and connect. If you wish give static IP have to mention on the docker-compose file alone.

Linking Containers

You may define additional aliases that services can use to reach one another. Services on the same network can already reach one another. In the example below, we allow web to reach db via one of two hostnames (db or database):

version: '2'
services:    
web:        
   build: .         
   links:             
       - "db:database"    
   db:        
     image: postgres

If you do not specify a second hostname (for example, – db instead of – “db:database”), Docker Compose uses the service name (db). Links express dependency like depends_on does, meaning links dictate the order of service startup.

Networking with Multiple Hosts

You may use the overlay driver when deploying Docker Compose to a Swarm cluster. We’ll cover more on Docker Swarm in a future article.

Configuring the Default Network

If you desire, you can configure the default network instead of (or in addition to) customizing your own network. Simply define a default entry under networks:

version: '2' 

services:    
   web:        
       build: .         
       ports:            
            - "8000:8000"    
   db:        
       image: postgres 

networks:    
     default:        
        driver: custom-driver-1

Custom Networks

Specify your own networks with the top-level networks key, to allow creating more complex topologies and specify network drivers (and options). You can also use this configuration to connect services with external networks Docker Compose does not manage. Each service can specify which networks to connect to with its service-level networks key.

See More: Basic Docker Networking – Explained

The following example defines two custom networks. Keep in mind, proxy cannot connect to db, as they do not share a network; however, app can connect to both. In the front network, we specify the IPv4 and IPv6 addresses to use (we have to configure an ipam block defining the subnet and gateway configurations). We could customize either network or neither one, but we do want to use separate drivers to separate the networks

version: '2' 

services:    
  proxy:        
      build: ./proxy        
      networks:             
          - front    
  app:        
      build: ./app        
      networks:            
         # you may set custom IP addresses            
         front:                
             ipv4_address: 192.168.0.161                 
             ipv6_address: "2001:3998:3929::10"
         - back    
  db:          
      image: postgres        
      networks:            
          - back 
networks:    
   front:        
       driver: bridge        
       driver_opts:            
          com.docker.network.enable_ipv6: "true"        
       ipam:            
          driver: default            
          config:                
              - subnet: 192.168.0.0/24                
              gateway: 192.168.0.3                
              - subnet: "2001:3998:3929::/64"                
              gateway: "2001:3998:3929::1"    
   back:        
       driver: custom-driver-1

Pre-Existing Networks

You can even use pre-existing networks with Docker Compose; just use the external option:

version: '2' 
networks:    
  default:        
    external:            
      name: Existing_Custom_Network

In this case, Docker Compose never creates the default network; instead connecting the app’s containers to the Existing_Custom_Network network.

Common Issues

You’ll need to use version 2 of the Compose file format. (If you follow along with these tutorials, you already do.) Legacy (version 1) Compose files do not support networking. You can determine the version from the version: line in the docker-compose.yml file.

General YAML

Use quotes (“” or ‘’) whenever you have a colon (:) in your configuration values, to avoid confusion with key-value pairs.

Updating Containers

Container IP addresses change on update. Reference containers by name, not IP, whenever possible. Otherwise you’ll need to update the IP address you use.

Links

If you define both links and networks, linked services must share at least one network to communicate.

Exit mobile version