Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why I don't lose postgresql data when rebuild docker image?

version: '3'

services:
  db:
    image: postgres
  web:
    build: .
    command: python3 manage.py runserver 0.0.0.0:8000
    volumes:
      - .:/code
    ports:
      - "8000:8000"
    depends_on:
      - db

Why I don't lose data when running docker-compose build --force-em --no-cache. If this is normal, why do we need to create volume for data folder ?

like image 839
user2080105 Avatar asked Jan 03 '18 12:01

user2080105


People also ask

Do I lose my data when the docker container exits?

Do I lose my data when the container exits? 🔗 Not at all! Any data that your application writes to disk gets preserved in its container until you explicitly delete the container.

Does docker container persist data?

Docker also supports containers storing files in-memory on the host machine. Such files are not persisted. If you're running Docker on Linux, tmpfs mount is used to store files in the host's system memory. If you're running Docker on Windows, named pipe is used to store files in the host's system memory.

Where is Postgres docker data stored?

User Defined Volume To circumvent this issue, we can use the information we gathered earlier that showed us that the volume is mounted at /var/lib/postgresql/data. Inside the container, this directory is where Postgres stores all the relevant tables and databases.


1 Answers

When running the command docker-compose build --force-em --no-cache, this will only build the web Docker image from the Dockerfile which in your case is in the same directory.

This command will not stop the containers that you have previously started using this compose file, thus you want lose any data when running this command.

However, as soon as you remove the containers using docker-compose down or when containers are stopped docker-compose rm, you won't find the postgres data when you restart the container.

If you want to persist the data, and make the container pick it up when it is recreated, you need to give the postgres data volume a name as such.

version: '3'

services:
  db:
    image: postgres
    volumes:
      - pgdata:/var/lib/postgresql/data
  web:
    build: .
    command: python3 manage.py runserver 0.0.0.0:8000
    volumes:
      - .:/code
    ports:
      - "8000:8000"
    depends_on:
      - db

Now the postgres data won't be lost when the containers are recreated.

like image 79
yamenk Avatar answered Sep 27 '22 23:09

yamenk