Docker Compose Link

2

I'm using Docker Compose to upload a set of three "Selenium Grid" containers:

selenium-hub:
  image: selenium/hub
  container_name: selenium-hub
  ports:
    - 4444:4444

nodeff:
  image: selenium/node-firefox
  ports:
    - 5900
  links:
    - selenium-hub:hub

nodechrome:
  image: selenium/node-chrome
  ports:
    - 5900
  links:
    - selenium-hub:hub

The Grid works perfectly. But when I need to upload another container with maven using another Docker Compose File, passing the link parameter as "selenium-hub: hub", the process fails.

Maven Docker Compose File:

maven-test:
 build: .
 volumes:
  - ./Screenshots:/MeuProjeto/Screenshots
 links:
  - selenium:hub

Maven Docker File:

FROM maven

#RUN mkdir /NaturaSiteNG

#WORKDIR /NaturaSiteNG

#COPY . /NaturaSiteNG

ENTRYPOINT curl http://selenium-hub:4444

Error message:

ERROR: Service 'maven-test' has a link to service 'selenium:hub' which is undefined.

But by uploading the container directly through the docker run, everything works perfectly.

docker run -it --link selenium-hub:hub maven

What am I setting wrong?

    
asked by anonymous 06.10.2017 / 14:58

1 answer

0
The compose will always create a network for your services and this is where the problem is, since by default you will not be able to access a container from distinct networks other than default .  The docker run will always put the container on the network default , if not another one, then why creating it so you got access.

To construct the image and create the container from the compose so that it can access a container on another network - service defined in another compose you have some ways to do, always keep the networks in mind. The simplest way is using external_links and network_mode as bridge . In this case, change in your composes to something like this:

  • maven compose :
maven-test:
  build: .
  volumes:
    - ./Screenshots:/MeuProjeto/Screenshots
  network_mode: bridge
  external_links:
    - selenium-hub:hub
  • selenium compose :
selenium-hub:
  image: selenium/hub
  container_name: selenium-hub
  network_mode: bridge
  ports:
    - 4444:4444

Here, what we basically do is that all services work in bridge network mode, so regardless of the network you are viewing it, it is an "aggregated" network.

>

Another way is to use the maven compose network created in selenium compose : in this case I will assume that the network created is called selenium_default , in the compose selenium does not need to change, only in maven :

maven-test:
  build: .
  volumes:
    - ./Screenshots:/MeuProjeto/Screenshots
  external_links:
    - selenium-hub:hub
  networks:
    - selenium_default
networks:
  selenium_default:
    external: true

In this second form we have made the maven service to be part of an external network, created by the selenium compose . Also note how you will reference the other container in the construction of your image, use alias if informed, etc.

There are other ways to do this, such as creating a network explicitly internally or externally to compose , take a look at how networks work and see which works best for you.

    
06.10.2017 / 17:16