NGINX HTTP Request To Linked Container












0














This is my docker-compose file which starts up a NodeJS/PM2 container and a React/Nginx container on the same host.



version: '3'
services:
nodejs:
image: nodejs_pm2:1.00
container_name: NODE_CONTAINER
ports:
- "8000:8000"
build:
context: ./nodejs
dockerfile: Dockerfile-nodejs
react:
image: react_nginx:1.00
container_name: REACT_CONTAINER
ports:
- "3000:3000"
build:
context: ./react-app
dockerfile: Dockerfile-react
depends_on:
- nodejs


As you can see, my react service depends_on nodejs. This should link the containers, correct?



Question 1) Assuming that's true, what's the proper way to make an HTTP request to my node backend?



Here's what I've tried doing to my nginx config file:



upstream backend {
server nodejs:8000
}

server {
listen 3000;

location ~ ^/api/[0-9a-z]+/$ {
proxy_pass http://backend/;
include /etc/nginx/proxy_params;
}

location / {
root /usr/share/nginx/html;
index index.html index.htm;
try_files $uri $uri/ /index.html;
}
}


This does not work. In fact this totally prevents the front end from showing up at all. What I'm trying to do is something like this axios.get("http://backend/api/*") from my react app. I've looked up several stackoverflow post and none of them seem to have worked for me.



Question 2) Follow up question, what's the point of linking containers if
two containers live on the same host, couldn't I just do a request to localhost:port like normal?










