Cannot setup cluster of ES with Docker of 2 EC2 machines on 3 nodes

The doc assumes that everything is going to run in one machine, your architecture is completely different.

If you are running the same docker-compose in both machines you are starting 6 nodes, 3 on each ec2 instance.

I do not know much about docker, but I don't think that using the IP address of your host as the publish address of your container will work like that as the containers will run on a different network created by docker.

From the docker documentation you have this:

Bridge networks apply to containers running on the same Docker daemon host. For communication among containers running on different Docker daemon hosts, you can either manage routing at the OS level, or you can use an overlay network.

Which basically means that your containers will only be able to connect witch containers running on the same ec2 instance.

Your issue is more related to your network architecture, you just need to make sure that your containers can talk with each other on the publish address.

Maybe the discussion in this post can help a little.