0

this is my docker-compose.yml

site1:
build:
  context: .
  dockerfile: php/Dockerfile
image: alpine-php7.1
container_name: site1
restart: unless-stopped
tty: true
working_dir: /var/www
volumes:
  - ../site1:/var/www
  - ./php/local.ini:/usr/local/etc/php/conf.d/local.ini
depends_on:
  - webserver
networks:
  - dockernetwork

site2:
build:
  context: .
  dockerfile: php/Dockerfile
image: alpine-php7.1
container_name: site2
restart: unless-stopped
tty: true
working_dir: /var/www
volumes:
  - ../site2:/var/www
  - ./php/local.ini:/usr/local/etc/php/conf.d/local.ini
depends_on:
  - webserver
networks:
  - dockernetwork

#Nginx Service
webserver:
image: nginx:alpine
container_name: webserver
restart: unless-stopped
tty: true
ports:
  - "80:80"
  - "9000:9000"
volumes:
  - ../site1:/var/www
  - ../site2:/var/www
  - ./nginx/conf.d/:/etc/nginx/conf.d/
networks:
  - dockernetwork

And here is my Dockerfile

FROM php:7.1-fpm-alpine

# Install packages
RUN apk --no-cache add \
    bash \
    libzip-dev \
    libpng-dev \
    libjpeg-turbo-dev

# Install php extensions
RUN docker-php-ext-install zip
RUN docker-php-ext-configure gd --with-jpeg-dir=/usr/include/ && \
    docker-php-ext-install gd
RUN docker-php-ext-install pdo

# Install composer
RUN curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer

I need to establish CURL communication between those two websites. When I run docker exec -it site1 bash and then curl site2:9000 it gives me the following error curl: (56) Recv failure: Connection reset by peer

I also tried curl site2:80 and it gives me curl: (7) Failed to connect to site2 port 80: Connection refused

I have two PHP projects (two websites) and site1 requests data from site2 via CURL. When it's in production, it works fine as I curl to specific domains. But in the docker environment, I can't curl to site2 or site2.test since site1 is on a separate container.

Could you suggest a better docker-compose structure and some suggestions for the Dockerfile (what to use in FROM)

  • 1
    1) AFAICT both `site1` and `site2` are PHP-FPM containers. PHP-FPM is not a web server, so trying to make http connections to it won't work. I think that explains the first error you've shown. 2) `site2` has nothing running on port 80, which I think explains the 2nd error. Maybe you can edit the question and clarify what you're actually trying to do, as it isn't clear. – Don't Panic Nov 07 '21 at 08:57
  • Hm, thank you for the explanation. I have two websites and one is requesting data from another via PHP curl. So when it's in production, it works fine as I curl to specific domains. But in the docker environment, I can't curl to site2 or site2.test since site1 is on a separate container. It would be very helpful if you can suggest a better container structure – user3412802 Nov 07 '21 at 09:20
  • "*I can't curl to site2 or site2.test since site1 is on a separate container*" - no, the reason you can't make a curl connection is bcs there is no web server running in your `site2` container to connect to. You are mixing up Docker containers and websites. I am guessing you have 2 vhosts set up for `site1/2` in your `webserver` container? If so, why not just use curl in that container? That's where your (single) web server is, so that's where you need to connect to. – Don't Panic Nov 07 '21 at 10:06
  • 1
    It is up to you but I'd suggest renaming your containers to avoid this confusion - `site1` and `site2` suggest they are really web sites. They are not. `php1` and `php2` might be more appropriate. – Don't Panic Nov 07 '21 at 10:07
  • 1
    Or going a step further - do you really need 2 separate PHP containers? They are using the same php.ini, are they identical? Maybe both sites can use the same PHP container. – Don't Panic Nov 07 '21 at 10:19
  • Ok, I moved those two projects under the same container. Now I have only webserver and php containers. Websites are loading fine when I open them in the browser site1.test and site2.test, but when I do a php curl method like POST to http://site2.test/do_something, it gives me code 0. Am I missing some setting in my docker-compose? – user3412802 Nov 07 '21 at 10:48
  • 1
    What do you mean `code 0`? Is PHP working, eg `phpinfo()` from site1 and site2? If your curl is failing, debug it - check your logs, check [`curl_error()`](https://stackoverflow.com/questions/3987006/how-to-catch-curl-errors-in-php), and [`curl_getinfo()`](https://www.php.net/manual/en/function.curl-getinfo.php), etc. – Don't Panic Nov 07 '21 at 10:56
  • phpinfo() tells that there is `cURL support enabled cURL Information 7.64.0.` When I do a php curl POST or GET to let's say 'google.com', it returns me a pretty standard response code. 400/405/403 and so on. But when I try to do the Curl to site1.test or site2.test, it `$response = curl_exec($ch);` returns `false`. CURLINFO_HTTP_CODE = 0, CURLINFO_SIZE_DOWNLOAD = 0. So to me it seems that the problem is not in php curl, but in the fact that site1 backend doesn't see site2.test. – user3412802 Nov 07 '21 at 20:05

1 Answers1

0

So, found the solution:

adding this to php container

extra_hosts:
    - "dockerhost:10.0.75.1"

    - "api-project.dev:10.0.75.1"

    - "consumer-project.dev:10.0.75.1"

To figure out dockerhost ip: 10.0.75.1, CLI into your nginx container and run ip -4 route show default | cut -d" " -f3

  • Glad you solved it. I found an alternative here which might be simpler: https://stackoverflow.com/a/56929097/6089612 – Don't Panic Nov 08 '21 at 07:33