Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Docker - send http request from one container to another

Tags:

docker

reactjs

I cannot send an HTTP request to backend container when I'm running app on AWS server production. However, when I'm running app locally I can make requests to backend just fine. For making requests I use fetch:

fetch('http://localhost:8000/something')

Here is how project structure looks like:

.
├── docker-compose.yml
|
├── backend
│   ├── Dockerfile
│   └── server.js
|
└── frontend
    ├── Dockerfile
    ├── package.json
    ├── public
    │   └── index.html
    └── src
       ├── components
       ├── data
       ├── index.js
       ├── routes.js
       ├── static
       ├── tests
       └── views

docker-compose.yml:

version: '3'

services:
  frontend:
    build:
      context: .
      dockerfile: frontend/Dockerfile
    volumes:
      - ./frontend:/frontend
    ports:
      - "80:5000"
    links:
      - backend
  backend:
    build:
      context: .
      dockerfile: backend/Dockerfile
    volumes:
      - ./backend:/backend
    ports:
      - "8000:8000"

Dockerfile in frontend:

FROM node:latest

RUN mkdir -p /frontend

WORKDIR /frontend

ADD . /frontend

VOLUME ["/frontend"]

EXPOSE 5000

CMD yarn && yarn build && yarn global add serve && serve -s build

Dockerfile in backend:

FROM node:latest

RUN mkdir -p /backend

WORKDIR /backend

ADD . /backend

VOLUME ["/backend"]

EXPOSE 8000

CMD yarn && yarn start

Can someone explain me what is wrong with my config? I'm very confused, because it works without any issues locally.

like image 467
sympi Avatar asked May 08 '17 16:05

sympi


People also ask

Can 2 Docker containers talk to each other?

If you are running more than one container, you can let your containers communicate with each other by attaching them to the same network. Docker creates virtual networks which let your containers talk to each other. In a network, a container has an IP address, and optionally a hostname.

How do you communicate between containers in Docker?

If you are running more than one container, you can let your containers communicate with each other by attaching them to the same network. Docker creates virtual networks which let your containers talk to each other. In a network, a container has an IP address, and optionally a hostname.

Does Docker use port 8080?

Docker also finds ports you expose with --expose 8080 (assuming you want to expose port 8080). Docker maps all of these ports to a host port within a given epehmeral port range . You can find the configuration for these ports (usually 32768 to 61000) in /proc/sys/net/ipv4/ip_local_port_range .

Can Docker containers communicate with each other by default?

Containers can only communicate with each other if they share a network. Containers that don't share a network cannot communicate with one another. That's one of the isolation features provided by Docker. A container can belong to more than one network, and a network can have multiple containers inside.


1 Answers

TLDR: Need to change the frontend code to call the current host instead of 'localhost'

The problem is your app is saying 'hey localhost' instead of 'hey VPS ip', when visiting from YOUR browser. You need to edit your frontend code to visit the current host you're visiting. That's why you're receiving a request on YOUR localhost server.

Instead of fetch("http:///localhost:8000/something") change it to fetch("http://"+location.host+":8000") (There are better ways, this gets it done).

Also note docker containers are a little different in terms of networking as well. A docker container doesn't really have a concept of 'localhost' the same way non docker container apps do. You have to use the VPS's IP/Local IP when making the call from server to server. A trick I use is to use docker's default docker0 bridge 172.17.0.1.

I tend to use networks over 'links' and actually cant comment fully on it, but when containers were on the same docker network, you could access the other container by using the container's name. This only works for server side code however, ie: node.js server -> node.js server/mongo db. Example mongodb connection would be mongodb://mongo_server:27017/mydatabase and mongo_server would resolve to the container's IP.

Another thing you'll possibly encounter when attempting to use the IP is your firewall, you would have to allow that particular ip/port in through your firewall as well.

like image 104
C Jones Avatar answered Sep 20 '22 23:09

C Jones