0

I want to isolate my new project infrastructure using Docker containers and I'm newbie in Docker. I'm stuck with a problem. First of all I will show example of my config and then I will address the problem.

Config example

nginx:
      restart: always
      image: nginx:latest
      expose:
          - 80
      ports:
          - 8181:80
#          - 80:80
      volumes:
          - ../.:/code
          - ./docker/nginx/hosts:/etc/nginx/conf.d/
          - ./docker/nginx/nginx.conf:/etc/nginx/nginx.conf
      links:
          - php
 php:
      restart: always
      build: docker/php5.6-fpm
  #    image: php:5.6-fpm
  #    ports:
  #        - 9000:9000
      volumes:
          - ../.:/code
      links:
              - redis_loc
              - postgres_loc
              - memcached_loc
              - rabbitmq_loc

This is an example. I have dropped details for *_loc services. So, I have two local domains for development, like site1.loc and site2.loc. I'm use micro-service architecture and site2.loc is a micro-service for site1.loc.

I have common php container for all micro-services code and I have common nginx container for all micro-services which configured with *.loc.conf config files. Nginx listen to 80 port inside Docker network. This hosts are available through 8181 port on host machine. On local (host) machine I have front nginx with the same *.loc configs configured as proxy to 8181 analogues. Also I have in my /etc/hosts records

127.0.0.1 site1.loc
127.0.0.1 site2.loc

So, I can open in browser both local sites and work with them separately.

The problem

The problem is, that site2.loc is a micro-service, so it should be accessible from site1.loc php code. And now, when I'm trying to send request to site2.loc from site1.loc php code I'm getting the error:

'stream_socket_client(): unable to connect to site2.loc:80 (php_network_getaddresses: getaddrinfo failed: Name or service not known)'

Php can't find the way to site2.loc.

I have tried docker-compose feature extra_hosts on nginx and php containers, but it's doesn't helped me.

#      extra_hosts:
#          - "site1.loc:nginx"
#          - "site2.loc:nginx"

I have tried to configure custom network, but then I'm realized, that this is wrong way.

Also I have tried jwilder/nginx-proxy, but it's make vhosts available just for local machine, not inside php container.

I clearly understand that source of problem is that environment inside php container doesn't know anything about site2.loc host. And I guess I need some kind of hosts mapping or dns to be configured.

But I don't know how to implement such configuration and can't find any example (I had googled through half day yesterday).

How could I make site2.loc accessible to site1.loc php code?

UPD

I just have figured out that I need use Consul. Are there another ways without Consul?

UPD

Helped way of indicating the hosts with reference to 172.17.0.1 in extra_hosts. Prompt, how it is better to select a method for Productions - extra_hosts, dns, Consul? Given that I have on the server several projects nearby, in containers or without.

1 Answer 1

1

Do you need external access to your microservice site2.loc ? If not you can split site1 and site2 in two nginx containers, then you can do like this with user-defined networks:

  1. Create a network named "app-net"
  2. When you create each service connect them to the network and give them an alias (--network-alias), let's say you put the alias site2 on your microservice
  3. There is an internal DNS server in Docker that will resolve the domain names for the container. So when you want to access site2 from the PHP code simply use "site2" wich will be resolved into a IP adress accessible by the PHP container (if they are all connected to the same user-defined network)

Just make sure the PHP process can access the PHP files with the same path as nginx.

EDIT : In docker compose I think the net alias is set automaticaly with the service name
EDIT2 : in fact docker compose is setting up a network automaticaly https://docs.docker.com/compose/networking/#/networking-in-compose

5
  • The main point here is - split site1 and site2 in two nginx containers. I though about this solution already, but i want access site2.loc by virtual host (by link like http: //site2.loc), not by docker dns name for service. Commented Oct 6, 2016 at 10:49
  • php: ... links: - nginx:site1.loc - nginx:site2.loc That should do it by creating two aliases for the nginx container and nginx will redirect to the right virtual host Commented Oct 6, 2016 at 12:41
  • usually configuration is when nginx links php, in this direction, not opposite. and it's impossible to link back from php to nginx simultaneously. it's a well known problem in docker with bidirectional linking. and this couldn't be solved without additional dns container or without usage of service discovery soft, like Consul. or please show working example of docker-compose config if I didn't understand you right. Commented Oct 6, 2016 at 15:40
  • ERROR: Circular dependency between nginx and php . the problem and possible solutions are described here abdelrahmanhosny.com/2015/07/01/… Commented Oct 6, 2016 at 15:42
  • I have a web app that run with a separate php container but I didn't use compose. Maybe you should try with the docker CLI first so you understand what is really happening. First create a network and then spin up the two containers and connect them to the network Commented Oct 7, 2016 at 9:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.