Got "Bad request" from dockerized Apache when requested by container name

Hello,

this is my first post in the forum.
I have a php5.6 + apache application running in docker compose, named gru_joomla_1

Docker version: Docker version 19.03.8, build afacb8b7f0
Container image: joomla:3.9-php5.6-apache
Apache version in image: 2.4.25

If I try to make an http request from another container (from a different compose stack, with external network of gru_joomla_1 included to scope) to the one running apache (listening to port 80) i got a “400 Bad Request” error.
In order to make some test I installed curl in a couple of containers, trying to make the request, obtaining the same error:

curl http://gru_joomla_1/query/get_contatto.php?

<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
<html><head>
<title>400 Bad Request</title>
</head><body>
<h1>Bad Request</h1>
<p>Your browser sent a request that this server could not understand.<br />
</p>
<p>Additionally, a 400 Bad Request
error was encountered while trying to use an ErrorDocument to handle the request.</p>
<hr>
<address>Apache/2.4.25 (Debian) Server at gru.it Port 80</address>
</body></html>

I can successfully ping and make telnet connection to port 80 from the same container obtaining the error.

The strange thing is that I can succesfully browse the application from my browser and, moreover, curl works from the same container above if I use the internal docker ip address or any domain name (previously updating the /etc/hosts file of the container sendin the request)

Example of working tests:
curl http://gru.it/query/get_contatto.php
curl http://172.17.0.1/query/get_contatto.php

For now I solved the problem by using an nginx image instead of apache, but why does apache answers with bad request if reached by container name?

Thank you in advance

Here’s my docker-compose.yml

version: '3.7'

services:
  db-gru:
    image: mysql:5.5.54
    env_file:
      - variables-gru.env
    volumes:
      - ./volumes/mysql-gru:/var/lib/mysql
      - ./init-gru:/docker-entrypoint-initdb.d
      - ./my.cnf:/etc/mysql/my.cnf
      - ./log/mysql-gru:/var/log
    networks:
      - backend

  db-joomla:
    image: mysql:5.5.54
    env_file:
      - variables-joomla.env
    volumes:
      - ./volumes/mysql-joomla:/var/lib/mysql
      - ./init-joomla:/docker-entrypoint-initdb.d
      - ./my.cnf:/etc/mysql/my.cnf
      - ./log/mysql-joomla:/var/log
    networks:
      - backend

  joomla:
    image: joomla:3.9-php5.6-apache
    depends_on:
      - db-joomla
      - db-gru
    env_file:
      - variables-joomla.env
    volumes:
      - ./volumes/joomla-root:/var/www/gru
      - ./site.conf:/etc/apache2/sites-enabled/000-default.conf
      - ./log/joomla:/var/log
    ports:
      - "80:80"
    networks:
      - frontend
      - backend

networks:
  frontend:
    driver: bridge
  backend:
    driver: bridge

Thank you for your help. I’ve just found the problem. It seems that Apache doesn’t like to receive requests through an hostname containing underscore characters.

Using the container_name directive in my docker-compose.yml (eg. gru-joomla) fixed the problem.

More info here:

I would suggest thinking about going one step further. See this article:


One change: if you want to be able to use Xdebug, change the proxy to another port (say 8090) rather than 9000.
Then, add this to the Dockerfile for the php container to move the php-fpm port away from the xdebug port:
RUN sed -i ‘s/9000/8090/’ /usr/local/etc/php-fpm.d/zz-docker.conf