How to cURL between containers?

0

I started using Docker a short time ago, probably because of this I got stuck in this problem.

I have an API and Front that is making requests via cURL for it.
But when I put these projects in Docker and separated them in containers (to "simulate" the actual situation, since the API strong> stand on separate servers) the cURL requisitions have stopped working.

I did other tests and could not make cURL work between containers . Here's an example image:

Docker Image used to create the two containers.

In the above test we used the following Docker-compose (as exemplified in the next image the only difference between the two containers and the docker-compose is the port and container names):

a:
environment:
    TZ: "America/Sao_Paulo"
image: o2multi/apache-php7
ports:
    - "80:80"
volumes:
    - ./src:/var/www/html/

IfItrytomakecURLforotherplacesitworksperfectly.

Attemptsmade----------------

version:'2'services:a:environment:TZ:"America/Sao_Paulo"
    image: o2multi/apache-php7
    ports:
        - "80:80"
    volumes:
        - ./a:/var/www/html/
    links:
      - b
b:
    environment:
        TZ: "America/Sao_Paulo"
    image: o2multi/apache-php7
    ports:
        - "443:80"
    volumes:
        - ./b:/var/www/html/
    
asked by anonymous 12.06.2017 / 14:47

2 answers

0

I was able to get the following solution:

  • You must link the containers in docker-compose
    • Using the links : container_name to upload the containers to the same docker-compose.
    • Using external_links : container_name to link your container to another container that is already active.
  • Check the IP of the container that will receive the requests.
  • Use IP in cURL instead of the% container

The following docker-compose is used:

version: '2'                                                        
services:
a:
    environment:
        TZ: "America/Sao_Paulo"
    image: o2multi/apache-php7
    ports:
        - "80:80"
    volumes:
        - ./a:/var/www/html/
    links:
      - b
b:
    image: o2multi/apache-php7
    ports:
        - "443:80"
    volumes:
        - ./b:/var/www/html/

Then I used the docker insect container_name command and looked for IP :

AfterthisincURLIjustchangedtheurlhttp://localhost:portatohttp://localhost:443andcURLstartedworkingperfectly.

    
14.06.2017 / 21:23
0

You will need to use the link, your Docker-compose only has a container defined, I will put an example of a database connection for you to get an idea:

version: '2'

services:
  server-front:
    image: sua/imagem
    ports:
      - "80:80"
    volumes:
      - ./src:/var/www/html/
    build:
      context: ./pasta-do-front
      dockerfile: Dockerfile
    links:
      - server-back

  server-back:
    image: sua/imagem
    ports:
      - "porta:porta"
    volumes:
      - ./seu-volume
    build:
      context: ./pasta-do-back
      dockerfile: Dockerfile  

See that the front has a back link, you would call the server by the http://server-back:porta link within your container because it will automatically create the host.

    
12.06.2017 / 14:54