Search code examples
dockernetwork-programmingdocker-network

Docker networking, connecting to linux container from windows host


I'm not sure why when running linux based containers on a windows 10 host, I cannot access via the IP Address of the container, while if running windows containers I can.

Example for linux

networks:
  team_net:
    ipam:
      driver: default
      config:
        - subnet: 172.28.1.0/26

services:
  sql:
    image: mcr.microsoft.com/mssql/server:latest
    networks:
      team_net:
        ipv4_address: 172.28.1.11
    environment:
      - ACCEPT_EULA: Y
      - SA_PASSWORD: My_password123
    volumes:
      - "mssql:/var/opt"
    restart: unless-stopped

volumes:
  mssql:

connecting to server : 172.28.1.11,1433, will refuses to connect.

Also, if not set and inspecting the running container, I can see the Ip Address assigned to it, but again trying to use it fails.

I'm not a network person, but would like to know if it is possible at all to use directly the IP Address assigned to a container, then connecting using exposed ports. If it is not possible, why is that ?


Solution

  • This is explicitly noted in the Docker Desktop documentation. There are a variety of other circumstances when you can't directly reach the container-private IP addresses: on a MacOS host; if you're using Docker Toolbox or another VM-based solution; if you're not on the same physical host. Since this works on so few environments, directly using the container-private IP addresses isn't usually a best practice.

    Regardless of platform, though, it works to specify ports: for your service, and then access those published ports using the host's IP address (localhost from the same host outside a container; the VM's IP address if you're using Docker Toolbox). You should almost never need the manual IP configuration you show in the question.

    version: '3.8'
    # Compose provides a "default" network; just use that
    services:
      sql:
        image: mcr.microsoft.com/mssql/server:latest
        # Compose provides
        # networks: [default]
        # But to make the container accessible
        ports:
          - '1433:1433' # second port _must_ be 1433, first can be anything
        environment:
          - ACCEPT_EULA: Y
          - SA_PASSWORD: My_password123
        volumes:
          - "mssql:/var/opt"
        restart: unless-stopped
    volumes:
      mssql:
    

    With this configuration you can access localhost and port 1433 (or, if you changed the first ports: number, that alternate port).