0

I have a python web service and it simply replies on redis, for which I want to create a dev env using docker, it showed the build process was successful but I can't access the page on my host locally.

My dockerfile is

FROM python:3.8-slim

RUN apt-get update && apt-get install -y git python3-dev default-libmysqlclient-dev build-essential pkg-config libpq-dev

RUN mkdir -p -m 0700 ~/.ssh && ssh-keyscan github.com >> ~/.ssh/known_hosts

WORKDIR /code/mo_backend

COPY . .

RUN --mount=type=ssh pip install -r requirements.txt

# EXPOSE 8000

CMD ["python", "manage.py", "runserver"]

Compose file is

services:
  mo_backend:
    build:
      context: .
      ssh:
        - default
    ports:
      - "8000:8000"
    depends_on:
      - redis
  redis:
    image: redis:5.0.7
    ports:
      - "6379:6379"

After I ran docker compose up, it showed service started

But when I tried to visit the 8000 port, it was empty.

I tried localhost:8000, 127.0.0.1:8000, 0.0.0.0:8000, neither of them worked

Im using WSL, tried to access the site on my windows host locally.

Alan Wang
  • 1
  • 1
  • 1
    What HTTPcode did you get when calling it? "Empty" could mean that you successfully reach the server but not the right path. Are you sure your code listens on port 8000? Did you try to exec in the container and use curl to debug? – Gaël J Aug 20 '23 at 16:32
  • Do you need to tell the Django server to listen on 0.0.0.0, as in ["The connection was reset" in localhost:8000 using django and docker](https://stackoverflow.com/questions/60183313/the-connection-was-reset-in-localhost8000-using-django-and-docker)? (Change your Dockerfile `CMD`, don't put an override `command:` in the Compose file.) – David Maze Aug 20 '23 at 17:10
  • @DavidMaze Thx it works BTW may I know the difference here between 127.0.0.1 vs 0.0.0.0? I did some search and seems 127.0.0.1 is the address that you can only visit on the same machine, so based on virtualization of the container, it was treated as another machine. Please correct me if Im wrong What about 0.0.0.0 then? Why can I visit this address through this? If I run the service locally, I can't really tell the difference, cuz both of them works – Alan Wang Aug 21 '23 at 09:54
  • See [Docker app server ip address 127.0.0.1 difference of 0.0.0.0 ip](https://stackoverflow.com/questions/59179831/docker-app-server-ip-address-127-0-0-1-difference-of-0-0-0-0-ip). Since Docker provides each container with an isolated network environment, 127.0.0.1 is the container-private localhost interface. – David Maze Aug 21 '23 at 10:34

1 Answers1

0

The line exposing the port is a comment. Remove the # in

# EXPOSE 8000

Is it enough ?

Antoine B
  • 1
  • 1
  • `EXPOSE` doesn't really actually do anything in modern Docker, so uncommenting this line shouldn't matter. – David Maze Aug 20 '23 at 17:11
  • You're right, I thought it was doing something. It is stated [here](https://docs.docker.com/engine/reference/builder/#expose) in the docs. – Antoine B Aug 20 '23 at 17:23