I am new to docker and developing a project using docker compose. From the documentation I have learned that I should be using data only containers to keep data persistant but I am unable to do so using docker-compose.
Whenever I do docker-compose down
it removes the the data from db but by doing docker-compose stop
the data is not removed. May be this is because that I am not creating named data volume and docker-compose down
hardly removes all the containers. So I tried naming the container but it threw me errors.
Please have a look at my yml
file:
version: '2'
services:
data_container:
build: ./data
#volumes:
# - dataVolume:/data
db:
build: ./db
ports:
- "5445:5432"
environment:
- POSTGRES_USER=postgres
- POSTGRES_DB=postgres
# - PGDATA=/var/lib/postgresql/data/pgdata
volumes_from:
# - container:db_bus
- data_container
geoserver:
build: ./geoserver
depends_on:
- db
ports:
- "8004:8080"
volumes:
- ./geoserver/data:/opt/geoserverdata_dir
web:
build: ./web
volumes:
- ./web:/code
ports:
- "8000:8000"
depends_on:
- db
command: python manage.py runserver 0.0.0.0:8000
nginx:
build: ./nginx
ports:
- "83:80"
depends_on:
- web
The Docker file for the data_container is:
FROM stackbrew/busybox:latest
MAINTAINER Tom Offermann <[email protected]>
# Create data directory
RUN mkdir /data
# Create /data volume
VOLUME /data
I tried this but by doing docker-compose down, the data is lost. I tried naming the data_container as you can see the commented line, it threw me this error:
ERROR: Named volume "dataVolume:/data:rw" is used in service "data_container" but no declaration was found in the volumes section.
So right now what I am doing is I created a stand alone data only named container and put that in the volumes_from
value of the db. It worked fine and didn't remove any data even after doing docker-compose down
.
My queries:
What is the best approach to make containers that can store database's data using the
docker-compose
and to use them properly ?My conscious is not agreeing with me on approach that I have opted, the one by creating a stand alone data container. Any thoughts?
volumes: - /data/mydata:/var/lib/postgresql/data/pgdata
which is the preferred method over data container volumes. – LanERROR: Named volume "dataVolume:/data:rw" is used in service "data_container" but no declaration was found in the volumes section.
I am also curious about data portability using this approach. – Roseline