Links between containers not working with docker-compose version 2

docker-compose connect to host
docker-compose links
docker-compose network subnet
docker-compose host network
docker-compose host ip
docker-compose named volumes
docker-compose bind mount
docker-compose multiple containers

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'

    image: tomcat:8.0
    container_name: my-app1
      - 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       localhost
    ::1     localhost ip6-localhost ip6-loopback
    fe00::0 ip6-localnet
    ff00::0 ip6-mcastprefix
    ff02::1 ip6-allnodes
    ff02::2 ip6-allrouters      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

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'

    image: tomcat:8.0
    container_name: my-app1
      - 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:

Links between containers doesn't work with version 2 · Issue #2841 , But while starting containers with docker-compose up command, I see following error: web_1 | PG::ConnectionBad: could not translate host name  According to docker-compose network documentation, it should work without link declaration. By default Compose sets up a single network for your app. Each container for a service joins the default network and is both reachable by other containers on that network,

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:

Note that RHEL/Centos 7 also use firewalld.


Networking in Compose, I have a very simple 2 container application, 1 running Tomcat the other Redis. use docker-compose but the link between the containers is not working. between the containers. version: '2' services: web: image: web links: Version 2 of the Compose file format instructs Compose to use the new networking system, so there are no environment variables, and so the haproxy container can't find the web container. The reason it works with docker run is that it still uses the bridge network by default, which preserves old-style links.

In my case the problem was in service name.

version: "2"
       build: auth-server
           - "8082:8082"

       build: core-api
           - "8081:8081"   
           - my_auth_server:auth-server # <-- here changed from auth_server to auth-server

Compose file version 2 reference, Learn how to connect Docker containers together. One feature that user-​defined networks do not support that you can do with --link is sharing environment variables python 5 seconds ago Up 2 seconds​49155->5000/tcp nostalgic_morse This is also useful for showing you specific port configurations. When docker-compose executes V2 files, it will automatically build a network between all of the containers defined in the file, and every container will be immediately able to refer to the others just using the names defined in the docker-compose.yml file.

Docker-compose not creating links between containers, In the majority of cases, moving from version 1 to 2 is a very when running version 2 projects, so links behave slightly If you're using net: "container:[​container name/id]" , the value does not need to  Not supported for version 2 docker-compose files. Use network_mode instead. name. Added in version 2.1 file format. Set a custom name for this network. The name field can be used to reference networks which contain special characters. The name is used as is and will not be scoped with the stack name.

Legacy container links, There are several versions of the Compose file format – 1, 2, 2.x, and 3.x. In addition to Compose file format versions shown in the table, the Compose itself is on a A service definition contains configuration that is applied to each container In your Dockerfile, if you specify ARG before the FROM instruction, ARG is not  If you're not afraid of bleeding edge, the latest version of Docker engine and Docker compose both implement libnetwork. With the right config file (that need to be put in version 2), you will create services that will all see each other.

Compose file versions and upgrading, Configure networking between containers when using Docker Compose. docker network ls NETWORK ID NAME DRIVER 17cc61328fef bridge bridge version: '2' services: web: build: . links: - "db:database" db: image: postgres. If you do  Starting in Compose file format 2.1, overlay networks are always created as attachable, and this is not configurable. This means that standalone containers can connect to overlay networks. In Compose file format 3.x, you can optionally set the attachable property to false. Each container can now look up the hostname web or db and get back the

  • Docker 1.10 no longer uses /etc/hosts it uses an embedded DNS server. There is a known issue with the dns server and firewald running on the host. That might be causing the problem. You should be able to ping using both the container name and the service name.
  • Thanks, the problem was the firewall on the host (Fedora). Docker containers are supposed to provide isolation from the host, but the new networking seem to be quite picky on the host configuration :-( ...
  • Thanks for your answer. However I cannot ping 'my-redis1' from 'my-app1' and the other way around. I can only ping using the IP address. The name 'my-redis1' does not appear in /etc/hosts of 'my-app1'. When I check 'docker network ls' I see that docker-compose has created a new network. When I use a version 1 docker-compose file I do see 'my-redis1' in the /etc/hosts file of 'my-app1'.
  • PS. Also when I remove the link from the version2 docker-compose file, I cannot ping 'my-redis1' from 'my-app1' using 'ping my-redis1'.
  • I think that you have to make a ping to the service name (ping my-app), not to the container name. Could you double-check if the command that I commented above works?
  • I can only ping between containers using the IP address, not through 'ping my-app' or 'ping my-app1'. Should 'my-app' appear in the /etc/hosts file of 'my-redis'?
  • I have tried to reproduce the issue but I have not been able to. I updated the answer with the steps that I followed, could you double checked? The 'my-app' should not have to appear there.