Database is not recreated in docker after removing the volume - docker-compose

After I delete the volume of the db service by running docker volume rm <volume_name>, the database is not recreated when I am rebuilding the container by docker-compose up -d --build. What is the reason ?
My docker-compose file looks like this:
version: "2"
services:
db:
image: 'postgres:latest'
ports:
- "5432:5432"
env_file:
- .env
volumes:
- db:/var/lib/postgresql/data:rw
restart: "always"
volumes:
db: {}

Related

How to attach persistent volume in docker-compose file for mongodb?

I have a docker-compose file that will bring up mongo and mongo-express containers in the same network "mynet".
I have created a network by:
docker network create mynet
I have created a volume named "demo-vol" externally by docker command.
docker volume create demo-vol
Inside the container, I have created a sample mongo collection.
When I do docker-compose up I'm able to see the container running but I'm not able to find the mongo data in that specified volume.
Below is my docker-compose.yaml file
version: '3'
services:
mongo:
image: mongo
container_name: mymongo
environment:
MONGO_INITDB_ROOT_USERNAME: root
MONGO_INITDB_ROOT_PASSWORD: example
volumes:
- "/demo-vol:/data/db"
networks:
- mynet
ports:
- 27017:27017
mongoexpress:
image: mongo-express
container_name: mymongoexpress
ports:
- 8081:8081
volumes:
- "/demo-vol:/data/db"
environment:
ME_CONFIG_MONGODB_ADMINUSERNAME: root
ME_CONFIG_MONGODB_ADMINPASSWORD: example
depends_on:
- mongo
networks:
- mynet
volumes:
demo-vol:
external: true
networks:
mynet:
external: true
What I need is:
Even after deleting the container, I want my data to be persistent.
How to do that and please explain. Where i'm going wrong?
Note:I'm a beginner to Docker concepts.
Thanks in advance.
You can to use local driver for volume
volumes:
demo-vol:
driver: local
and try to remove slash
volumes:
- demo-vol:/data/db

docker: create a mongodb volume that is still saved after docker-compose down?

I have the following docker-compose.yml file:
version: "3"
services:
pokerstats:
image: pokerstats
container_name: pokerstats
ports:
- 8080:8080
depends_on:
- db
db:
image: mongo
container_name: mongo
volumes:
- ./database:/data
ports:
- "27018:27017"
environment:
MONGO_INITDB_ROOT_USERNAME: admin
MONGO_INITDB_ROOT_PASSWORD: admin
MONGO_INITDB_DATABASE: pokerstats
My issue is that when I run docker-compose down all the data within my mongo database is lost.
How can I create a mongo volume that persists even when the mongo container goes down?
Per the image documentation the database volume needs to be /data/db. This is also seen in the Dockerfile volume.
Since the volume is defined in the Dockerfile, if you do not create a volume at that directory, even if you created a volume in the parent like /data, docker will create an anonymous volume at /data/db which will show up as a long guid volume name in docker volume ls. Depending on how the container is run, those may be left behind.
Therefore the fix is to adjust your volume mount to that path:
version: "3"
services:
pokerstats:
image: pokerstats
container_name: pokerstats
ports:
- 8080:8080
depends_on:
- db
db:
image: mongo
container_name: mongo
volumes:
- ./database:/data/db
ports:
- "27018:27017"
environment:
MONGO_INITDB_ROOT_USERNAME: admin
MONGO_INITDB_ROOT_PASSWORD: admin
MONGO_INITDB_DATABASE: pokerstats
Note that unless you need direct access to this data on the host, I'd recommend using a named volume instead. It includes initialization steps that helps with permission issues you may encounter with host volumes, particularly when running directly on a Linux host.
To use a named volume, that would look like:
version: "3"
services:
pokerstats:
image: pokerstats
container_name: pokerstats
ports:
- 8080:8080
depends_on:
- db
db:
image: mongo
container_name: mongo
volumes:
- dbdata:/data/db
ports:
- "27018:27017"
environment:
MONGO_INITDB_ROOT_USERNAME: admin
MONGO_INITDB_ROOT_PASSWORD: admin
MONGO_INITDB_DATABASE: pokerstats
volumes:
dbdata:
Try to change volumes for MongoDb container:
volumes:
- "./database:/data/db"

Save Postgres Data to Directory in Docker Named Volume

