Answer a question

I'm trying to connect a simple Dart web server to a php website, but it seems like other containers can't reach it even though I have bound the HTTP server to 0.0.0.0. How can I resolve it? Thanks anyway.

docker-compose.yaml

version: '3'
services: 
    my-api:
        build: ./server_api
        ports: 
            - 5060:5060
    website:
        image: php:apache
        volumes: 
            -  ./website:/var/www/html
        ports: 
            - 5000:80
        depends_on: 
            - my-api

Dart HTTP server dockerfile

FROM google/dart

WORKDIR  /usr/src/app
COPY bin .
COPY pubspec.yaml .

RUN pub get
ADD . /app
RUN pub get --offline

CMD []
ENTRYPOINT ["/usr/bin/dart", "main.dart"]

Dart Http server code

import 'dart:convert';
import 'dart:io';

void main() async {
  print('hello 2020');

  var server = await HttpServer.bind('0.0.0.0', 5060);

  var jsonData = {
    'prodotti': ["cozza", "vino", "prosciutto"]
  };

  server.listen((HttpRequest request) {

    print('Yeah');

    request.response
      ..statusCode = HttpStatus.accepted
      ..write(jsonEncode(jsonData));
  });
}

index.php

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Documento di legge</title>
</head>
<body>

<?php
    $json = file_get_contents('http://my-api/');
    echo("<h1>$json</h1>");
?>
</body>
</html>

Answers

in your php, you have$json = file_get_contents('http://my-api/');, but you start dart on port 5060 - var server = await HttpServer.bind('0.0.0.0', 5060); Try:

$json = file_get_contents('http://my-api:5060/');
Logo

云原生社区为您提供最前沿的新闻资讯和知识内容

更多推荐