I am new to docker and web-development generally. When trying to containerize my django-project I run into an error and can’t solve it for several days. The error might be silly, but I can’t find a solution. I’ll be very glad if someone helps me to solve me the problem. Thank you in advance.
As operational system I use Ubuntu 22.04
Here is the output after commands docker compose build
and docker compose up
:
[+] Running 2/0
⠿ Container test_task-db_task-1 Created 0.0s
⠿ Container test_task_container Created 0.0s
Attaching to test_task-db_task-1, test_task_container
test_task-db_task-1 |
test_task-db_task-1 | PostgreSQL Database directory appears to contain a database; Skipping initialization
test_task-db_task-1 |
test_task-db_task-1 | 2023-06-07 10:46:07.124 UTC [1] LOG: starting PostgreSQL 14.1 on x86_64-pc-linux-musl, compiled by gcc (Alpine 10.3.1_git20211027) 10.3.1 20211027, 64-bit
test_task-db_task-1 | 2023-06-07 10:46:07.125 UTC [1] LOG: listening on IPv4 address "0.0.0.0", port 5432
test_task-db_task-1 | 2023-06-07 10:46:07.125 UTC [1] LOG: listening on IPv6 address "::", port 5432
test_task-db_task-1 | 2023-06-07 10:46:07.147 UTC [1] LOG: listening on Unix socket "/var/run/postgresql/.s.PGSQL.5432"
test_task-db_task-1 | 2023-06-07 10:46:07.163 UTC [21] LOG: database system was shut down at 2023-06-07 10:45:29 UTC
test_task-db_task-1 | 2023-06-07 10:46:07.218 UTC [1] LOG: database system is ready to accept connections
test_task_container | Watching for file changes with StatReloader
test_task_container | Exception in thread django-main-thread:
test_task_container | Traceback (most recent call last):
test_task_container | File "/usr/local/lib/python3.10/site-packages/django/db/backends/base/base.py", line 289, in ensure_connection
test_task_container | self.connect()
test_task_container | File "/usr/local/lib/python3.10/site-packages/django/utils/asyncio.py", line 26, in inner
test_task_container | return func(*args, **kwargs)
test_task_container | File "/usr/local/lib/python3.10/site-packages/django/db/backends/base/base.py", line 270, in connect
test_task_container | self.connection = self.get_new_connection(conn_params)
test_task_container | File "/usr/local/lib/python3.10/site-packages/django/utils/asyncio.py", line 26, in inner
test_task_container | return func(*args, **kwargs)
test_task_container | File "/usr/local/lib/python3.10/site-packages/django/db/backends/postgresql/base.py", line 275, in get_new_connection
test_task_container | connection = self.Database.connect(**conn_params)
test_task_container | File "/usr/local/lib/python3.10/site-packages/psycopg2/__init__.py", line 122, in connect
test_task_container | conn = _connect(dsn, connection_factory=connection_factory, **kwasync)
test_task_container | psycopg2.OperationalError: connection to server on socket "/var/run/postgresql/.s.PGSQL.5432" failed: No such file or directory
test_task_container | Is the server running locally and accepting connections on that socket?
test_task_container |
test_task_container |
test_task_container | The above exception was the direct cause of the following exception:
test_task_container |
test_task_container | Traceback (most recent call last):
test_task_container | File "/usr/local/lib/python3.10/threading.py", line 1016, in _bootstrap_inner
test_task_container | self.run()
test_task_container | File "/usr/local/lib/python3.10/threading.py", line 953, in run
test_task_container | self._target(*self._args, **self._kwargs)
test_task_container | File "/usr/local/lib/python3.10/site-packages/django/utils/autoreload.py", line 64, in wrapper
test_task_container | fn(*args, **kwargs)
test_task_container | File "/usr/local/lib/python3.10/site-packages/django/core/management/commands/runserver.py", line 136, in inner_run
test_task_container | self.check_migrations()
test_task_container | File "/usr/local/lib/python3.10/site-packages/django/core/management/base.py", line 574, in check_migrations
test_task_container | executor = MigrationExecutor(connections[DEFAULT_DB_ALIAS])
test_task_container | File "/usr/local/lib/python3.10/site-packages/django/db/migrations/executor.py", line 18, in __init__
test_task_container | self.loader = MigrationLoader(self.connection)
test_task_container | File "/usr/local/lib/python3.10/site-packages/django/db/migrations/loader.py", line 58, in __init__
test_task_container | self.build_graph()
test_task_container | File "/usr/local/lib/python3.10/site-packages/django/db/migrations/loader.py", line 235, in build_graph
test_task_container | self.applied_migrations = recorder.applied_migrations()
test_task_container | File "/usr/local/lib/python3.10/site-packages/django/db/migrations/recorder.py", line 81, in applied_migrations
test_task_container | if self.has_table():
test_task_container | File "/usr/local/lib/python3.10/site-packages/django/db/migrations/recorder.py", line 57, in has_table
test_task_container | with self.connection.cursor() as cursor:
test_task_container | File "/usr/local/lib/python3.10/site-packages/django/utils/asyncio.py", line 26, in inner
test_task_container | return func(*args, **kwargs)
test_task_container | File "/usr/local/lib/python3.10/site-packages/django/db/backends/base/base.py", line 330, in cursor
test_task_container | return self._cursor()
test_task_container | File "/usr/local/lib/python3.10/site-packages/django/db/backends/base/base.py", line 306, in _cursor
test_task_container | self.ensure_connection()
test_task_container | File "/usr/local/lib/python3.10/site-packages/django/utils/asyncio.py", line 26, in inner
test_task_container | return func(*args, **kwargs)
test_task_container | File "/usr/local/lib/python3.10/site-packages/django/db/backends/base/base.py", line 288, in ensure_connection
test_task_container | with self.wrap_database_errors:
test_task_container | File "/usr/local/lib/python3.10/site-packages/django/db/utils.py", line 91, in __exit__
test_task_container | raise dj_exc_value.with_traceback(traceback) from exc_value
test_task_container | File "/usr/local/lib/python3.10/site-packages/django/db/backends/base/base.py", line 289, in ensure_connection
test_task_container | self.connect()
test_task_container | File "/usr/local/lib/python3.10/site-packages/django/utils/asyncio.py", line 26, in inner
test_task_container | return func(*args, **kwargs)
test_task_container | File "/usr/local/lib/python3.10/site-packages/django/db/backends/base/base.py", line 270, in connect
test_task_container | self.connection = self.get_new_connection(conn_params)
test_task_container | File "/usr/local/lib/python3.10/site-packages/django/utils/asyncio.py", line 26, in inner
test_task_container | return func(*args, **kwargs)
test_task_container | File "/usr/local/lib/python3.10/site-packages/django/db/backends/postgresql/base.py", line 275, in get_new_connection
test_task_container | connection = self.Database.connect(**conn_params)
test_task_container | File "/usr/local/lib/python3.10/site-packages/psycopg2/__init__.py", line 122, in connect
test_task_container | conn = _connect(dsn, connection_factory=connection_factory, **kwasync)
test_task_container | django.db.utils.OperationalError: connection to server on socket "/var/run/postgresql/.s.PGSQL.5432" failed: No such file or directory
test_task_container | Is the server running locally and accepting connections on that socket?
test_task_container |
Here is my docker-compose file:
services:
task:
build: .
volumes:
- .:/test_task
ports:
- 8000:8000
image: app:django
container_name: test_task_container
command: python manage.py runserver 0.0.0.0:8000
db_task:
image: postgres:14.1-alpine
restart: always
env_file:
- .env
ports:
- '5432:5432'
volumes:
- db:/var/lib/postgresql/data
volumes:
db:
driver: local
Here is dockerfile itself:
FROM python:3.10-slim-buster
ENV PYTHONBUFFERED=1
WORKDIR /test_task
COPY requirements.txt requirements.txt
RUN pip3 install -r requirements.txt
.env file:
POSTGRES_HOST=db
POSTGRES_PORT=5432
POSTGRES_USER=postgres
POSTGRES_PASSWORD=postgres
POSTGRES_DB=postgres
DJANGO_SETTINGS_MODULE=core.settings
And database settings in settings.py:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql',
'HOST': os.getenv('POSTGRES_HOST'),
'PORT': os.getenv('POSTGRES_PORT'),
'USER': os.getenv('POSTGRES_USER'),
'PASSWORD': os.getenv('POSTGRES_PASSWORD'),
'NAME': os.getenv('POSTGRES_DB')
}
}
I was trying to containerize my project and open it up on my local server
>Solution :
Several things to consider:
- The services in your compose setup (within the created docker network) – by default – resolve to their name. Hence,
POSTGRES_HOST
should be"db_task"
- the
.env
should be picked up automatically if located in the same location as yourdocker-compose.yaml
. So for consistency, specifyenv_file
for both or neither. - The django app depends on the db. If the db is not yet up and running, the django app can’t connect to it. Hence you should add a
depends_on
to thetask
service, so the app won’t be brought up before the db is ready:
task:
# ...
depends_on:
- db_task