1

I have 2 containers:

  • nginx(serving React app)
  • node

Both inside the same docker-compose file.

When i run it locally ('npm start' for React and node index.js for backend) they work fine (backend receive request) - but when they are both in containers, something goes wrong..

** when i exec the nginx container - it does recognize http://server:5000/ endpoint (with curl)

docker-compose.yaml

version: "3.8"
services:
    server:
        build:
            context: ./server
        ports:
            - "5000:5000"
        networks:
          - frontend
    nginx:
        build:
            context: ./app
        restart: always
        ports:
            - "80:80"
        networks:
          - frontend
          
networks:
  frontend:

request part on frontend

async function sendMsg(...order){
    await axios.put('http://server:5000/msg', order)
}

receive part on backend

app.put('/msg', (req, res) => {
  console.log(req.body)
}

1 Answer 1

3

The problem is your react app is served by your container but is executing in your browser.

Your browser doesn't know what "server" refers to.

Try changing http://server:5000/msg to http://localhost:5000/msg

Also make sure your node server is running.

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks a lot ! a question about it - i expected the nginx localhost to be the container itself, how does it refer to another container - the node one?
Your Nginx server can refer to your node container with server in your .conf file for example but your react app won't be able to because it's not executed in the container but in the browser. You could redirect the request to your Nginx server to your node api in your .conf file. This tutorial could help you achieve what you want: section.io/engineering-education/…
Will take a look,thanks..for some reason i'm facing another problem.i run the compose file on a EC2,opened ports 80 and 5000 but frontend get 'net::ERR_CONNECTION_REFUSED' error when trying to send the PUT request. it did worked locally

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.