Saleor: Error while running migrations command

Created on 8 Jan 2019  Â·  8Comments  Â·  Source: mirumee/saleor

What I'm trying to achieve

…build and run the containers using docker-compose to understand the work flow of source code

Steps to reproduce the problem

  1. (myenv) ganesh@ganesh-Latitude-E6420:~/learning/saleor$ docker-compose run web python3 manage.py migrate
    Starting saleor_db_1 ...
    Starting saleor_redis_1 ...
    Starting saleor_db_1 ... done
    Traceback (most recent call last):
    File "/usr/local/lib/python3.6/site-packages/django/db/backends/base/base.py", line 216, in ensure_connection
    self.connect()
    File "/usr/local/lib/python3.6/site-packages/django/db/backends/base/base.py", line 194, in connect
    self.connection = self.get_new_connection(conn_params)
    File "/usr/local/lib/python3.6/site-packages/django/db/backends/postgresql/base.py", line 178, in get_new_connection
    connection = Database.connect(*conn_params)
    File "/usr/local/lib/python3.6/site-packages/psycopg2/__init__.py", line 130, in connect
    conn = _connect(dsn, connection_factory=connection_factory, *
    kwasync)
    psycopg2.OperationalError: could not translate host name "db" to address: Temporary failure in name resolution

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
File "manage.py", line 10, in
execute_from_command_line(sys.argv)
File "/usr/local/lib/python3.6/site-packages/django/core/management/__init__.py", line 381, in execute_from_command_line
utility.execute()
File "/usr/local/lib/python3.6/site-packages/django/core/management/__init__.py", line 375, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/usr/local/lib/python3.6/site-packages/django/core/management/base.py", line 316, in run_from_argv
self.execute(args, cmd_options)
File "/usr/local/lib/python3.6/site-packages/django/core/management/base.py", line 353, in execute
output = self.handle(
args, *options)
File "/usr/local/lib/python3.6/site-packages/django/core/management/base.py", line 83, in wrapped
res = handle_func(
args, *kwargs)
File "/usr/local/lib/python3.6/site-packages/django/core/management/commands/migrate.py", line 82, in handle
executor = MigrationExecutor(connection, self.migration_progress_callback)
File "/usr/local/lib/python3.6/site-packages/django/db/migrations/executor.py", line 18, in __init__
self.loader = MigrationLoader(self.connection)
File "/usr/local/lib/python3.6/site-packages/django/db/migrations/loader.py", line 49, in __init__
self.build_graph()
File "/usr/local/lib/python3.6/site-packages/django/db/migrations/loader.py", line 212, in build_graph
self.applied_migrations = recorder.applied_migrations()
File "/usr/local/lib/python3.6/site-packages/django/db/migrations/recorder.py", line 61, in applied_migrations
if self.has_table():
File "/usr/local/lib/python3.6/site-packages/django/db/migrations/recorder.py", line 44, in has_table
return self.Migration._meta.db_table in self.connection.introspection.table_names(self.connection.cursor())
File "/usr/local/lib/python3.6/site-packages/django/db/backends/base/base.py", line 255, in cursor
return self._cursor()
File "/usr/local/lib/python3.6/site-packages/django/db/backends/base/base.py", line 232, in _cursor
self.ensure_connection()
File "/usr/local/lib/python3.6/site-packages/django/db/backends/base/base.py", line 216, in ensure_connection
self.connect()
File "/usr/local/lib/python3.6/site-packages/django/db/utils.py", line 89, in __exit__
raise dj_exc_value.with_traceback(traceback) from exc_value
File "/usr/local/lib/python3.6/site-packages/django/db/backends/base/base.py", line 216, in ensure_connection
self.connect()
File "/usr/local/lib/python3.6/site-packages/django/db/backends/base/base.py", line 194, in connect
self.connection = self.get_new_connection(conn_params)
File "/usr/local/lib/python3.6/site-packages/django/db/backends/postgresql/base.py", line 178, in get_new_connection
connection = Database.connect(
conn_params)
File "/usr/local/lib/python3.6/site-packages/psycopg2/__init__.py", line 130, in connect
conn = _connect(dsn, connection_factory=connection_factory, *
kwasync)
django.db.utils.OperationalError: could not translate host name "db" to address: Temporary failure in name resolution

  1. MY docker-compose.yaml file is
    version: '2'

services:
web:
build:
context: .
dockerfile: ./Dockerfile
args:
STATIC_URL: '/static/'
restart: unless-stopped
networks:
- saleor-backend-tier
ports:
- 8000:8000
env_file: common.env
depends_on:
- db
- redis
command: python manage.py runserver 0.0.0.0:8000

db:
image: library/postgres:latest
restart: unless-stopped
networks:
- saleor-backend-tier
volumes:
- sale-db:/var/lib/postgresql
ports:
- 5432:5432
environment:
- POSTGRES_USER=postgres
- POSTGRES_PASSWORD=ganesh55

redis:
image: library/redis:latest
restart: unless-stopped
networks:
- saleor-backend-tier
volumes:
- saleor-redis:/data
ports:
- 6379:6379

celery:
build:
context: .
dockerfile: ./Dockerfile
args:
STATIC_URL: '/static/'
command: celery -A saleor worker --app=saleor.celeryconf:app --loglevel=info
restart: unless-stopped
networks:
- saleor-backend-tier
volumes:
- .:/app:Z
env_file: common.env
depends_on:
- redis

volumes:
sale-db:
driver: local
saleor-redis:
driver: local

