A container's filesystem is thrown away when it is removed. Keep data with named volumes, mount your source code with a bind mount for live development, and clean volumes up.
A container's filesystem is temporary — delete the container and everything written inside it is gone. That is fine for the app, fatal for a database. The fix is to store data outside the container in a managed location Docker calls a volume.
Run a database with no volume, then write some data...
docker run -d --name db -e POSTGRES_PASSWORD=secret postgres:16...remove the container and the database files vanish with it
docker rm -f dbA named volume is storage Docker manages and keeps separate from any container. Mount it with -v name:/path-in-container. Now the database writes into the volume, so you can delete and recreate the container as often as you like and the data is still there.
Create the volume once
docker volume create pgdataMount it into the database's data directory
docker run -d --name db \
-e POSTGRES_PASSWORD=secret \
-v pgdata:/var/lib/postgresql/data \
postgres:16Remove the container — re-run the command above and the data is still there
docker rm -f dbA bind mount maps a folder on YOUR machine straight into the container, so the container sees your code live. Edit a file on your laptop and it changes inside the container instantly — pair it with a dev server that reloads and you get hot-reload in Docker. The syntax is -v host-path:container-path. The second -v keeps the container's node_modules from being hidden by your host folder.
Mount the current folder into /app so edits show up live
docker run -p 3000:3000 \
-v "$(pwd)":/app \
-v /app/node_modules \
myapp npm run devVolumes outlive containers on purpose, so they pile up. List them, inspect where one lives, remove the ones you no longer need, and prune everything unused in one go to reclaim disk space.
List volumes
docker volume lsShow where a volume lives and who uses it
docker volume inspect pgdataRemove one (no container may be using it)
docker volume rm pgdataRemove ALL unused volumes to reclaim disk
docker volume prune