skip to Main Content
I have just started learning docker . This is my docker-compose.yml file . It shows the following error   "services.catalog.api.networks must be a list" When I docker compose up
version: '3.4'

networks:
  testnetwork:
    driver: bridge 


services:
  orderdb:
    image: mongo

  catalogdb:
    image: mongo  
    
  orderapi:
    image: orderapi
    build:
      context: .
      dockerfile: OrderApi/Dockerfile
    ports:
      - 5243:5243
    networks:
      testnetwork 



  catalog.api:
    image: farhanpatel/catalogapi
    build:
      context: .
    environment:
      - ASPNETCORE_ENVIRONMENT=Development
      - "DatabaseSettings:ConnectionString=mongodb://catalogdb:27017"      
    depends_on: 
      - catalogdb  
    ports:
      - "5116:5116"  
    networks:
      testnetwork     

volumes:
  mongo_data:

I want to connect both orderapi and catalogapi through docker bridge network.

I have created a bridge network named testnetwork and used it in above docker-compose.yml .

Could someone explain how should I connect both the images

2

Answers


  1. You should separate app form Mongodb. My example docker-compose(node.js,MongoDB)

    version: "3"
    services:
      nodeapp:
        container_name: nodeapp
        restart: always
        build:
          context: .
          dockerfile: Dockerfile
        env_file: .env
        ports:
          - "4500:4500"
        links:
          - mongodb
        depends_on:
          - mongodb
        environment:
          WAIT_HOSTS: mongodb:27017
        networks:
          - node-webapp-network
      mongodb:
        container_name: mongodb
        image: mongo:6.0
        volumes:
          - ~/mongo:/data/db
        ports:
          - "27017:27017"
        environment:
          - MONGO_INITDB_ROOT_USERNAME=your_user
          - MONGO_INITDB_ROOT_PASSWORD=your_password
          - MONGO_INITDB_DATABASE=admin
        networks:
          - node-webapp-network
    networks:
      node-webapp-network:
        driver: bridge
    

    command

    docker network ls
    

    to list networks

    And to inspect

    docker inspect testnetwork
    

    will give you

    [
        {
            "Name": "testnetwork",
            "Id": "259b9fd2d442984f142849152ac68eabd481c2ed0d24168080edace7930c5302",
            "Created": "2022-05-01T07:45:29.549723985+02:00",
            "Scope": "local",
            "Driver": "bridge",
            "EnableIPv6": false,
            "IPAM": {
                "Driver": "default",
                "Options": {},
                "Config": [
                    {
                        "Subnet": "172.24.0.0/16",
                        "Gateway": "172.24.0.1"
                    }
                ]
            },
            "Internal": false,
            "Attachable": false,
            "Ingress": false,
            "ConfigFrom": {
                "Network": ""
            },
            "ConfigOnly": false,
            "Containers": {},
            "Options": {},
            "Labels": {}
        }
    ]
    

    You should also read some Docker books or courses where you have step by step explained.

    Login or Signup to reply.
  2. You should just delete all of the networks: blocks in the entire file. Compose on its own creates a network named default and attaches containers to it, and this is enough for all standard Docker networking functionality to work. Also see Networking in Compose in the Docker documentation.

    version: '3.8'
    services:
      orderdb:
        image: mongo
      orderapi:
        build:
          context: .
          dockerfile: OrderApi/Dockerfile
        environment:
          DB_HOST: orderdb
        ports:
          - 5243:5243
    # no networks: at all
    

    If you do have networks:, a container can be attached to multiple networks, and so the value of the per-container networks: needs to be a YAML list. Usually this means putting - at the front of the line (the space is important). You need to do this for all of your containers; in the example as you’ve shown it, the API servers would be on testnetwork but the databases still on the default network.

    version: '3.8'
    networks:            # <-- added from previous example
      testnetwork:       # <-- (or you can safely delete _all_ of these)
    services:
      orderdb:
        image: mongo
        networks:        # <--
          - testnetwork  # <--
      orderapi:
        build:
          context: .
          dockerfile: OrderApi/Dockerfile
        environment:
          DB_HOST: orderdb
        ports:
          - 5243:5243
        networks:        # <--
          - testnetwork  # <--
    #     ^^ note YAML list marker
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search