Problem
I have an application with postgres. I want to be able to back up the initial database data so that I don't have to re enter it each deployment. However, despite having a named volume set up in my compose file.
What I'm not sure of is how to have postgres save its data into the directory associated with the volume. I'm also not sure exactly how to associate a directory with the named volume. What I want is for the docker host server to be able to see the postgress data in the named volume's associated directory.
Could someone please provide an explanation/some examples of how to handle this? Right now even though the volume is associated with the docker service in the compose file, it doesn't write any data to the database_volume/ directory. This is what I would like to address.
Code
Here's my Dockerfile:
FROM python:3.6
ARG requirements=requirements/production.txt
ENV DJANGO_SETTINGS_MODULE=sasite.settings.production_test
WORKDIR /app
COPY manage.py /app/
COPY requirements/ /app/requirements/
RUN pip install -r $requirements
COPY config config
COPY sasite sasite
COPY templates templates
COPY logs logs
ADD /scripts/docker-entrypoint.sh /docker-entrypoint.sh
RUN chmod a+x /docker-entrypoint.sh
EXPOSE 8001
ENTRYPOINT ["/docker-entrypoint.sh"]
CMD ["/usr/local/bin/gunicorn", "--config", "config/gunicorn.conf", "--log-config", "config/logging.conf", "-e", "DJANGO_SETTINGS_MODULE=sasite.settings.production_test", "-w", "4", "-b", "0.0.0.0:8001", "sasite.wsgi:application"]
And my docker-compose.yml:
version: "3.2"
services:
app:
restart: always
build:
context: .
dockerfile: Dockerfile.prodtest
args:
requirements: requirements/production.txt
container_name: dj01
environment:
- DJANGO_SETTINGS_MODULE=sasite.settings.production_test
- PYTHONDONTWRITEBYTECODE=1
volumes:
- ./:/app
- /static:/static
- /media:/media
networks:
- main
depends_on:
- db
db:
restart: always
image: postgres:10.1-alpine
container_name: ps01
environment:
POSTGRES_DB: sasite_db
POSTGRES_USER: pguser
POSTGRES_PASSWORD: pguser123
ports:
- "5432:5432"
volumes:
- database_volume:/var/lib/postgresql/data
networks:
- main
nginx:
restart: always
image: nginx
container_name: ng01
volumes:
- ./config/nginx-prodtest.conf:/etc/nginx/conf.d/default.conf:ro
- ./static:/usr/share/nginx/sasite/static
- ./media:/usr/share/nginx/sasite/media
ports:
- "80:80"
- "443:443"
networks:
- main
depends_on:
- app
networks:
main:
volumes:
database_volume:
driver_opts:
type: none
device: ./database_volume
o: bind

Docker compose fiware WireCloud data persistance not loaded from volume

I am using a docker container for my FiWare WireCloud. It is working properly but when I stop my container with docker compose down and restart it with docker compose up all my data are erased even if I specified a volume for the postgresql database and I have the following error:
ERROR: relation "wirecloud_workspace" does not exist at character 370
If I want to make it work again, I have to recreate the whole database from scratch (initdb & createsuperuser)
What I would like to do is to be able to save my wirecloud data inside a volume and be able te backup it and reload it. Here is my current docker-compose.yml file in version 3:
version: '3.3'
services:
iot-mongo:
image: mongo:3.2
ports:
- "27017:27017"
volumes:
- ./data/mongo:/data/db
orion:
image: fiware/orion:1.9.0
links:
- iot-mongo
ports:
- "1026:1026"
command: -dbhost iot-mongo
nginx:
restart: always
image: nginx:1.13
ports:
- "80:80"
- "443:443"
volumes:
- ./nginx/nginx.conf:/etc/nginx/nginx.conf:ro
- ./nginx/sites-available:/etc/nginx/sites-available
- ./letsencrypt/well-known:/www/letsencrypt
- /etc/letsencrypt/:/etc/letsencrypt/
- wirecloudwww:/var/www/static
- wirecloudinstance:/opt/wirecloud_instance
links:
- wirecloud:wirecloud
- orion:orion
postgres:
restart: always
image: postgres:latest
ports:
- "5432:5432"
volumes:
- postgresdata:/var/lib/postgresql
environment:
POSTGRES_USER: postgres
POSTGRES_PASSWORD:
POSTGRES_DB: postgres
PGDATA: /tmp
wirecloud:
restart: always
image: fiware/wirecloud:1.0-composable
links:
- postgres:postgres
volumes:
- wirecloudwww:/var/www/static
- wirecloudinstance:/opt/wirecloud_instance
volumes:
wirecloudwww: {}
wirecloudinstance: {}
postgresdata: {}
I also tried with docker-compose v1 like they show in the documentation but the result is the same.
The problem is the definition of the postgres volume and the PGDATA environment variable. The PGDATA environment is telling PostgreSQL to store data in /tmp, so it is not going to store data inside de volume (you can create a volume on /tmp, but this seems a bit strange). If you remove the PGDATA environment variable, postgres will store data into /var/lib/postgresql/data. Using this definition for the postgres service should do the trick:
postgres:
restart: always
image: postgres:latest
ports:
- "5432:5432"
volumes:
- postgresdata:/var/lib/postgresql/data
environment:
POSTGRES_USER: postgres
POSTGRES_PASSWORD:
POSTGRES_DB: postgres

docker share OS volume between services

This is an extract from Volume configuration reference, in docker docs.
version: "3"
services:
db:
image: db
volumes:
- data-volume:/var/lib/db
backup:
image: backup-service
volumes:
- data-volume:/var/lib/backup/data
volumes:
data-volume:
Can I have something similar mapped to a custom directory, instead of /var/lib/docker/volumes/?
Something like this:
version: "3"
services:
db:
image: db
volumes:
- data-volume:/var/lib/db
backup:
image: backup-service
volumes:
- data-volume:/var/lib/backup/data
volumes:
data-volume: /home/user/db
It is possible. See https://github.com/docker/docker/issues/19990#issuecomment-248955005
You will have to create the volume with docker cli.