Docker Compose Link

Asked

Viewed 2,838 times

5

I am using Docker Compose to climb a set of three containers "Selenium Grid":

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 climb 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

Docker File from Maven:

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 when climbing the container straight up the "Docker run", everything works perfectly.

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

I wonder what I’m setting wrong?

1 answer

4


The commie will always create a network for your services and this is where the problem lies, since by default you will not be able to access container of different networks other than the default. The docker run will always put the container on the net default, if not specified other, then why creating so you got access.

To build the image and create the container from the commie so that you can access a container in another network - service defined in another commie you have some ways to do, always keep in mind the networks. The simplest way is by using external_links and network_mode as bridge. In this case, change your composes for 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 do is basically that all services work in network mode bridge, then regardless of the network that is will see it, since it is an "aggregated network".

Another way is using in commie of Maven the network created on commie of Selenium: in this case I will assume that the network created is called selenium_default, in commie of Selenium there is no need to change only the Maven:

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

In this second way we made the service of the Maven is part of an external network, established by commie of Selenium. Also note how you will reference the other container in building your image, use the alias if informed, etc.

There are other ways of doing, such as creating a network explicitly internal or external to commie, take a look at how networks work and see what suits you best.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.