Dan
Dan

Reputation: 3584

Conusume API from a client docker container to the server container

I have two different projects running on different docker containsers. Below the two YML files:

FILE webserver-api/docker-compose.yml

version: "3.1"
services:
    webserver:
      image: nginx:alpine
      container_name: webserver-api
      working_dir: /application
      volumes:
          - .:/application
          - ./docker/nginx/nginx.conf:/etc/nginx/conf.d/default.conf
      ports:
          - "8005:80"

FILE client-app/docker-compose.yml

version: '3'
services:
    web:
        container_name: client-app
        build:
            context: ./
            dockerfile: deploy/web.docker
        volumes:
            - ./:/var/www
        ports:
            - "8010:80"
        links:
            - app
    app: [...]
    database: [...]

From the client-app I would like to call the webserver-api. When I'm trying to consume the API from webserver-api I'm getting the message "cURL error connection refused" or timeout error.

For example

$response = file_get_contents('http:/localhost:8005/api/test');

I tried also to replace the localhost with the IP of the webserver-api container like this:

$response = file_get_contents('http://172.25.0.2:8005/api/test');

But still I get a timeout connection error.

Which is the correct URL of the server container to call form the client container? Or how to set the host URL?

Thanks a lot for the help and time.

Upvotes: 0

Views: 331

Answers (3)

Dan
Dan

Reputation: 3584

Finally I figured it out.

By default docker creates a network called (in my case) webserver-api_default where webserver-api is the name of the folder that contains the YML file [projectname]_default.

On the client-app/docker-compose.yml of the client I had to specify which network to join:

version: '3'
    networks:
        default:
            external:
                name: webserver-api_default

    web:
        container_name: client-app
        build:
            context: ./
            dockerfile: deploy/web.docker
        volumes:
            - ./:/var/www
        ports:
            - "8010:80"
        links:
            - app
    app: [...]
    database: [...]

And from the client container I have to make the call to the URL:

$response = file_get_contents('http://webserver-api:8005/api/test');

Where webserver-api is the name of the server container and not the name of the network.

https://docs.docker.com/compose/networking/

Upvotes: 0

asolanki
asolanki

Reputation: 1373

As per the docker-compose documentation

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, and discoverable by them at a hostname identical to the container name.

So ideally if your service are interdependent you should put them in a single compose file. In that case you could have accessed your service directly by name and container port

http://webserver/api/test

But since they are in separate compose file, you can access the service via host mapped port

$response = file_get_contents('http://localhost:8005/api/test');

it should also work.

To debug you can check

  1. If port binding to 8005 is happening on your host.
  2. The endpoint specified is correct and accessible from host.

Upvotes: 1

todaynowork
todaynowork

Reputation: 1026

You need create a network first. Then use this network for both your client and server docker compose. Otherwise the network is isolated.

Another approach is expose the port of server to localhost and connect to localhost from client side.

Upvotes: 1

Related Questions