networks:
saleor-backend-tier:
driver: bridge

  1. database uri in settings.py is as
    DATABASES = {
    'default': dj_database_url.config(
    default='postgres://postgres:ganesh55@localhost:5432/sale',
    conn_max_age=600)}

What I expected to happen

… i want to build a docker image

Screenshots

System information
Operating system:
Browser:

Most helpful comment

Just note that the changes that you mentioned:

DATABASES = { 'default': dj_database_url.config( default='postgres://postgres:ganesh55@localhost:5432/sale', conn_max_age=600)}

Are being overridden by the DATABASE_URL environment variable in common.env. You should edit that file instead.

But the hostname db (set in common.env) should correctly resolve. I can't get a look into it during this week. You will need to wait for an another answer.

All 8 comments

Just note that the changes that you mentioned:

DATABASES = { 'default': dj_database_url.config( default='postgres://postgres:ganesh55@localhost:5432/sale', conn_max_age=600)}

Are being overridden by the DATABASE_URL environment variable in common.env. You should edit that file instead.

But the hostname db (set in common.env) should correctly resolve. I can't get a look into it during this week. You will need to wait for an another answer.

Thanks for the reply and also found DATABASE_URL in one more file, have to change it or not?

The other files are for the CIs. It's only common.env that you need to adapt with the DB password and login.

django.db.utils.OperationalError: could not connect to server: Connection refused
Is the server running on host "localhost" (127.0.0.1) and accepting
TCP/IP connections on port 5432?
could not connect to server: Cannot assign requested address
Is the server running on host "localhost" (::1) and accepting
TCP/IP connections on port 5432?
This is the error i got again?

@ganeshsingamaneni Just run the docker-compose command again and it should work

finally got it

Watching for file changes with StatReloader
Exception in thread django-main-thread:
Traceback (most recent call last):
File "/home/sampath/.local/lib/python3.6/site-packages/django/db/backends/postgresql/base.py", line 20, in
import psycopg2 as Database
ModuleNotFoundError: No module named 'psycopg2'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File "/usr/lib/python3.6/threading.py", line 916, in _bootstrap_inner
self.run()
File "/usr/lib/python3.6/threading.py", line 864, in run
self._target(self._args, *self._kwargs)
File "/home/sampath/.local/lib/python3.6/site-packages/django/utils/autoreload.py", line 54, in wrapper
fn(args, *kwargs)
File "/home/sampath/.local/lib/python3.6/site-packages/django/core/management/commands/runserver.py", line 109, in inner_run
autoreload.raise_last_exception()
File "/home/sampath/.local/lib/python3.6/site-packages/django/utils/autoreload.py", line 77, in raise_last_exception
raise _exception[1]
File "/home/sampath/.local/lib/python3.6/site-packages/django/core/management/__init__.py", line 337, in execute
autoreload.check_errors(django.setup)()
File "/home/sampath/.local/lib/python3.6/site-packages/django/utils/autoreload.py", line 54, in wrapper
fn(args, *kwargs)
File "/home/sampath/.local/lib/python3.6/site-packages/django/__init__.py", line 24, in setup
apps.populate(settings.INSTALLED_APPS)
File "/home/sampath/.local/lib/python3.6/site-packages/django/apps/registry.py", line 114, in populate
app_config.import_models()
File "/home/sampath/.local/lib/python3.6/site-packages/django/apps/config.py", line 211, in import_models
self.models_module = import_module(models_module_name)
File "/usr/lib/python3.6/importlib/__init__.py", line 126, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "", line 994, in _gcd_import
File "", line 971, in _find_and_load
File "", line 955, in _find_and_load_unlocked
File "", line 665, in _load_unlocked
File "", line 678, in exec_module
File "", line 219, in _call_with_frames_removed
File "/home/sampath/.local/lib/python3.6/site-packages/django/contrib/auth/models.py", line 2, in
from django.contrib.auth.base_user import AbstractBaseUser, BaseUserManager
File "/home/sampath/.local/lib/python3.6/site-packages/django/contrib/auth/base_user.py", line 47, in
class AbstractBaseUser(models.Model):
File "/home/sampath/.local/lib/python3.6/site-packages/django/db/models/base.py", line 117, in __new__
new_class.add_to_class('_meta', Options(meta, app_label))
File "/home/sampath/.local/lib/python3.6/site-packages/django/db/models/base.py", line 321, in add_to_class
value.contribute_to_class(cls, name)
File "/home/sampath/.local/lib/python3.6/site-packages/django/db/models/options.py", line 204, in contribute_to_class
self.db_table = truncate_name(self.db_table, connection.ops.max_name_length())
File "/home/sampath/.local/lib/python3.6/site-packages/django/db/__init__.py", line 28, in __getattr__
return getattr(connections[DEFAULT_DB_ALIAS], item)
File "/home/sampath/.local/lib/python3.6/site-packages/django/db/utils.py", line 201, in __getitem__
backend = load_backend(db['ENGINE'])
File "/home/sampath/.local/lib/python3.6/site-packages/django/db/utils.py", line 110, in load_backend
return import_module('%s.base' % backend_name)
File "/usr/lib/python3.6/importlib/__init__.py", line 126, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "/home/sampath/.local/lib/python3.6/site-packages/django/db/backends/postgresql/base.py", line 24, in
raise ImproperlyConfigured("Error loading psycopg2 module: %s" % e)
django.core.exceptions.ImproperlyConfigured: Error loading psycopg2 module: No module named 'psycopg2'

where should i write code for psycop2 ?? how to configured?

Was this page helpful?
0 / 5 - 0 ratings