John Davidson

php - cURL same server from a inside docker container

0 comments
Message:


I saw a lot of similar problem but not exactly like mine...
I have a classic architecture,


A server with docker, a stack with Traefik to dispatch request,
and few stack of nginx/apache and php. Let's say



  • website1.com

  • website2.com


The problem is that I can't cURL another website from inside a docker container.





If I try to curl from server itself


curl -v website2.com
curl -v https://website2.com

I get a valid response




If I try to curl from website1 container


curl -v website2.com
curl -v https://website2.com

I can see



Trying XX.XX.XX.XX:443...



With my external IP (so the DNS should be OK) but I don't get any response so it end with a timeout message.




If I add 192.168.48.4 website2.com into my /etc/hosts and I try to cURL, I get a valid response but tell me if I'm wrong but in this case it access through local and not from outside of the server




By the way, I need to cURL from a "manager" website that get all domains from an API, so cURL should be dynamic and I can't really add all IP manually into a hosts file or whatever


I really want to access other containers from outside, like an external service


Do you have any suggestion ?
Thank you guys



My Answer:
docker container to another website on the same server, not from the server itself.

One possible solution is to use the docker network to allow communication between containers. You can create a custom network and attach both website1 and website2 containers to that network. This way, they can communicate with each other using their container names as hostnames.

Here's an example of how you can create a custom network and attach containers to it:

1. Create a custom network:

docker network create mynetwork


2. Attach website1 container to the custom network:

docker network connect mynetwork website1


3. Attach website2 container to the custom network:

docker network connect mynetwork website2


4. Now, you should be able to cURL website2 from website1 container using its container name as the hostname:

docker exec -it website1 curl website2


This way, the communication between containers will go through the docker network and should work without any issues.

Rate this post

3 of 5 based on 4145 votes

Comments




© 2024 Hayatsk.info - Personal Blogs Platform. All Rights Reserved.
Create blog  |  Privacy Policy  |  Terms & Conditions  |  Contact Us