If you’re trying to make requests between Docker containers, it’s not as straight-forward as updating the URLs for the requests.
Here’s how to let Docker containers communicate and resolve connection errors such as:
cURL error 7: Failed to connect to localhost port 8080: Connection refused
The Docker containers must be in the same network to communicate.
$ docker network create my_network $ docker network connect my_network myapi_php_1 $ docker network connect my_network mysite_wordpress_1
You can then confirm the containers are connected to the same network by executing:
$ docker network inspect my_network
Confirm by looking in the returned JSON at the paths
Containers.[id].Name to confirm the container names are listed.
Now you may be surprised that you still can’t get the containers to communicate.
What you need to do is use the container’s name in place of the domain name for your HTTP request:
| || |
If you need the containers to always be in the same network, look at adding the
networks key to your