dockernetwork-programmingdnsapache2local

Docker networking forwarding failed


I got a problem regarding Docker networking. I've a php (non-framework) and mysql setup with Docker Compose and Docker Desktop. Mysql is using port 3636 and is working perfectly, like: I can reach him on both inside a Docker Container and on my local. But once I open the web application with php, I can do a curl inside the Docker Container and got a 302 message and can see the application by command line, but once I try it on my local environment (my computer), I got the following error:

It doesn't seems to make sense, my web application is running on 36001, I debug it and got the following information:

My computer:

Docker Container:

Docker stack trace (log):

2024-03-27 19:25:34 [Wed Mar 27 18:25:34 2024] PHP 8.3.4 Development Server (http://127.0.0.1:36001) started
2024-03-27 19:26:04 [Wed Mar 27 18:26:04 2024] 127.0.0.1:54472 Accepted
2024-03-27 19:26:04 [Wed Mar 27 18:26:04 2024] 127.0.0.1:54472 [302]: GET /
2024-03-27 19:26:04 [Wed Mar 27 18:26:04 2024] 127.0.0.1:54472 Closing

Docker compose:

version: "3.8"

services:
  web:
    container_name: ${CONTAINER_NAME?error}
    env_file:
      - path: .env
        required: true
    environment:
      LANGUAGE: ${LANGUAGE?error}
    stdin_open: true
    tty: true
    build:
      context: .
      args:
        LANGUAGE: ${LANGUAGE?error}
      dockerfile: Dockerfile
    ports:
      - "${WEB_PORT?error}:${WEB_PORT?error}"
    volumes:
      - .:${CONTAINER_WORKDIR?error}
    depends_on: # only if you're using the database service
        - database
    networks:
      - testnetwork
    init: true

  # Conditionally include database service if DATABASE_ENGINE is not 'none'
  database:
    env_file:
      - path: .env
        required: true
    environment:
      MYSQL_ROOT_PASSWORD: '${DB_PASSWORD}'
      MYSQL_ROOT_HOST: "%"
      MYSQL_DATABASE: '${DB_NAME}'
      MYSQL_USER: ${DB_USER}
      MYSQL_PASSWORD: '${DB_PASSWORD}'
      MYSQL_ALLOW_EMPTY_PASSWORD: 1
      DATABASE_PORT: ${DATABASE_PORT?error}
      DB_SQL_PATH: ${DB_SQL_PATH?error}
    image: ${DATABASE_ENGINE}:${DB_VERSION}
    command: mysqld --sql_mode=""
    hostname: mysql
    ports:
        - '${DATABASE_PORT?error}:${DATABASE_PORT?error}'
    volumes:
        - 'test-mysql:/var/lib/mysql'
        - './${DB_SQL_PATH?error}:/docker-entrypoint-initdb.d/${DB_SQL_PATH?error}'
    networks:
        - testnetwork
    healthcheck:
        test: [ "CMD", "mysqladmin", "ping", "-p test" ]
        retries: 3
        timeout: 5s

networks:
    testnetwork:
        driver: bridge

volumes:
    test-mysql:
        driver: local

The Docker Compose is modified on arguments and environment because otherwise it would be a long code display inside the question.


Solution

  • Your application binds to the loopback interface (127.0.0.1). That means it'll only accept connections from there. In a container, it'll only accept connections from inside the container.

    You need to bind to 0.0.0.0 for it to accept connections from outside the container.

    The key log message to look for is Development Server (http://127.0.0.1:36001) started. That needs to change to 0.0.0.0.