Issue with Accessing Web Application in Docker Container from Host Machine

Hello everyone,

I hope you’re doing well. I’m currently facing a challenge and could use some assistance.

Problem Description: I have a Docker container hosting a web application, which is intended to be accessible on port 8000. The web application runs smoothly within the Docker container without any errors or issues. However, I’m encountering a problem when trying to access the application from my host machine (MacBook) using a web browser. The connection times out, and I’m receiving a “Connection Refused” error.

Environment Details:

  • Docker Version: Docker Engine 20.0.6
  • Operating System: macOS 13.6 (Apple silicon)
  • Docker Compose Version: 2.22

Steps Taken: I’ve taken several troubleshooting steps, including verifying that the Docker container is running correctly with docker ps, checking the Docker container’s IP address, and reviewing the Docker Compose file to ensure port mapping is correctly configured.

Expected Outcome: My expectation is to access the web application within the Docker container from my host machine by navigating to http://localhost:8000 in a web browser.

Actual Outcome: While I can access the web application from within the Docker container using http://localhost:8000, I face connectivity issues when trying to access it from my host machine. The connection times out, and the application remains inaccessible.

Additional Information:

  • The firewall on my MacBook is disabled, and there are no other firewall rules in place.
  • The Docker container’s network mode is set to the default, with no custom network configurations applied.

Attachments: I’ve attached the Docker Compose file (docker-compose.yaml) for reference.

I would greatly appreciate any insights or guidance in diagnosing and resolving this issue, as it’s currently impacting my local development and testing. If additional information or further clarification is needed to understand the problem better, please don’t hesitate to ask.

Thank you for your time and assistance. I’m looking forward to your responses and suggestions.

Please share your config file.

Do you mean docker-compose.yaml

version: '3'
services:
  web:
    build: ./app 
    volumes:
      - ./app:/app
    ports:
      - "8000:8000"
    depends_on:
      - db
  db:
    image: postgres
    environment:
      POSTGRES_PASSWORD: example

and Dockerfile

FROM python:3.8
WORKDIR /app
COPY requirements.txt requirements.txt
RUN pip install -r requirements.txt
COPY . .
CMD ["python", "app.py"]

Seing how your app listens on a port in the container could also help. My guess is that it listens on localhost in the container so that won’t work outside of it. Make sure the app listens on 0.0.0.0 (all ip addresses) so port forward could work from the host to the containers IP address.

1 Like

How can I modify app listens IP ?

I don’t know your app so it’s something you need to solve :slight_smile: You probably defined the IP and port. If not, then check the documentation of the library or framework you are using to find out how you should define the IP address.

yes, its work. I setup IP address in my flask app to 0.0.0.0, and all works. Thanks