Need Two docker containers to communicate together.
New here? Learn about Bountify and follow @bountify to get notified of new bounties! x

I've created a Docker/docker-compose script for my php Laravel app.

You can find it here:******

See the docker-compose.yml file here *****

Basically, I have these settings in my .env file:

DOCKER_CONTAINER_NAME_SLUG=docker
DOCKER_APP_PORTS=9000:9000
DOCKER_WEBSERVER_PORTS=80:80
DOCKER_WEBSERVER_SSL_PORTS=4430:4430
DOCKER_MYSQL_PORTS=33060:33060

if I copy the folder and change the variables above to :

DOCKER_CONTAINER_NAME_SLUG=docker-two
DOCKER_APP_PORTS=9001:9000
DOCKER_WEBSERVER_PORTS=8001:80
DOCKER_WEBSERVER_SSL_PORTS=4431:4430
DOCKER_MYSQL_PORTS=33061:33060

Both apps work at http://docker.test/ and http://docker-two.test:8001/

But if I uncomment these lines in docker-two (in my route file web.php):

$response = Http::withHeaders([
])->get('http://docker.test');
return $response;

Then I can see that the docker-two app isn't able to communicate with the first app. I get this error:

cURL error 7: Failed to connect to docker.test port 80: Connection refused (see https://curl.haxx.se/libcurl/c/libcurl-errors.html)

I'll pay extra someone finds a solution.

frow what i know, you have to run them in the same network in order to make them connect with each others.
Chlegou 3 months ago
They are in the same network but have different container names.
randomhelprequested 3 months ago
do you mean different docker instances? because you can't have 2 docker containers in the same docker instance with the same name.
Chlegou 3 months ago
I have dockerapp and docker-twoapp, dockerwebserver and docker-twowebserver and dockerdb and docker-twodb. So the containers have different names.
randomhelprequested 3 months ago
the utility of docker is running multiple containers in the same dockerapp (same server). why you don't run all the containers in the same docker app?
Chlegou 3 months ago
I guess that means I'd need to have just one docker-compose.yml file ? Is that what you mean ?
randomhelprequested 3 months ago
If you're willing to help, we could meet on a slack team. Let me know !!! As long as I can find a way to have multiple websites running and able to communicate with each other on my local machine. One Dockerfile or not I'll be happy. Let me know.
randomhelprequested 3 months ago
i have struggled with this 2 weeks ago :p i would be happy to share the knowledge i have got to help you out. but let me tell you first that i'm a basic learner of docker. this is my email for further contact, we could schedule a Hangout call: nicolastsue@gmail.com
Chlegou 3 months ago
Would you check the solution? Building dockers takes much time, but ypu can believe it's working - I checked it.
TheOsch 3 months ago
3 months ago

Crowdsource coding tasks.

1 Solution


That's where the problem is: the second docker doesn't know the docker.test host. The simplest possible solution is to edit docker-compose.yml:

  1. Change

    container_name: ${DOCKER_CONTAINER_NAME_SLUG}_webserver

    to

    container_name: ${DOCKER_CONTAINER_NAME_SLUG}.test

  2. Add one line to networks section in BOTH directories and another line only in THE SECOND one, So that the whole section will look like this:

    #Docker Networks
    networks:
    ..my-network:
    ....external: true #This only in the second one
    ....name: docker_test_network
    ....driver: bridge
    (sorry, I replaced spaces with dots, couldn't make Bountify editor keep indents).

  3. Uncomment those lines in the web.php of the second server, run the first server then the second one. Will work.

What does it do: it creates a network common for both containers; each service using this network can communicate to other service using the service's name as a hostname, no matter in what container(s) are both services situated.