When I dockerize django vue3 app django container does not work

I am learning docker and while I dockerize my django vue3 app I got error.

Dockerfile:

FROM python:3.11-slim

RUN apt-get update && apt-get install -y \
    libpq-dev gcc curl \
    && apt-get clean \
    && rm -rf /var/lib/apt/lists/*


WORKDIR /backend/project

COPY requirements.txt /backend/

RUN pip install --no-cache-dir -r /backend/requirements.txt

COPY . /backend/

RUN chown -R www-data:www-data /backend

USER www-data

ENV PYTHONUNBUFFERED=1

ENV PYTHONDONTWRITEBYTECODE=1

EXPOSE 8000

CMD ["python", "manage.py", "runserver", "0.0.0.0:8000"]

Docker-compose:

version: '3.9'

services:
  web:
    build:
      context: .
      dockerfile: dockerfile
    container_name: c_django
    command: python manage.py runserver 0.0.0.0:8000
    volumes:
      - ./backend:/app
    ports:
      - 8000:8000
    environment:
      ALLOWED_HOSTS: '*'
      DEBUG: 'True'
      SECRET_KEY: random_secret_key
      DATABASE_NAME: fitness
      DATABASE_USER: postgres
      DATABASE_PASSWORD: 123456
      DATABASE_HOST: db
      DATABASE_PORT: 5432
      REDIS_URL: redis://redis:6379/1
    depends_on:
      - db
      - redis
    networks:
      - frontend_backend

  db:
    image: postgres:15
    container_name: c_postgres
    environment:
      POSTGRES_USER: postgres
      POSTGRES_PASSWORD: 123456
      POSTGRES_DB: fitness
    ports:
      - 5432:5432
    volumes:
      - postgres_data:/var/lib/postgresql/data

  frontend:
    build:
      context: ../frontend
    container_name: c_frontend
    ports:
      - 8080:80
    depends_on:
      - web
    networks:
      - frontend_backend


  nginx:
    image: nginx:latest
    container_name: c_nginx
    volumes:
      - ./nginx/nginx.conf:/etc/nginx/conf.d
      - ./frontend/dist/:/usr/share/nginx/html
    ports:
      - 80:80
    depends_on:
      - frontend
    networks:
      - frontend_backend

  redis:
    image: redis:8.0-M02-alpine
    container_name: c_redis
    ports:
      - 6379:6379
    networks:
      - frontend_backend

volumes:
  postgres_data:

networks:
  frontend_backend:
    driver: bridge

When I docker compose up I got this error for django :

c_django    | Watching for file changes with StatReloader
c_django    | Performing system checks...
c_django    | 
c_django    | 
c_django    | System check identified no issues (0 silenced).
c_django    | Exception in thread django-main-thread:
c_django    | Traceback (most recent call last):
c_django    |   File "/usr/local/lib/python3.11/site-packages/django/db/backends/base/base.py", line 275, in ensure_connection
c_django    |     self.connect()
c_django    |   File "/usr/local/lib/python3.11/site-packages/django/utils/asyncio.py", line 26, in inner
c_django    |     return func(*args, **kwargs)
c_django    |            ^^^^^^^^^^^^^^^^^^^^^
c_django    |   File "/usr/local/lib/python3.11/site-packages/django/db/backends/base/base.py", line 256, in connect
c_django    |     self.connection = self.get_new_connection(conn_params)
c_django    | 
c_django    |                 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
c_django    |   File "/usr/local/lib/python3.11/site-packages/django/utils/asyncio.py", line 26, in inner
c_django    |     return func(*args, **kwargs)
c_django    |            ^^^^^^^^^^^^^^^^^^^^^
c_django    |   File "/usr/local/lib/python3.11/site-packages/django/db/backends/postgresql/base.py", line 277, in get_new_connection
c_django    |     connection = self.Database.connect(**conn_params)
c_django    |                  ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
c_django    |   File "/usr/local/lib/python3.11/site-packages/psycopg/connection.py", line 728, in connect
c_django    |     attempts = conninfo_attempts(params)
c_django    |                ^^^^^^^^^^^^^^^^^^^^^^^^^
c_django    |   File "/usr/local/lib/python3.11/site-packages/psycopg/_conninfo_attempts.py", line 45, in conninfo_attempts
c_django    |     raise e.OperationalError(str(last_exc))
c_django    | psycopg.OperationalError: [Errno -3] Temporary failure in name resolution
c_django    | 
c_django    | The above exception was the direct cause of the following exception:
c_django    | 
c_django    | Traceback (most recent call last):
c_django    |   File "/usr/local/lib/python3.11/threading.py", line 1045, in _bootstrap_inner
c_django    | 
c_django    |     self.run()
c_django    |   File "/usr/local/lib/python3.11/threading.py", line 982, in run
c_django    |     self._target(*self._args, **self._kwargs)
c_django    |   File "/usr/local/lib/python3.11/site-packages/django/utils/autoreload.py", line 64, in wrapper
c_django    |     fn(*args, **kwargs)
c_django    |   File "/usr/local/lib/python3.11/site-packages/django/core/management/commands/runserver.py", line 136, in inner_run
c_django    |     self.check_migrations()
c_django    |   File "/usr/local/lib/python3.11/site-packages/django/core/management/base.py", line 581, in check_migrations
c_django    |     executor = MigrationExecutor(connections[DEFAULT_DB_ALIAS])
c_django    |                ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
c_django    | ^^^^^^^
c_django    |   File "/usr/local/lib/python3.11/site-packages/django/db/migrations/executor.py", line 18, in __init__
c_django    |     self.loader = MigrationLoader(self.connection)
c_django    |                   ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
c_django    |   File "/usr/local/lib/python3.11/site-packages/django/db/migrations/loader.py", line 58, in __init__
c_django    |     self.build_graph()
c_django    |   File "/usr/local/lib/python3.11/site-packages/django/db/migrations/loader.py", line 235, in build_graph
c_django    |     self.applied_migrations = recorder.applied_migrations()
c_django    | 
c_django    | 
c_django    |  ^^^^
c_django    | ^^^^
c_django    | ^^^^^^
c_django    | ^^^^^
c_django    | ^^^
c_django    | ^^^^^^^
c_django    |   File "/usr/local/lib/python3.11/site-packages/django/db/migrations/recorder.py", line 89, in applied_migrations
c_django    |     if self.has_table():
c_django    |        ^^^^^^^^^^^^^^^^
c_django    |   File "/usr/local/lib/python3.11/site-packages/django/db/migrations/recorder.py", line 63, in has_table
c_django    |     with self.connection.cursor() as cursor:
c_django    |          ^^^^^^^^^^^^^^^^^^^^^^^^
c_django    |   File "/usr/local/lib/python3.11/site-packages/django/utils/asyncio.py", line 26, in inner
c_django    |     return func(*args, **kwargs)
c_django    |            ^^^^^^^^^^^^^^^^^^^^^
c_django    |   File "/usr/local/lib/python3.11/site-packages/django/db/backends/base/base.py", line 316, in cursor
c_django    |     return self._cursor()
c_django    |            ^^^^^^^^^^^^^^
c_django    |   File "/usr/local/lib/python3.11/site-packages/django/db/backends/base/base.py", line 292, in _cursor
c_django    |     self.ensure_connection()
c_django    |   File "/usr/local/lib/python3.11/site-packages/django/utils/asyncio.py", line 26, in inner
c_django    |     return func(*args, **kwargs)
c_django    |            ^^^
c_django    | ^^^^^^^^^^^^^^^^^^
c_django    |   File "/usr/local/lib/python3.11/site-packages/django/db/backends/base/base.py", line 274, in ensure_connection
c_django    |     with self.wrap_database_errors:
c_django    |   File "/usr/local/lib/python3.11/site-packages/django/db/utils.py", line 91, in __exit__
c_django    |     raise dj_exc_value.with_traceback(traceback) from exc_value
c_django    |   File "/usr/local/lib/python3.11/site-packages/django/db/backends/base/base.py", line 275, in ensure_connection
c_django    |     self.connect()
c_django    |   File "/usr/local/lib/python3.11/site-packages/django/utils/asyncio.py", line 26, in inner
c_django    |     return func(*args, **kwargs)
c_django    |            ^^^^^^^^^^^^^^^^^^^^^
c_django    |   File "/usr/local/lib/python3.11/site-packages/django/db/backends/base/base.py", line 256, in connect
c_django    |     self.connection = self.get_new_connection(conn_params)
c_django    |                       ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
c_django    |   File "/usr/local/lib/python3.11/site-packages/django/utils/asyncio.py", line 26, in inner
c_django    |     return func(*args, **kwargs)
c_django    |            ^^^^^^^^^^^^^^^^^^^^^
c_django    |   File "/usr/local/lib/python3.11/site-packages/django/db/backends/postgresql/base.py", line 277, in get_new_connection
c_django    |     connection = self.Database.connect(**conn_params)
c_django    |                  ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
c_django    |   File "/usr/local/lib/python3.11/site-packages/psycopg/connection.py", line 728, in connect
c_django    |     attempts = conninfo_attempts(params)
c_django    |                ^^^^^^^^^^^^^^^^^^^^^^^^^
c_django    |   File "/usr/local/lib/python3.11/site-packages/psycopg/_conninfo_attempts.py", line 45, in conninfo_attempts
c_django    |     raise e.OperationalError(str(last_exc))
c_django    | django.db.utils.OperationalError: [Errno -3] Temporary failure in name resolution

When I search it on google, I did not solve this problem. Even I asked it to chatgpt but no results. How can I solve this problem ? I will deploy my web app but firstly I want to test it on my localhost. Settings are for localhost for now.

Settings:

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.postgresql',
        'NAME': 'fitness',
        'USER': 'postgres',
        'PASSWORD': '123456',
        'HOST': 'db',
        'PORT': '5432',
    }
}

CACHES = {
    'default': {
        'BACKEND': 'django_redis.cache.RedisCache',
        'LOCATION': 'redis://redis:6379/1',
        'OPTIONS': {
            'CLIENT_CLASS': 'django_redis.client.DefaultClient',
        }
    }
}

CHANNEL_LAYERS = {
    'default': {
        'BACKEND': 'channels_redis.core.RedisChannelLayer',
        'CONFIG': {
            "hosts": ["redis://redis:6379/1"],
        },
    },
}

DEBUG = True

ALLOWED_HOSTS = ['*']
Вернуться на верх