NGINX HTTP Request To Linked Container
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
add a comment |
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
add a comment |
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
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
node.js http docker nginx containers
asked Nov 23 '18 at 1:15
Phillip
376318
376318
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
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/
I tried doingNODE_CONTAINER:8000
in my http request and gotFailed to load resource: net::ERR_NAME_NOT_RESOLVED
– Phillip
Nov 23 '18 at 3:11
If I go tohttp://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 thedocker-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
add a comment |
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
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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/
I tried doingNODE_CONTAINER:8000
in my http request and gotFailed to load resource: net::ERR_NAME_NOT_RESOLVED
– Phillip
Nov 23 '18 at 3:11
If I go tohttp://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 thedocker-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
add a comment |
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/
I tried doingNODE_CONTAINER:8000
in my http request and gotFailed to load resource: net::ERR_NAME_NOT_RESOLVED
– Phillip
Nov 23 '18 at 3:11
If I go tohttp://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 thedocker-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
add a comment |
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/
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/
answered Nov 23 '18 at 1:30
Domingo Tamayo
12612
12612
I tried doingNODE_CONTAINER:8000
in my http request and gotFailed to load resource: net::ERR_NAME_NOT_RESOLVED
– Phillip
Nov 23 '18 at 3:11
If I go tohttp://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 thedocker-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
add a comment |
I tried doingNODE_CONTAINER:8000
in my http request and gotFailed to load resource: net::ERR_NAME_NOT_RESOLVED
– Phillip
Nov 23 '18 at 3:11
If I go tohttp://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 thedocker-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
add a comment |
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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