Docker: Admin user is created twice #870

Closed
opened 2025-12-29 16:26:29 +01:00 by adam · 1 comment
Owner

Originally created by @cimnine on GitHub (Apr 18, 2017).

Issue type: Bug Report

Python version: 2.7
NetBox version: Via Docker: digitalocean/netbox:v2.0-beta2 (d40c1651c8f5)

Using the docker-compose file, (slightly modified to fetch netbox from Docker Hub), I ran docker-compose up, waited until all initialised and then ran docker-compose down.

When I start the services using docker-compose up again, then netbox won't start anymore because of the following error:

postgres_1  | ERROR:  duplicate key value violates unique constraint "auth_user_username_key"
postgres_1  | DETAIL:  Key (username)=(admin) already exists.
postgres_1  | STATEMENT:  INSERT INTO "auth_user" ("password", "last_login", "is_superuser", "username", "first_name", "last_name", "email", "is_staff", "is_active", "date_joined") VALUES ('pbkdf2_sha256$36000$0FiX4CLRYA4l$BOFIAEm4DrX5NqGwt7t3o7AoyL6pXPsXMPkVhr3FM38=', NULL, true, 'admin', '', '', 'admin@example.com', true, true, '2017-04-18T10:19:25.248805+00:00'::timestamptz) RETURNING "auth_user"."id"
netbox_1    | Traceback (most recent call last):
netbox_1    |   File "/opt/netbox/netbox/manage.py", line 10, in <module>
netbox_1    |     execute_from_command_line(sys.argv)
netbox_1    |   File "/usr/local/lib/python2.7/site-packages/django/core/management/__init__.py", line 363, in execute_from_command_line
netbox_1    |     utility.execute()
netbox_1    |   File "/usr/local/lib/python2.7/site-packages/django/core/management/__init__.py", line 355, in execute
netbox_1    |     self.fetch_command(subcommand).run_from_argv(self.argv)
netbox_1    |   File "/usr/local/lib/python2.7/site-packages/django/core/management/base.py", line 283, in run_from_argv
netbox_1    |     self.execute(*args, **cmd_options)
netbox_1    |   File "/usr/local/lib/python2.7/site-packages/django/core/management/base.py", line 330, in execute
netbox_1    |     output = self.handle(*args, **options)
netbox_1    |   File "/usr/local/lib/python2.7/site-packages/django/core/management/commands/shell.py", line 101, in handle
netbox_1    |     exec(sys.stdin.read())
netbox_1    |   File "<string>", line 1, in <module>
netbox_1    |   File "/usr/local/lib/python2.7/site-packages/django/contrib/auth/models.py", line 170, in create_superuser
netbox_1    |     return self._create_user(username, email, password, **extra_fields)
netbox_1    |   File "/usr/local/lib/python2.7/site-packages/django/contrib/auth/models.py", line 153, in _create_user
netbox_1    |     user.save(using=self._db)
netbox_1    |   File "/usr/local/lib/python2.7/site-packages/django/contrib/auth/base_user.py", line 80, in save
netbox_1    |     super(AbstractBaseUser, self).save(*args, **kwargs)
netbox_1    |   File "/usr/local/lib/python2.7/site-packages/django/db/models/base.py", line 806, in save
netbox_1    |     force_update=force_update, update_fields=update_fields)
netbox_1    |   File "/usr/local/lib/python2.7/site-packages/django/db/models/base.py", line 836, in save_base
netbox_1    |     updated = self._save_table(raw, cls, force_insert, force_update, using, update_fields)
netbox_1    |   File "/usr/local/lib/python2.7/site-packages/django/db/models/base.py", line 922, in _save_table
netbox_1    |     result = self._do_insert(cls._base_manager, using, fields, update_pk, raw)
netbox_1    |   File "/usr/local/lib/python2.7/site-packages/django/db/models/base.py", line 961, in _do_insert
netbox_1    |     using=using, raw=raw)
netbox_1    |   File "/usr/local/lib/python2.7/site-packages/django/db/models/manager.py", line 85, in manager_method
netbox_1    |     return getattr(self.get_queryset(), name)(*args, **kwargs)
netbox_1    |   File "/usr/local/lib/python2.7/site-packages/django/db/models/query.py", line 1060, in _insert
netbox_1    |     return query.get_compiler(using=using).execute_sql(return_id)
netbox_1    |   File "/usr/local/lib/python2.7/site-packages/django/db/models/sql/compiler.py", line 1099, in execute_sql
netbox_1    |     cursor.execute(sql, params)
netbox_1    |   File "/usr/local/lib/python2.7/site-packages/django/db/backends/utils.py", line 65, in execute
netbox_1    |     return self.cursor.execute(sql, params)
netbox_1    |   File "/usr/local/lib/python2.7/site-packages/django/db/utils.py", line 94, in __exit__
netbox_1    |     six.reraise(dj_exc_type, dj_exc_value, traceback)
netbox_1    |   File "/usr/local/lib/python2.7/site-packages/django/db/backends/utils.py", line 65, in execute
netbox_1    |     return self.cursor.execute(sql, params)
netbox_1    | django.db.utils.IntegrityError: duplicate key value violates unique constraint "auth_user_username_key"
netbox_1    | DETAIL:  Key (username)=(admin) already exists.

The problem seems to be in docker-entrypoint.sh:16, where the superuser is created no matter what, i.e. without a check, if it was already created before.