share|improve this question



























    0














    This is my docker-compose file which starts up a NodeJS/PM2 container and a React/Nginx container on the same host.



    version: '3'
    services:
    nodejs:
    image: nodejs_pm2:1.00
    container_name: NODE_CONTAINER
    ports:
    - "8000:8000"
    build:
    context: ./nodejs
    dockerfile: Dockerfile-nodejs
    react:
    image: react_nginx:1.00
    container_name: REACT_CONTAINER
    ports:
    - "3000:3000"
    build:
    context: ./react-app
    dockerfile: Dockerfile-react
    depends_on:
    - nodejs


    As you can see, my react service depends_on nodejs. This should link the containers, correct?



    Question 1) Assuming that's true, what's the proper way to make an HTTP request to my node backend?



    Here's what I've tried doing to my nginx config file:



    upstream backend {
    server nodejs:8000
    }

    server {
    listen 3000;

    location ~ ^/api/[0-9a-z]+/$ {
    proxy_pass http://backend/;
    include /etc/nginx/proxy_params;
    }

    location / {
    root /usr/share/nginx/html;
    index index.html index.htm;
    try_files $uri $uri/ /index.html;
    }
    }


    This does not work. In fact this totally prevents the front end from showing up at all. What I'm trying to do is something like this axios.get("http://backend/api/*") from my react app. I've looked up several stackoverflow post and none of them seem to have worked for me.



    Question 2) Follow up question, what's the point of linking containers if
    two containers live on the same host, couldn't I just do a request to localhost:port like normal?










    share|improve this question

























      0












      0








      0







      This is my docker-compose file which starts up a NodeJS/PM2 container and a React/Nginx container on the same host.



      version: '3'
      services:
      nodejs:
      image: nodejs_pm2:1.00
      container_name: NODE_CONTAINER
      ports:
      - "8000:8000"
      build:
      context: ./nodejs
      dockerfile: Dockerfile-nodejs
      react:
      image: react_nginx:1.00
      container_name: REACT_CONTAINER
      ports:
      - "3000:3000"
      build:
      context: ./react-app
      dockerfile: Dockerfile-react
      depends_on:
      - nodejs


      As you can see, my react service depends_on nodejs. This should link the containers, correct?



      Question 1) Assuming that's true, what's the proper way to make an HTTP request to my node backend?



      Here's what I've tried doing to my nginx config file:



      upstream backend {
      server nodejs:8000
      }

      server {
      listen 3000;

      location ~ ^/api/[0-9a-z]+/$ {
      proxy_pass http://backend/;
      include /etc/nginx/proxy_params;
      }

      location / {
      root /usr/share/nginx/html;
      index index.html index.htm;
      try_files $uri $uri/ /index.html;
      }
      }


      This does not work. In fact this totally prevents the front end from showing up at all. What I'm trying to do is something like this axios.get("http://backend/api/*") from my react app. I've looked up several stackoverflow post and none of them seem to have worked for me.



      Question 2) Follow up question, what's the point of linking containers if
      two containers live on the same host, couldn't I just do a request to localhost:port like normal?










      share|improve this question













      This is my docker-compose file which starts up a NodeJS/PM2 container and a React/Nginx container on the same host.



      version: '3'
      services:
      nodejs:
      image: nodejs_pm2:1.00
      container_name: NODE_CONTAINER
      ports:
      - "8000:8000"
      build:
      context: ./nodejs
      dockerfile: Dockerfile-nodejs
      react:
      image: react_nginx:1.00
      container_name: REACT_CONTAINER
      ports:
      - "3000:3000"
      build:
      context: ./react-app
      dockerfile: Dockerfile-react
      depends_on:
      - nodejs


      As you can see, my react service depends_on nodejs. This should link the containers, correct?



      Question 1) Assuming that's true, what's the proper way to make an HTTP request to my node backend?



      Here's what I've tried doing to my nginx config file:



      upstream backend {
      server nodejs:8000
      }

      server {
      listen 3000;

      location ~ ^/api/[0-9a-z]+/$ {
      proxy_pass http://backend/;
      include /etc/nginx/proxy_params;
      }

      location / {
      root /usr/share/nginx/html;
      index index.html index.htm;
      try_files $uri $uri/ /index.html;
      }
      }


      This does not work. In fact this totally prevents the front end from showing up at all. What I'm trying to do is something like this axios.get("http://backend/api/*") from my react app. I've looked up several stackoverflow post and none of them seem to have worked for me.



      Question 2) Follow up question, what's the point of linking containers if
      two containers live on the same host, couldn't I just do a request to localhost:port like normal?







      node.js http docker nginx containers






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 23 '18 at 1:15









      Phillip

      376318




      376318
























          1 Answer
          1






          active

          oldest

          votes


















          0














          you actually have 3 questions:



          Question 0 This should link the containers, correct?



          Nope, depends_on defines container dependency (eg, wait for container A to start before container B), not links



          Question 1 what's the proper way to make an HTTP request to my node backend?



          Your 2 containers can connect to each other using the container_name property
          So for your nginx.config, you should be able to connect to the nodejs container thru NODE_CONTAINER:8000



          Question 2* what's the point of linking containers if two containers live on the same host, couldn't I just do a request to localhost:port like normal?



          Please refer to this, it will explain the container networking and linking concepts better than me https://docs.docker.com/compose/networking/






          share|improve this answer





















          • I tried doing NODE_CONTAINER:8000 in my http request and got Failed to load resource: net::ERR_NAME_NOT_RESOLVED
            – Phillip
            Nov 23 '18 at 3:11










          • If I go to http://localhost:8000/ it works fine.
            – Phillip
            Nov 23 '18 at 3:11










          • Are you testing those connectivities from the docker host or from inside the container? - NODE_CONTAINER will only get resolved from inside the containers as they are automatically connected to a default bridged docker network - http://localhost:8000 works since it that port was exposed in the docker-compose file For troubleshooting, you can try to get inside one of the container by: - check the container ids or name: docker-ps - `docker exec -it <container id or name> /bin/sh
            – Domingo Tamayo
            Nov 23 '18 at 11:41













          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53439618%2fnginx-http-request-to-linked-container%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          0














          you actually have 3 questions:



          Question 0 This should link the containers, correct?



          Nope, depends_on defines container dependency (eg, wait for container A to start before container B), not links



          Question 1 what's the proper way to make an HTTP request to my node backend?



          Your 2 containers can connect to each other using the container_name property
          So for your nginx.config, you should be able to connect to the nodejs container thru NODE_CONTAINER:8000



          Question 2* what's the point of linking containers if two containers live on the same host, couldn't I just do a request to localhost:port like normal?



          Please refer to this, it will explain the container networking and linking concepts better than me https://docs.docker.com/compose/networking/






          share|improve this answer





















          • I tried doing NODE_CONTAINER:8000 in my http request and got Failed to load resource: net::ERR_NAME_NOT_RESOLVED
            – Phillip
            Nov 23 '18 at 3:11










          • If I go to http://localhost:8000/ it works fine.
            – Phillip
            Nov 23 '18 at 3:11










          • Are you testing those connectivities from the docker host or from inside the container? - NODE_CONTAINER will only get resolved from inside the containers as they are automatically connected to a default bridged docker network - http://localhost:8000 works since it that port was exposed in the docker-compose file For troubleshooting, you can try to get inside one of the container by: - check the container ids or name: docker-ps - `docker exec -it <container id or name> /bin/sh
            – Domingo Tamayo
            Nov 23 '18 at 11:41


















          0














          you actually have 3 questions:



          Question 0 This should link the containers, correct?



          Nope, depends_on defines container dependency (eg, wait for container A to start before container B), not links



          Question 1 what's the proper way to make an HTTP request to my node backend?



          Your 2 containers can connect to each other using the container_name property
          So for your nginx.config, you should be able to connect to the nodejs container thru NODE_CONTAINER:8000



          Question 2* what's the point of linking containers if two containers live on the same host, couldn't I just do a request to localhost:port like normal?



          Please refer to this, it will explain the container networking and linking concepts better than me https://docs.docker.com/compose/networking/






          share|improve this answer





















          • I tried doing NODE_CONTAINER:8000 in my http request and got Failed to load resource: net::ERR_NAME_NOT_RESOLVED
            – Phillip
            Nov 23 '18 at 3:11










          • If I go to http://localhost:8000/ it works fine.
            – Phillip
            Nov 23 '18 at 3:11










          • Are you testing those connectivities from the docker host or from inside the container? - NODE_CONTAINER will only get resolved from inside the containers as they are automatically connected to a default bridged docker network - http://localhost:8000 works since it that port was exposed in the docker-compose file For troubleshooting, you can try to get inside one of the container by: - check the container ids or name: docker-ps - `docker exec -it <container id or name> /bin/sh
            – Domingo Tamayo
            Nov 23 '18 at 11:41
















          0












          0








          0






          you actually have 3 questions:



          Question 0 This should link the containers, correct?



          Nope, depends_on defines container dependency (eg, wait for container A to start before container B), not links



          Question 1 what's the proper way to make an HTTP request to my node backend?



          Your 2 containers can connect to each other using the container_name property
          So for your nginx.config, you should be able to connect to the nodejs container thru NODE_CONTAINER:8000



          Question 2* what's the point of linking containers if two containers live on the same host, couldn't I just do a request to localhost:port like normal?



          Please refer to this, it will explain the container networking and linking concepts better than me https://docs.docker.com/compose/networking/






          share|improve this answer












          you actually have 3 questions:



          Question 0 This should link the containers, correct?



          Nope, depends_on defines container dependency (eg, wait for container A to start before container B), not links



          Question 1 what's the proper way to make an HTTP request to my node backend?



          Your 2 containers can connect to each other using the container_name property
          So for your nginx.config, you should be able to connect to the nodejs container thru NODE_CONTAINER:8000



          Question 2* what's the point of linking containers if two containers live on the same host, couldn't I just do a request to localhost:port like normal?



          Please refer to this, it will explain the container networking and linking concepts better than me https://docs.docker.com/compose/networking/







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 23 '18 at 1:30









          Domingo Tamayo

          12612




          12612












          • I tried doing NODE_CONTAINER:8000 in my http request and got Failed to load resource: net::ERR_NAME_NOT_RESOLVED
            – Phillip
            Nov 23 '18 at 3:11










          • If I go to http://localhost:8000/ it works fine.
            – Phillip
            Nov 23 '18 at 3:11










          • Are you testing those connectivities from the docker host or from inside the container? - NODE_CONTAINER will only get resolved from inside the containers as they are automatically connected to a default bridged docker network - http://localhost:8000 works since it that port was exposed in the docker-compose file For troubleshooting, you can try to get inside one of the container by: - check the container ids or name: docker-ps - `docker exec -it <container id or name> /bin/sh
            – Domingo Tamayo
            Nov 23 '18 at 11:41




















          • I tried doing NODE_CONTAINER:8000 in my http request and got Failed to load resource: net::ERR_NAME_NOT_RESOLVED
            – Phillip
            Nov 23 '18 at 3:11










          • If I go to http://localhost:8000/ it works fine.
            – Phillip
            Nov 23 '18 at 3:11










          • Are you testing those connectivities from the docker host or from inside the container? - NODE_CONTAINER will only get resolved from inside the containers as they are automatically connected to a default bridged docker network - http://localhost:8000 works since it that port was exposed in the docker-compose file For troubleshooting, you can try to get inside one of the container by: - check the container ids or name: docker-ps - `docker exec -it <container id or name> /bin/sh
            – Domingo Tamayo
            Nov 23 '18 at 11:41


















          I tried doing NODE_CONTAINER:8000 in my http request and got Failed to load resource: net::ERR_NAME_NOT_RESOLVED
          – Phillip
          Nov 23 '18 at 3:11




          I tried doing NODE_CONTAINER:8000 in my http request and got Failed to load resource: net::ERR_NAME_NOT_RESOLVED
          – Phillip
          Nov 23 '18 at 3:11












          If I go to http://localhost:8000/ it works fine.
          – Phillip
          Nov 23 '18 at 3:11




          If I go to http://localhost:8000/ it works fine.
          – Phillip
          Nov 23 '18 at 3:11












          Are you testing those connectivities from the docker host or from inside the container? - NODE_CONTAINER will only get resolved from inside the containers as they are automatically connected to a default bridged docker network - http://localhost:8000 works since it that port was exposed in the docker-compose file For troubleshooting, you can try to get inside one of the container by: - check the container ids or name: docker-ps - `docker exec -it <container id or name> /bin/sh
          – Domingo Tamayo
          Nov 23 '18 at 11:41






          Are you testing those connectivities from the docker host or from inside the container? - NODE_CONTAINER will only get resolved from inside the containers as they are automatically connected to a default bridged docker network - http://localhost:8000 works since it that port was exposed in the docker-compose file For troubleshooting, you can try to get inside one of the container by: - check the container ids or name: docker-ps - `docker exec -it <container id or name> /bin/sh
          – Domingo Tamayo
          Nov 23 '18 at 11:41




















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.





          Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


          Please pay close attention to the following guidance:


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53439618%2fnginx-http-request-to-linked-container%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          Berounka

          Sphinx de Gizeh

          Different font size/position of beamer's navigation symbols template's content depending on regular/plain...