I have a docker-compose file that spins up several services. I just got an error stating I ran out of disk space so I typed "docker system df" and saw I have 21 volumes. If I have 3 docker containers with each having a volume attached to them, why its showing me a total count of 21 volumes?
I am using AWS EC2. This is my compose file, is there anything wrong with the way I have the volumes set? The postgres data got persisted when I shutdown and restart, I am just confused aboutthe volume size and the message that I can not rebuild due to no space on a T2 Large instance.
version: "3"
services:
nftapi:
env_file:
- .env
build:
context: .
ports:
- '443:5000'
depends_on:
- postgres
volumes:
- .:/app
- /app/node_modules
networks:
- postgres
postgres:
container_name: postgres
image: postgres:latest
ports:
- "5432:5432"
volumes:
- /data/postgres:/data/postgres
env_file:
- docker.env
networks:
- postgres
pgadmin:
links:
- postgres:postgres
container_name: pgadmin
image: dpage/pgadmin4
ports:
- "8080:80"
volumes:
- /data/pgadmin:/root/.pgadmin
env_file:
- docker.env
networks:
- postgres
networks:
postgres:
driver: bridge
2
Answers
if your main containers are up and running run
and it should remove any volumes that are detached or unused by any container
i make it a habit to run periodically on my aws instance
A Docker image’s Dockerfile can contain a
VOLUME
directive. This is an instruction to Docker that tells it that some container directory contains data that needs to be persisted, and Docker should always ensure a volume of some sort is mounted on that directory.More specifically, the
postgres
image declaresYour Compose setup doesn’t mount anything on that specific directory. Because of this, Docker creates an anonymous volume and mounts it there for you. This isn’t specific to the
postgres
image and other containers in your stack may have similar local data directories. Those anonymous volumes are what you’re seeing in thedocker system df
output (docker volume ls
will also show them).In a later question you also note that Compose has trouble finding these anonymous volumes, and it’s better to not rely on this functionality. Make sure you’re mounting a host directory or named volume for these data directories via Compose
volumes:
.