Let's say we have two containers on the same host, run these commands
Create a network:
docker network create -d bridge "your-created-network" // Your network, you can name anything
docker run -d -p 8080:80 --network="your-created-network" --name=nginxone nginx:alpine
and change the nginx port to 8081 to up another container of nginx
docker run -d -p 8081:80 --network="your-created-network" --name=nginxtwo nginx:alpine
to communicate between these two containers you need to execute:
docker exec -it nginxtwo sh
ping nginxone // this will work
if you want to communicate between two specific containers, add your created network to those containers.
For the second case, I haven't tried but you can use boot2docker or apache.