How do you define a network in a version 2 docker-compose definition file?

Please find below example which include network definition, traffic implementation etc.

version: '2.2'
services: 
  unifiedpushserver: 
    image: docker.io/aerogear/unifiedpush-wildfly:2.1.0 
    networks: 
      - network 
    volumes: 
       - ./helper:/ups-helper:z  
    entrypoint: "/ups-helper/exportKeycloakHost.sh" 
    depends_on: 
       unifiedpushDB: 
         condition: service_healthy 
    environment: <br>
        POSTGRES_SERVICE_HOST: ${POSTGRES_SERVICE_HOST} 
        POSTGRES_USER: ${POSTGRES_USER} 
        POSTGRES_PASSWORD: ${POSTGRES_PASSWORD} 
        POSTGRES_SERVICE_PORT: ${POSTGRES_SERVICE_PORT} 
        POSTGRES_DATABASE: ${POSTGRES_DATABASE} 
        KEYCLOAK_SERVICE_HOST: ${KEYCLOAK_SERVICE_HOST} 
        KEYCLOAK_SERVICE_PORT: ${KEYCLOAK_SERVICE_PORT} 
    labels: 
      traefik.backend: "aerogear" 
      traefik.docker.network: "appliance" 
      traefik.domain: "notification.com" 
      traefik.enable: "true" 
      traefik.frontend.entryPoints: "http, https" 
      traefik.frontend.redirect: "false" 
      traefik.frontend.rule: "Host: notification.com" 
    links: 
      - unifiedpushDB:unifiedpush 
      - keycloakServer:keycloak 
    ports: 
      - 9999:8080 
  unifiedpushDB: 
    image: postgres:9.6 
    networks: 
      - network 
    environment: 
      POSTGRES_USER: ${POSTGRES_USER} 
      POSTGRES_PASSWORD: ${POSTGRES_PASSWORD} 
      POSTGRES_DB: ${POSTGRES_DATABASE} 
    healthcheck: 
      test: ["CMD-SHELL", "pg_isready -U postgres"] 
      timeout: 20s 
      retries: 10 
  keycloakServer: 
    networks: 
      - network 
    image: docker.io/jboss/keycloak:4.1.0.Final 
    command: "-b 0.0.0.0 -Dkeycloak.import=/ups-keycloak-config/ups-realm-sample.json" 
    volumes: 
      - ./keycloak-realm:/ups-keycloak-config:z 
    environment: 
      KEYCLOAK_USER: ${KEYCLOAK_USER} 
      KEYCLOAK_PASSWORD: ${KEYCLOAK_PASSWORD} 
networks: 
     network: 
       external: 
         name: appliance 

Compose will create a default network for you as long as you use the version 2 format, but if you'd like to customize the networks the docs are here:

  • https://docs.docker.com/compose/compose-file/#network-configuration-reference
  • https://docs.docker.com/compose/networking/#specifying-custom-networks

You can create a networks section at the top level of the Compose file and reference them in the networks section of each service. But you don't need to, just use the default network as described in the comments below.