dockernetwork-programmingdocker-compose

How do I give access to internet to my docker container in docker-compose?


So, my container is trying to make a chat completion via API of Azure - https://models.inference.ai.azure.com/chat/completions, but it doesn't have access to internet. I've attached it to bridge network, added many dns but still doesn't work. If you know how to fix - please help me. Thanks in advance!

assistant:
    image: ghcr.io/kiroshi-ai/kiroshi-ai-assistant:v1
    build:
      context: ./assistant
      dockerfile: Dockerfile
    ports:
      - "3500:3500"
    depends_on:
      - surrealdb
      - redis
      - loki
      - nats

    dns:
      - 8.8.8.8
      - 8.8.4.4
      - 1.1.1.1
      - 9.9.9.9
      - 168.63.129.16
      - 20.62.61.128
      - 20.98.195.77
      - 84.200.69.80
      - 8.26.56.26
      - 208.67.222.222
      - 10.254.254.254
    extra_hosts:
      - "host.docker.internal:host-gateway"
    networks:
      - default
      - db
      - logs
      - nats
      - nginx


networks:
  default:
    driver: bridge
  db:
    name: kiroshi-ai-db-network
  logs:
    name: kiroshi-ai-logs-network
  nats:
    name: kiroshi-ai-nats-cluster
  nginx:
    name: kiroshi-ai-nginx-network

Solution

  • I little a bit modified your compose config to build and start this docker service by itself, without requiring a surrealdb, a redis, a loki and a nats services. My compose config looks like:

    services:
      assistant:
        image: ghcr.io/kiroshi-ai/kiroshi-ai-assistant:v1
        build:
          context: ./assistant
          context: .
          dockerfile: Dockerfile
          dockerfile_inline: |
            FROM ghcr.io/kiroshi-ai/kiroshi-ai-assistant:v1
        ports:
          - "3500:3500"
        depends_on:
          - surrealdb
          - redis
          - loki
          - nats
    
        command:
          - sleep
          - "3600"
    
        dns:
          - 8.8.8.8
          - 8.8.4.4
          - 1.1.1.1
          - 9.9.9.9
          - 168.63.129.16
          - 20.62.61.128
          - 20.98.195.77
          - 84.200.69.80
          - 8.26.56.26
          - 208.67.222.222
          - 10.254.254.254
        extra_hosts:
          - "host.docker.internal:host-gateway"
        networks:
          - default
          - db
          - logs
          - nats
          - nginx
    
    
    
    networks:
      default:
        driver: bridge
      db:
        name: kiroshi-ai-db-network
      logs:
        name: kiroshi-ai-logs-network
      nats:
        name: kiroshi-ai-nats-cluster
      nginx:
        name: kiroshi-ai-nginx-network
    

    So, in general, your compose service has access to the global network. I checked it the next way:

    # ping models.inference.ai.azure.com
    PING vienna-eastus2-ip-standard-tagged-02.eastus2.cloudapp.azure.com (20.98.195.77) 56(84) bytes of data.
    64 bytes from 20.98.195.77 (20.98.195.77): icmp_seq=1 ttl=100 time=187 ms
    64 bytes from 20.98.195.77 (20.98.195.77): icmp_seq=2 ttl=100 time=211 ms
    64 bytes from 20.98.195.77 (20.98.195.77): icmp_seq=3 ttl=100 time=245 ms
    ^C
    --- vienna-eastus2-ip-standard-tagged-02.eastus2.cloudapp.azure.com ping statistics ---
    3 packets transmitted, 3 received, 0% packet loss, time 2000ms
    rtt min/avg/max/mdev = 186.650/214.048/244.756/23.836 ms
    

    Try to reproduce, and if you still haven't access to the amazon.com from docker, check do you have access to amazon from the host system.