Home > Blockchain >  Docker postgres persisting data without volumes (should not persist)
Docker postgres persisting data without volumes (should not persist)

Time:10-22

I am using a docker container to run postgres for testing purposes, it should NOT persist data between different runs.

This is the dockerfile:

FROM postgres:alpine
ENV POSTGRES_PASSWORD=1234
EXPOSE 5432

And this is my compose file:

version: "3.9"
services:
  web:
    build:
      context: ../../.
      dockerfile: ./services/web/Dockerfile
    ports:
      - "3000:3000"
  
  db:
    build: ../db
    ports:
      - "5438:5432"

  graphql:
    build:
      context: ../../.
      dockerfile: ./services/graphql/Dockerfile
    ports: 
      - "4000:4000"
  
  indexer:
    build:
        context: ../../.
        dockerfile: ./services/indexer-ts/Dockerfile
    volumes:
      - ~/.aws/:/root/.aws:ro

However, I find that between sessions all data is being persisted and I have no clue why. This is totally messing my tests and is not expected to happen.

Even after running docker system prune, all data still persists, meaning that the container is probably using a volume somehow

Does anyone know why this is happening and how to not persist the data?

CodePudding user response:

When your stop your docker-compose environment by typing CTRL-C or similar, next time you run docker-compose up it will restart the same container if the configuration hasn't changed. So even absent volumes, any data that was there previously will continue to be there.

To ensure you're starting with fresh containers, always run:

docker-compose down

If you have explicit volumes defined in your configuration, adding -v will also delete those volumes:

docker-compose down -v

(That's not necessary in this situation.)


Unrelated to your question, but why are you building a custom postgres image? You could just set things up in your docker-compose.yaml file:

  db:
    image: postgres:alpine
    environment:
      POSTGRES_PASSWORD: "${POSTGRES_PASSWORD}"
    ports:
      - "5438:5432"

(And then set POSTGRES_PASSWORD in your .env file.)

CodePudding user response:

You are correct, it is using a volume.

You can use the -v switch to clean up:

docker-compose rm -v db
  • Related