Dockerize Wagtail & PostgreSQL as a development environment

If you want to develop with Wagtail and PostgreSQL in a platform-independent way, using Docker is a good choice. In the following tutorial, I would like to share my approach.

www.phooky.com

  • 아래와 같은 문제가 생겼다.
(.venv) ➜ projectGoguma (main) ✗ docker-compose exec web python manage.py createsuperuser --settings=projectGoguma.settings.dev

You have 162 unapplied migration(s). Your project may not work properly until you apply the migrations for app(s): admin, auth, contenttypes, home, sessions, taggit, wagtailadmin, wagtailcore, wagtaildocs, wagtailembeds, wagtailforms, wagtailimages, wagtailredirects, wagtailsearch, wagtailusers.
Run 'python manage.py migrate' to apply them.
Traceback (most recent call last):
  File "/usr/local/lib/python3.8/site-packages/django/db/backends/utils.py", line 89, in _execute
    return self.cursor.execute(sql, params)
psycopg2.errors.UndefinedTable: relation "auth_user" does not exist
LINE 1: ...user"."is_active", "auth_user"."date_joined" FROM "auth_user...
                                                             ^


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

Traceback (most recent call last):
  File "manage.py", line 10, in <module>
    execute_from_command_line(sys.argv)
  File "/usr/local/lib/python3.8/site-packages/django/core/management/__init__.py", line 442, in execute_from_command_line
    utility.execute()
  File "/usr/local/lib/python3.8/site-packages/django/core/management/__init__.py", line 436, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/usr/local/lib/python3.8/site-packages/django/core/management/base.py", line 412, in run_from_argv
    self.execute(*args, **cmd_options)
  File "/usr/local/lib/python3.8/site-packages/django/contrib/auth/management/commands/createsuperuser.py", line 88, in execute
    return super().execute(*args, **options)
  File "/usr/local/lib/python3.8/site-packages/django/core/management/base.py", line 458, in execute
    output = self.handle(*args, **options)
  File "/usr/local/lib/python3.8/site-packages/django/contrib/auth/management/commands/createsuperuser.py", line 109, in handle
    default_username = get_default_username(database=database)
  File "/usr/local/lib/python3.8/site-packages/django/contrib/auth/management/__init__.py", line 168, in get_default_username
    auth_app.User._default_manager.db_manager(database).get(
  File "/usr/local/lib/python3.8/site-packages/django/db/models/manager.py", line 87, in manager_method
    return getattr(self.get_queryset(), name)(*args, **kwargs)
  File "/usr/local/lib/python3.8/site-packages/django/db/models/query.py", line 633, in get
    num = len(clone)
  File "/usr/local/lib/python3.8/site-packages/django/db/models/query.py", line 380, in __len__
    self._fetch_all()
  File "/usr/local/lib/python3.8/site-packages/django/db/models/query.py", line 1881, in _fetch_all
    self._result_cache = list(self._iterable_class(self))
  File "/usr/local/lib/python3.8/site-packages/django/db/models/query.py", line 91, in __iter__
    results = compiler.execute_sql(
  File "/usr/local/lib/python3.8/site-packages/django/db/models/sql/compiler.py", line 1562, in execute_sql
    cursor.execute(sql, params)
  File "/usr/local/lib/python3.8/site-packages/django/db/backends/utils.py", line 102, in execute
    return super().execute(sql, params)
  File "/usr/local/lib/python3.8/site-packages/django/db/backends/utils.py", line 67, in execute
    return self._execute_with_wrappers(
  File "/usr/local/lib/python3.8/site-packages/django/db/backends/utils.py", line 80, in _execute_with_wrappers
    return executor(sql, params, many, context)
  File "/usr/local/lib/python3.8/site-packages/django/db/backends/utils.py", line 89, in _execute
    return self.cursor.execute(sql, params)
  File "/usr/local/lib/python3.8/site-packages/django/db/utils.py", line 91, in __exit__
    raise dj_exc_value.with_traceback(traceback) from exc_value
  File "/usr/local/lib/python3.8/site-packages/django/db/backends/utils.py", line 89, in _execute
    return self.cursor.execute(sql, params)
django.db.utils.ProgrammingError: relation "auth_user" does not exist
LINE 1: ...user"."is_active", "auth_user"."date_joined" FROM "auth_user...
                                                             ^
  • 이리저리 다 수정을 한 것 같은데 왜 안될까???
  • docker container에 직접 attach를 해보니...
  • 엥???
(.venv) ➜ projectGoguma (main) ✗ docker ps -a
CONTAINER ID   IMAGE                  COMMAND                   CREATED              STATUS                   PORTS                                       NAMES
c281852e6c92   projectgoguma_web      "/app/entrypoint.sh …"   About a minute ago   Up About a minute        0.0.0.0:8000->8000/tcp, :::8000->8000/tcp   projectgoguma_web_1
06f0d1750419   postgres:13.5-alpine   "docker-entrypoint.s…"   About a minute ago   Up About a minute        0.0.0.0:5432->5432/tcp, :::5432->5432/tcp   projectgoguma_db_1
59ee14bdc489   hello-world            "/hello"                  3 weeks ago          Exited (0) 3 weeks ago                                               gallant_lamport
(.venv) ➜ projectGoguma (main) ✗ docker attach projectgoguma_web_1
/app/entrypoint.sh: 7: /app/entrypoint.sh: nc: not found
/app/entrypoint.sh: 7: /app/entrypoint.sh: nc: not found
/app/entrypoint.sh: 7: /app/entrypoint.sh: nc: not found
/app/entrypoint.sh: 7: /app/entrypoint.sh: nc: not found
/app/entrypoint.sh: 7: /app/entrypoint.sh: nc: not found
/app/entrypoint.sh: 7: /app/entrypoint.sh: nc: not found
/app/entrypoint.sh: 7: /app/entrypoint.sh: nc: not found
/app/entrypoint.sh: 7: /app/entrypoint.sh: nc: not found
/app/entrypoint.sh: 7: /app/entrypoint.sh: nc: not found
/app/entrypoint.sh: 7: /app/entrypoint.sh: nc: not found
/app/entrypoint.sh: 7: /app/entrypoint.sh: nc: not found
  • nc를 왜 필요로 하지?
  • 잘 모르겠지만 Dockerfile 에서 netcat 설치를 추가해 보니 문제 해결
# Install system packages required by Wagtail and Django.
RUN apt-get update --yes --quiet && apt-get install --yes --quiet --no-install-recommends \
    build-essential \
    libpq-dev \
    libmariadbclient-dev \
    libjpeg62-turbo-dev \
    zlib1g-dev \
    libwebp-dev \
    netcat \
    # environ \
    # postgresql-dev \
    # python3-dev \
    # musl-dev \
  • 잘된다.
(.venv) ➜ projectGoguma (main) ✗ docker-compose up -d --build                    
Building web
[+] Building 57.1s (14/14) FINISHED                                                                      docker:default
 => [internal] load build definition from Dockerfile                                                               0.0s
 => => transferring dockerfile: 2.50kB                                                                             0.0s
 => [internal] load .dockerignore                                                                                  0.0s
 => => transferring context: 418B                                                                                  0.0s
 => [internal] load metadata for docker.io/library/python:3.8.1-slim-buster                                        1.3s
 => [internal] load build context                                                                                  0.0s
 => => transferring context: 70B                                                                                   0.0s
 => [1/9] FROM docker.io/library/python:3.8.1-slim-buster@sha256:73f3903470a6e55202a6bb989c23b047487eb1728feba655  0.0s
 => CACHED [2/9] RUN useradd wagtail                                                                               0.0s
 => [3/9] RUN apt-get update --yes --quiet && apt-get install --yes --quiet --no-install-recommends     build-es  23.8s
 => [4/9] RUN pip install "gunicorn==20.0.4"                                                                       1.9s 
 => [5/9] COPY requirements.txt /                                                                                  0.1s 
 => [6/9] RUN pip install -r /requirements.txt                                                                    27.9s 
 => [7/9] WORKDIR /app                                                                                             0.0s 
 => [8/9] RUN chown wagtail:wagtail /app                                                                           0.3s 
 => [9/9] COPY entrypoint.sh /app/entrypoint.sh                                                                    0.0s 
 => exporting to image                                                                                             1.6s 
 => => exporting layers                                                                                            1.6s 
 => => writing image sha256:5f7b2c8cec2309bcf3f45e6a3938484c3d7c90b29cef5bcb0ffb787b14bbf6ef                       0.0s 
 => => naming to docker.io/library/projectgoguma_web                                                               0.0s
Creating projectgoguma_db_1 ... done
Creating projectgoguma_web_1 ... done
(.venv) ➜ projectGoguma (main) ✗ docker-compose exec web python manage.py createsuperuser --settings=projectGoguma.settings.dev
Username (leave blank to use 'wagtail'):
  • 떴다!!!

반응형

'django & Wagtail' 카테고리의 다른 글

Wagtail Tutorial  (0) 2023.08.14
Wagtail 설정 with postgreSql  (0) 2023.08.14

+ Recent posts