Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Links between containers not working with docker-compose version 2

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:

  • Docker version 1.10.0, build 590d5108
  • docker-compose version 1.6.0, build d99cad6
  • Linux kernel: 4.3.5-300.fc23.x86_64
like image 993
user2030035 Avatar asked Feb 09 '16 16:02

user2030035


People also ask

Can 2 containers communicate with each other?

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.

Is Docker compose links deprecated?

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.

What is new in Docker compose V2?

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.


Video Answer


2 Answers

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

like image 91
JesusTinoco Avatar answered Nov 11 '22 21:11

JesusTinoco


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

like image 37
user2030035 Avatar answered Nov 11 '22 20:11

user2030035