简体   繁体   中英

How to discard and rebuild a postgres container using docker compose

So I have a docker-compose file that has 2 services app and db

version: '3.9'

services:
  app:
    build:
      context: .
    command: >
      sh -c "python manage.py wait_for_db &&
             python manage.py makemigrations &&
             python manage.py migrate &&
             python manage.py collectstatic --noinput &&
             python manage.py runserver 0.0.0.0:8000"
    ports:
      - 8000:8000
    volumes:
      - .:/app
      - ./data/web:/vol/web
    environment:
      - SECRET_KEY=devsecretkey
      - DEBUG=1
      - DB_HOST=db
      - DB_NAME=devdb
      - DB_USER=devuser
      - DB_PASS=changeme
    depends_on:
      - db

  db:
    image: postgres:13-alpine
    environment:
      - POSTGRES_DB=devdb
      - POSTGRES_USER=devuser
      - POSTGRES_PASSWORD=changeme

I changed the Django model, deleted the old migration files (which I shouldn't have done) and then did a 'manage.py migrate --fake zero' and now on migrating I get an obvious error that the table already exists in the Postgres container.

app_1  |   File "/py/lib/python3.9/site-packages/django/db/backends/utils.py", line 99, in execute
app_1  |     return super().execute(sql, params)
app_1  |   File "/py/lib/python3.9/site-packages/django/db/backends/utils.py", line 67, in execute
app_1  |     return self._execute_with_wrappers(sql, params, many=False, executor=self._execute)
app_1  |   File "/py/lib/python3.9/site-packages/django/db/backends/utils.py", line 76, in _execute_with_wrappers
app_1  |     return executor(sql, params, many, context)
app_1  |   File "/py/lib/python3.9/site-packages/django/db/backends/utils.py", line 85, in _execute
app_1  |     return self.cursor.execute(sql, params)
app_1  |   File "/py/lib/python3.9/site-packages/django/db/utils.py", line 90, in __exit__
app_1  |     raise dj_exc_value.with_traceback(traceback) from exc_value
app_1  |   File "/py/lib/python3.9/site-packages/django/db/backends/utils.py", line 83, in _execute
app_1  |     return self.cursor.execute(sql)
app_1  | django.db.utils.ProgrammingError: relation "search_genestorage" already exists

or from db

db_1   | 2022-04-03 01:29:49.231 UTC [31] ERROR:  relation "search_genestorage" already exists
db_1   | 2022-04-03 01:29:49.231 UTC [31] STATEMENT:  CREATE TABLE "search_genestorage" ("id" serial NOT NULL PRIMARY KEY, "chromosome" varchar(10) NOT NULL, "start_pos" integer NOT NULL, "end_pos" integer NOT NULL, "observed" text NOT NULL, "reference" text NULL, "zygosity" text NOT NULL, "refGene_function" text NULL, "refGene_gene" text NOT NULL, "quality" text NULL, "refGene_exonic_function" text NULL, "AC" text NULL, "AC_hom" text NULL, "aug_all" text NULL, "ExAC_ALL" text NULL, "gnomAD_exome_AF" text NULL, "Kaviar_AF" text NULL, "SIFT_pred_41a" text NULL, "SIFT4G_pred_41a" text NULL, "Polyphen2_HDIV_pred_41a" text NULL, "Polyphen2_HVAR_pred_41a" text NULL, "CADD_phred_41a" text NULL, "CLNSIG" text NULL, "filename" text NULL, "count_hom" integer NULL, "count_het" integer NULL, "count_total" integer NULL, "files_uploaded" integer NULL, "New_allele_frequency" double precision NULL)

I want to delete just the DB container and start with a fresh empty database to apply migrations to, Note that I cannot do docker-compose --build since the app service installs a lot of dependencies and takes a long time and I have pretty bad wifi. Is there a way to delete just the database container?

docker-compose down removes the images as mentioned in the comments

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM