How to make Curl between containers?

Asked

Viewed 579 times

0

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

I have a API and a Front who keeps making requests via Curl for her.
But when I put these projects on Docker and separated them into containers (to "simulate" the actual situation, since the API and the Front are on separate servers) requests Curl stopped working.

I did other tests and I couldn’t do the Curl work between containers. Follow image of example:
inserir a descrição da imagem aqui

Docker used to create the two containers.

In the above test the following Docker-Compose was used (as shown 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/

inserir a descrição da imagem aqui

If I try to make Curl to other places it works perfectly.
How do I make it work between containers?

Attempts made ----------------

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/

2 answers

0

You will need to use the link, your Docker-Compose only has a container set, 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 link with the back, you would call the server by the link http://server-back:porta inside your container because it will create the host automatically.

  • I had uploaded the separate containers each with a Docker-Compose. I did what you suggested, created a Docker-Compose with the two containers and used the links to link the front to the back but the error persists. The link worked because if I try to make a request from the back to the front it is loading too much time and from the timeout, if I do from front to back it does not return error. But it does not return the data either.

  • You will need to expose your doors on the back and on the front... But why are you trying to make a call on the back to the front

  • I was just testing to see if the link between the containers had actually worked. I exposed the ports but tbm didn’t work.

0


I managed to reach the following solution:

  • You must link the containers in Docker-Compose
    • Using the links: container name if you are going to upload the containers in 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 requisitions.
  • Use the IP in the Curl instead of the address http://localhost:porta container

Follow the used Docker-Compose:

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 command Docker Insect container name and looked for the IP: inserir a descrição da imagem aqui

After that on Curl I just swapped the url http://localhost:443 for http://172.17.0.5 and Curl went perfectly. inserir a descrição da imagem aqui

Browser other questions tagged

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