Partage
  • Partager sur Facebook
  • Partager sur Twitter

Accéder à Docker depuis Postman

Posmtan/Docker/API

Sujet résolu
    20 septembre 2019 à 10:20:53

    Bonjour à tous !

    Je ne sais pas si je suis dans la bonne section mais je pense que mon problème est plutôt orienté réseau...

    Je développe dans mon entreprise une application web qui tourne sous Docker. J'ai donc mon environnement en place sur ma machine et accède à mon application via une URL "jdma.local"

    Étant sous Linux (Mint 19), mon fichier /etc/hosts est configuré comme tel :

    127.0.0.1	localhost
    127.0.1.1	robin-X751LK
    127.0.0.1       jdma.local
    
    172.18.0.2      jdma.local
    172.18.0.6      jdma.local
    172.18.0.7      jdma.local
    172.18.0.8      jdma.local

    La raison pour laquelle j'ai bindé plusieurs IP est le fait que souvent, mon ``docker-compose up -d`` pour démarrer Docker change l'IP de mon container web...

    Par exemple, aujourd'hui, si j'exécute cette commande j'obtiens cette IP :

    robin@robin-X751LK:~/work/ifbn-server$ docker inspect -f '{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' web
    172.18.0.7

    Jusqu'à présent, tout fonctionne très bien. Seulement je commence à développer une API sur ce projet, qui va de paire avec une application mobile en développement. Pour tester cette API, j'utilise Postman.

    Seulement, lorsque dans l'URL, je saisi "jdma.local" ou "http://jdma.local", j'obtiens un très beau Unable to connect... :(

    J'ai désactivé la vérification SSL et les Proxy dans Postman mais rien à faire. Et lorsque je souhaite utiliser l'IP du conteneur qui m'intéresse, j'obtiens une belle erreur 403 Forbidden.... o_O

    Ce que j'ai du mal à comprendre, c'est qu'avec cURL, j'ai des résultats également très bizarres. Par exemple, si j'exécute ceci :

    robin@robin-X751LK:~/work/ifbn-server$ curl --verbose jdma.local
    * Rebuilt URL to: jdma.local/
    *   Trying 127.0.0.1...
    * TCP_NODELAY set
    * connect to 127.0.0.1 port 80 failed: Connection refused
    *   Trying 172.18.0.2...
    * TCP_NODELAY set
    * connect to 172.18.0.2 port 80 failed: Connection refused
    *   Trying 172.18.0.6...
    * TCP_NODELAY set
    * connect to 172.18.0.6 port 80 failed: Connection refused
    *   Trying 172.18.0.7...
    * TCP_NODELAY set
    * Connected to jdma.local (172.18.0.7) port 80 (#0)
    > GET / HTTP/1.1
    > Host: jdma.local
    > User-Agent: curl/7.58.0
    > Accept: */*
    > 
    < HTTP/1.1 302 Found
    < Server: nginx
    < Content-Type: text/html; charset=UTF-8
    < Transfer-Encoding: chunked
    < Connection: keep-alive
    < Cache-Control: no-cache
    < Location: /en/
    < Date: Fri, 20 Sep 2019 08:16:11 GMT
    < Set-Cookie: hl=en; expires=Sat, 19-Sep-2020 08:16:11 GMT; Max-Age=31536000; path=/
    < 
    <!DOCTYPE html>
    <html>
        <head>
            <meta charset="UTF-8" />
            <meta http-equiv="refresh" content="1;url=/en/" />
    
            <title>Redirecting to /en/</title>
        </head>
        <body>
            Redirecting to <a href="/en/">/en/</a>.
        </body>
    * Connection #0 to host jdma.local left intact
    

    Je constate que l'IP matchée correspond bien à 172.18.0.7:80, mais si j'essaie de charger cette même IP directement :

    robin@robin-X751LK:~/work/ifbn-server$ curl --verbose 172.18.0.7:80
    * Rebuilt URL to: 172.18.0.7:80/
    *   Trying 172.18.0.7...
    * TCP_NODELAY set
    * Connected to 172.18.0.7 (172.18.0.7) port 80 (#0)
    > GET / HTTP/1.1
    > Host: 172.18.0.7
    > User-Agent: curl/7.58.0
    > Accept: */*
    > 
    < HTTP/1.1 403 Forbidden
    < Server: nginx
    < Date: Fri, 20 Sep 2019 08:16:31 GMT
    < Content-Type: text/html
    < Content-Length: 162
    < Connection: keep-alive
    < 
    <html>
    <head><title>403 Forbidden</title></head>
    <body bgcolor="white">
    <center><h1>403 Forbidden</h1></center>
    <hr><center>nginx</center>
    </body>
    </html>
    * Connection #0 to host 172.18.0.7 left intact
    

    Cette fois ça ne passe pas ! >_<


    J'ai peu de compétences en Docker/Réseaux, alors je rate peut-être un truc évident ?

    Merci d'avance !

    • Partager sur Facebook
    • Partager sur Twitter
    Celui qui croit tout savoir ne sait rien
      20 septembre 2019 à 10:43:16

      Bonjour elalitte, et merci pour ta réponse !

      Dans mon docker-compose :

      version: '2.0'
      services:
        lb:
         image: traefik:1.5-alpine
         restart: always
         command: --web --acme.storage=/etc/traefik/acme.json --logLevel=debug \
                  ${TRAEFIK_ENTRYPOINT_HTTP} ${TRAEFIK_ENTRYPOINT_HTTPS}\
                  --defaultentrypoints=${TRAEFIK_DEFAULT_ENTRYPOINTS} \
                  --acme=${ACME_ENABLE} --acme.entrypoint=https --acme.httpchallenge --acme.httpchallenge.entrypoint=http \
                  --acme.domains="${ACME_DOMAINS}" --acme.email="${ACME_EMAIL}" \
                  --docker --docker.domain="${DOCKER_DOMAIN}" --docker.endpoint="unix:///var/run/docker.sock" \
                  --docker.watch=true --docker.exposedbydefault="false"
         container_name: traefik
         networks:
           - core
         ports:
           - "80:80"
           - "443:443"
      #     - "8080:8080"
         volumes:
           - ./docker/traefik:/etc/traefik/
           - /var/run/docker.sock:/var/run/docker.sock
      
        web:
          image: "mobiled/nginx"
          container_name: web
          depends_on:
            - php
          networks:
            - core
      #    ports:
      #      - "80:80"
          volumes:
            - ./src/:/var/www/
          labels:
            - "traefik.backend=nginx"
            - "traefik.frontend.rule=Host:${TRAEFIK_HOST}"
            - "traefik.port=80"
            - "traefik.frontend.entryPoints=http,https"
            - "traefik.backend.loadbalancer.method=wrr"
            - "traefik.enable=true"

      Où devrais-je indiquer l'IP ? En sachant que je dispose d'un .env contnant notamment :

      DOCKER_DOMAIN=http://jdma.local
      
      TRAEFIK_HOST=http://jdma.local
      TRAEFIK_DEFAULT_ENTRYPOINTS=http,https
      TRAEFIK_ENTRYPOINT_HTTP=--entryPoints="Name:http Address::80"
      TRAEFIK_ENTRYPOINT_HTTPS=--entryPoints="Name:https Address::443 TLS"

      Je vois que dans mon docker-compose, j'ai une ligne commentée au niveau du port. C'est peut-être stupide mais je vais essayer de la remettre.

      Je tiens à préciser qu'on m'a fourni ce fichier en l'état, donc je n'ai (quasiment) aucune idée de ce que tout ça signifie :euh:

      [EDIT]

      Après modification, cela semble fonctionner !

      Je ne comprends pas pourquoi le port était commenté... mais au moins c'est résolu ! Désolé du dérangement :o

      Cependant @elalitte, si tu as une idée pour empêcher mon IP de toujours changer, je serais ravi d'avoir un peu plus de précisions !

      Bonne journée à tous !

      -
      Edité par Algorun 20 septembre 2019 à 11:12:41

      • Partager sur Facebook
      • Partager sur Twitter
      Celui qui croit tout savoir ne sait rien

      Accéder à Docker depuis Postman

      × Après avoir cliqué sur "Répondre" vous serez invité à vous connecter pour que votre message soit publié.
      × Attention, ce sujet est très ancien. Le déterrer n'est pas forcément approprié. Nous te conseillons de créer un nouveau sujet pour poser ta question.
      • Editeur
      • Markdown