简体   繁体   中英

How to call a local ressource from a docker container?

I builded: Apache, Php7.3, and Postgresql images with 'docker-compose.yml'.

Everything works good, but when I make a call of a local photo, I get a warning message and the photo doesn't appear.

This is the error message: Warning: getimagesize(https://localhost:80/tofs.domain.com/photo.php?id=1): failed to open stream: Cannot assign requested address.

I get the same message when I use 'file_get_contents' function with 'localhost'.

Note: calling an external ressource works for theses functions.

I draw your attention that: I've already created a network. ~ All of the containers are sharing the same network (via “docker-compose.yml”). ~

But in my case, I think that the functions used (like: file_get_contents, getimagesize, …etc ) refer to the "localhost/127.0.0."1 of my Host machine, AND not to the network of the container.

So, what must I do to resolve this problem?

Thanks.

With file_get_contents You are fetch the image from PHP container to web server container

version: "3.7"
services:      
  webserver:
    image: nginx:alpine
    working_dir: /api
    depends_on:
      - php-fpm
    volumes:
      - ./api:/api
  
  php-fpm:
    build: docker/php-fpm
    working_dir: /api
    volumes:
      - ./backend:/backend

Webserver container needs to have depends_on of PHP-FPM container (link betwen from PHP-FPM to webserver should also work).

Now in the PHP container You can do ping http://webserver and You should see Your homepage of application.

I don't know why You are trying to do this like this:

  • localhost/127.0.0.1
  • https://localhost:80/tofs.domain.com/photo.php?id=1

You can't add domain to the... domain:)

Your localhost in my example of docker-compose.yml is webserver so this should work: https://webserver/photo.php?id=1

I know, I used "links" instead of "depends_on" to make the match between "php-FPM" and "apache2.4", and it works good.

According to "localhost/127.0.0.1", I meant "localhost" OR "127.0.0.1". AND According to "https://localhost:80/tofs.domain.com/photo.php?id=1", => "tofs.domain.com" is a directory for test and not a domain:).

Whene I tried your solution before, the server did not validate the URL, because there is no SSL certificat .

I've already created SSL certificat for "localhost", not for the name of a service as tested before...

What must I do?

Thanks.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM