Reputation: 11
I have currently a system built with docker-compose, it creates a Django application. I've use a database inside a container (postgresql) in my testing build. I want upload my docker image to docker-hub.
When i run
$ sudo docker-compose up
Server goes up sucecesfully
Recreating rest_api_db_1 ... done
Recreating rest_api_web_1 ... done
Attaching to rest_api_db_1, rest_api_web_1
db_1 |
db_1 | PostgreSQL Database directory appears to contain a database; Skipping initialization
db_1 |
db_1 | 2021-06-08 07:22:10.698 UTC [1] LOG: starting PostgreSQL 13.3 (Debian 13.3-1.pgdg100+1) on x86_64-pc-linux-gnu, compiled by gcc (Debian 8.3.0-6) 8.3.0, 64-bit
db_1 | 2021-06-08 07:22:10.699 UTC [1] LOG: listening on IPv4 address "0.0.0.0", port 5432
db_1 | 2021-06-08 07:22:10.699 UTC [1] LOG: listening on IPv6 address "::", port 5432
db_1 | 2021-06-08 07:22:10.708 UTC [1] LOG: listening on Unix socket "/var/run/postgresql/.s.PGSQL.5432"
db_1 | 2021-06-08 07:22:10.719 UTC [26] LOG: database system was shut down at 2021-06-08 07:15:22 UTC
db_1 | 2021-06-08 07:22:10.726 UTC [1] LOG: database system is ready to accept connections
web_1 | Operations to perform:
web_1 | Apply all migrations: admin, api_bus_routes, auth, contenttypes, sessions
web_1 | Running migrations:
web_1 | No migrations to apply.
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 | June 08, 2021 - 07:22:12
web_1 | Django version 3.2.4, using settings 'rest_api.settings'
web_1 | Starting development server at http://0.0.0.0:8000/
web_1 | Quit the server with CONTROL-C.
But when i tried to build and run
$ sudo docker-compose build
$ sudo docker run rest_api_web
I get an error
psycopg2.OperationalError: could not translate host name "db" to address: Name or service not known
Watching for file changes with StatReloader
Performing system checks...
System check identified no issues (0 silenced).
Exception in thread django-main-thread:
Traceback (most recent call last):
File "/usr/local/lib/python3.9/site-packages/django/db/backends/base/base.py", line 219, in ensure_connection
self.connect()
File "/usr/local/lib/python3.9/site-packages/django/utils/asyncio.py", line 26, in inner
return func(*args, **kwargs)
File "/usr/local/lib/python3.9/site-packages/django/db/backends/base/base.py", line 200, in connect
self.connection = self.get_new_connection(conn_params)
File "/usr/local/lib/python3.9/site-packages/django/utils/asyncio.py", line 26, in inner
return func(*args, **kwargs)
File "/usr/local/lib/python3.9/site-packages/django/db/backends/postgresql/base.py", line 187, in get_new_connection
connection = Database.connect(**conn_params)
File "/usr/local/lib/python3.9/site-packages/psycopg2/__init__.py", line 127, in connect
conn = _connect(dsn, connection_factory=connection_factory, **kwasync)
psycopg2.OperationalError: could not translate host name "db" to address: Name or service not known
My Docker-compose.yml
version: "3.8"
services:
db:
image: postgres
volumes:
- ./data/db:/var/lib/postgresql/data
environment:
- POSTGRES_DB=postgres
- POSTGRES_USER=postgres
- POSTGRES_PASSWORD=postgres
web:
build: .
depends_on:
- db
command: bash -c "python manage.py migrate &&
python manage.py runserver 0.0.0.0:8000"
volumes:
- .:/code
ports:
- "8000:8000"
My Dockerfile
# syntax=docker/dockerfile:1
FROM python:3
ENV PYTHONUNBUFFERED=1
RUN mkdir /code
WORKDIR /code
COPY . /code/
RUN pip install -r requirements.txt
EXPOSE 8000
CMD ["python", "manage.py", "runserver", "0.0.0.0:8000"]
My settings.py
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql_psycopg2',
'NAME': os.environ.get('POSTGRES_DB', default='postgres'),
'USER': os.environ.get('POSTGRES_USER', default='postgres'),
'PASSWORD': os.environ.get('POSTGRES_PASSWORD', default='postgres'),
'HOST': os.environ.get('POSTGRES_HOST', default='db'),
'PORT': "5432"
}
}
Upvotes: 1
Views: 1122
Reputation: 345
Docker-compose can only run with all services and creates its own network on with db
exists. But if you run a single container, the network doesn't exist and db-container can not be found. Either run the docker-compose, or in your settings.py define where the db is with a valid host.
Upvotes: 2