Linking between containers in a version 2 docker-compose file is not working.
Only when using the 'old' version 1 format, I do see the link in /etc/hosts of the container.
I have the following basic version 2 docker-compose.yml file.
version: '2' services: my-app: image: tomcat:8.0 container_name: my-app1 links: - my-redis my-redis: image: redis container_name: my-redis1
When I run the following command:
docker-compose up -d
I see that two containers are started, but no link is created in the /etc/hosts file:
docker exec -it my-app1 cat /etc/hosts
127.0.0.1 localhost ::1 localhost ip6-localhost ip6-loopback fe00::0 ip6-localnet ff00::0 ip6-mcastprefix ff02::1 ip6-allnodes ff02::2 ip6-allrouters 172.18.0.3 2abb84ccada9
From 'my-app1' I can ping the other container using the IP address of 'my-redis1', but I cannot 'ping my-redis1' (based on a name).
What could be the problem here?
Additional information:
If you are running more than one container, you can let your containers communicate with each other by attaching them to the same network. Docker creates virtual networks which let your containers talk to each other. In a network, a container has an IP address, and optionally a hostname.
Warning: The --link flag is a deprecated legacy feature of Docker. It may eventually be removed. Unless you absolutely need to continue using it, we recommend that you use user-defined networks to facilitate communication between two containers instead of using --link.
The New “docker compose” Command Docker Compose v2 brings Compose functionality into Docker itself. You'll be able to use Compose wherever the latest Docker CLI is installed, no extra steps required. Underneath, Docker continues to use the features provided by the Compose project.
With version 2 of docker-compose the 'services' (containers) that are in the same network are linked between them by default.
Using the below docker-compose.yml file
version: '2'
services:
my-app:
image: tomcat:8.0
container_name: my-app1
links:
- my-redis
my-redis:
image: redis
container_name: my-redis1
You just can execute ping my-app
from your my-redis
container and ping my-redis
from your my-app
container to check that they are linked.
For instance:
$ docker-compose up -d
$ docker exec -it my-app1 bash
# ping my-redis
You can get more information about that in the links below: https://blog.docker.com/2016/02/compose-1-6/ https://github.com/docker/compose/blob/master/docs/networking.md
The problem is the firewalld of my Fedora host.
With the firewall temporarily disabled ('systemctl stop firewalld', followed by 'systemctl restart docker') everything works according to the docker documentation.
There seems to be a major problem with firewalld when used with docker, see: https://github.com/docker/docker/issues/16137.
Note that RHEL/Centos 7 also use firewalld.
-Arjen
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With