问题
I have a ReactJS project with its own Dockerfile, exposing port 3000:3000.
I also have a PHP project with its own Dockerfile, exposing port 80:80. The PHP app also has containers for MySQL, Redis and Nginx
For the PHP app, I have a docker-compose file that creates a network (my-net
) for PHP, Nginx, MySQL and Redis to communicate on. However, I now want the ReactJS (which is in a separate project) to be able to communicate with the PHP app.
I added a docker-compose file to the React project, and added it to the network from the PHP project my-net
and declared it as external
so that it doesn't try to create it.
This seems to work: From the ReactJS container, I can ping app
(the name of my backend service) and it works properly. However, from the ReactJS code, if I use something like axios
to try and hit the backend API, it can't resolve app
or http://app
or any variation. It can however access the underlying IP address if I substitute that into in axios
.
So there seems to be some issue with the hostname resolution, and presumably this is on the axios
/ JavaScript end. is there something I'm missing or a reason this isn't working?
回答1:
When the JavaScript runs in a browser (outside of Docker) you can not use app
because that is only available inside the Docker network (via the embedded DNS server).
To access your PHP server from outside use localhost and the exposed port (80) instead.
来源:https://stackoverflow.com/questions/46080290/reactjs-browser-app-cannot-see-things-in-the-docker-compose-network