im-learning
im-learning

Reputation: 117

Django can’t establish a connection to the server

I'm using docker to start a project using django after I did build I get no error and I did up I get no error but still can't connect to server

my docker ps return

CONTAINER ID   IMAGE         COMMAND                  CREATED          STATUS         PORTS                               NAMES
385d949fdb65   yacosql_web   "python manage.py ru…"   2 minutes ago    Up 2 minutes   0.0.0.0:8000->8000/tcp              yacosql_web_1
754707984f75   mysql:5.7     "docker-entrypoint.s…"   11 minutes ago   Up 2 minutes   0.0.0.0:3306->3306/tcp, 33060/tcp   yacosql_db_1

in docker-compose up :

web_1  | Watching for file changes with StatReloader
web_1  | Performing system checks...
web_1  | 
web_1  | System check identified no issues (0 silenced).
web_1  | 
web_1  | You have 17 unapplied migration(s). Your project may not work properly until you apply the migrations for app(s): admin, auth, contenttypes, sessions.
web_1  | Run 'python manage.py migrate' to apply them.
web_1  | April 25, 2021 - 02:44:17
web_1  | Django version 2.2, using settings 'yacosql.settings'
web_1  | Starting development server at http://127.0.0.1:7777/
web_1  | Quit the server with CONTROL-C.

my docker-compose.yml :

version: "3.3"
   
services:
  db:
    image: mysql:5.7
    ports:
      - '3306:3306'
    environment:
       MYSQL_DATABASE: yacosql
       MYSQL_USER: yacosql
       MYSQL_PASSWORD: yacosql
       MYSQL_ROOT_PASSWORD: yacosql
  web:
    build:
        context: .
        dockerfile: ./docker/Dockerfile
    command: python manage.py runserver 127.0.0.1:7777
    volumes:
      - .:/usr/src/app
    ports:
      - "8000:7777"
    depends_on:
      - db

requirements.txt :

Django==2.2
mysqlclient==2.0.3
django-mysql==3.9.0

docker/Dockerfile

# syntax=docker/dockerfile:1
FROM python:3.6
ENV PYTHONUNBUFFERED=1
RUN mkdir /usr/src/app
WORKDIR /usr/src/app
COPY requirements.txt ./
RUN pip install -r requirements.txt
COPY . /usr/src/app

Upvotes: 0

Views: 1729

Answers (1)

jtr109
jtr109

Reputation: 104

Explain

python manage.py runserver 127.0.0.1:7777

Using command above in a container will make the Django server listening on loopback. For more detail you can read this explaination.

Specify Django server listening into 0.0.0.0:7777 can resolve it.

Resolution

Your docker-compose.yaml should be modified as following:

version: "3.3"
   
services:
  db:
    image: mysql:5.7
    ports:
      - '3306:3306'
    environment:
       MYSQL_DATABASE: yacosql
       MYSQL_USER: yacosql
       MYSQL_PASSWORD: yacosql
       MYSQL_ROOT_PASSWORD: yacosql
  web:
    build:
        context: .
        dockerfile: ./docker/Dockerfile
    command: python manage.py runserver 0.0.0.0:7777  # this line is modified.
    volumes:
      - .:/usr/src/app
    ports:
      - "8000:7777"
    depends_on:
      - db

After docker-compose up, you can see your web service running by executing docker ps. The result shoule be similar to:

CONTAINER ID   IMAGE                                 COMMAND                  CREATED         STATUS         PORTS                                                                                                                                  NAMES
1c6253f87494   so1_web                               "python manage.py ru…"   8 minutes ago   Up 4 minutes   0.0.0.0:8000->7777/tcp

Now you can try to connect to your service. Have a try with command:

curl 127.0.0.1:8000

You will get a response as expected if everything works well.

Others

I have push the source code to GitHub. It works as expected on my computer. You can clone it at will.

Upvotes: 3

Related Questions