Originally created by @cimnine on GitHub (Apr 18, 2017). ### Issue type: Bug Report **Python version:** 2.7 **NetBox version:** Via Docker: digitalocean/netbox:v2.0-beta2 (d40c1651c8f5) Using the [docker-compose file](https://github.com/digitalocean/netbox/blob/develop/docker-compose.yml), (slightly modified to fetch netbox from [Docker Hub](https://hub.docker.com/r/digitalocean/netbox/)), I ran `docker-compose up`, waited until all initialised and then ran `docker-compose down`. When I start the services using `docker-compose up` again, then netbox won't start anymore because of the following error: ``` postgres_1 | ERROR: duplicate key value violates unique constraint "auth_user_username_key" postgres_1 | DETAIL: Key (username)=(admin) already exists. postgres_1 | STATEMENT: INSERT INTO "auth_user" ("password", "last_login", "is_superuser", "username", "first_name", "last_name", "email", "is_staff", "is_active", "date_joined") VALUES ('pbkdf2_sha256$36000$0FiX4CLRYA4l$BOFIAEm4DrX5NqGwt7t3o7AoyL6pXPsXMPkVhr3FM38=', NULL, true, 'admin', '', '', 'admin@example.com', true, true, '2017-04-18T10:19:25.248805+00:00'::timestamptz) RETURNING "auth_user"."id" netbox_1 | Traceback (most recent call last): netbox_1 | File "/opt/netbox/netbox/manage.py", line 10, in <module> netbox_1 | execute_from_command_line(sys.argv) netbox_1 | File "/usr/local/lib/python2.7/site-packages/django/core/management/__init__.py", line 363, in execute_from_command_line netbox_1 | utility.execute() netbox_1 | File "/usr/local/lib/python2.7/site-packages/django/core/management/__init__.py", line 355, in execute netbox_1 | self.fetch_command(subcommand).run_from_argv(self.argv) netbox_1 | File "/usr/local/lib/python2.7/site-packages/django/core/management/base.py", line 283, in run_from_argv netbox_1 | self.execute(*args, **cmd_options) netbox_1 | File "/usr/local/lib/python2.7/site-packages/django/core/management/base.py", line 330, in execute netbox_1 | output = self.handle(*args, **options) netbox_1 | File "/usr/local/lib/python2.7/site-packages/django/core/management/commands/shell.py", line 101, in handle netbox_1 | exec(sys.stdin.read()) netbox_1 | File "<string>", line 1, in <module> netbox_1 | File "/usr/local/lib/python2.7/site-packages/django/contrib/auth/models.py", line 170, in create_superuser netbox_1 | return self._create_user(username, email, password, **extra_fields) netbox_1 | File "/usr/local/lib/python2.7/site-packages/django/contrib/auth/models.py", line 153, in _create_user netbox_1 | user.save(using=self._db) netbox_1 | File "/usr/local/lib/python2.7/site-packages/django/contrib/auth/base_user.py", line 80, in save netbox_1 | super(AbstractBaseUser, self).save(*args, **kwargs) netbox_1 | File "/usr/local/lib/python2.7/site-packages/django/db/models/base.py", line 806, in save netbox_1 | force_update=force_update, update_fields=update_fields) netbox_1 | File "/usr/local/lib/python2.7/site-packages/django/db/models/base.py", line 836, in save_base netbox_1 | updated = self._save_table(raw, cls, force_insert, force_update, using, update_fields) netbox_1 | File "/usr/local/lib/python2.7/site-packages/django/db/models/base.py", line 922, in _save_table netbox_1 | result = self._do_insert(cls._base_manager, using, fields, update_pk, raw) netbox_1 | File "/usr/local/lib/python2.7/site-packages/django/db/models/base.py", line 961, in _do_insert netbox_1 | using=using, raw=raw) netbox_1 | File "/usr/local/lib/python2.7/site-packages/django/db/models/manager.py", line 85, in manager_method netbox_1 | return getattr(self.get_queryset(), name)(*args, **kwargs) netbox_1 | File "/usr/local/lib/python2.7/site-packages/django/db/models/query.py", line 1060, in _insert netbox_1 | return query.get_compiler(using=using).execute_sql(return_id) netbox_1 | File "/usr/local/lib/python2.7/site-packages/django/db/models/sql/compiler.py", line 1099, in execute_sql netbox_1 | cursor.execute(sql, params) netbox_1 | File "/usr/local/lib/python2.7/site-packages/django/db/backends/utils.py", line 65, in execute netbox_1 | return self.cursor.execute(sql, params) netbox_1 | File "/usr/local/lib/python2.7/site-packages/django/db/utils.py", line 94, in __exit__ netbox_1 | six.reraise(dj_exc_type, dj_exc_value, traceback) netbox_1 | File "/usr/local/lib/python2.7/site-packages/django/db/backends/utils.py", line 65, in execute netbox_1 | return self.cursor.execute(sql, params) netbox_1 | django.db.utils.IntegrityError: duplicate key value violates unique constraint "auth_user_username_key" netbox_1 | DETAIL: Key (username)=(admin) already exists. ``` The problem seems to be in [docker-entrypoint.sh:16](https://github.com/digitalocean/netbox/blob/develop/docker/docker-entrypoint.sh#L16), where the superuser is created no matter what, i.e. without a check, if it was already created before.
adam added the status: duplicate label 2025-12-29 16:26:29 +01:00
adam closed this issue 2025-12-29 16:26:29 +01:00
Author
Owner

@zachmoody commented on GitHub (Apr 18, 2017):

Hi thanks for bringing it up. There's a workaround in #1056, longterm status is that docker stuff is getting its own repo.

@zachmoody commented on GitHub (Apr 18, 2017): Hi thanks for bringing it up. There's a workaround in #1056, longterm status is that docker stuff is getting its own repo.
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/netbox#870