docker volume won't persist - docker

this is my docker-compose file
version: '3'
services:
mongo:
image: mongo:latest
ports:
- 27017:27017
volumes:
- dbdata:/data/db
volumes:
dbdata:
When I start and stop the mongo container I lose all data.
What am I missing out on ?

Try running docker volume create dbdata in order to create a persistent volume, redeploy your stack and it should work after that. I.e. the first reboot will cause a data wipe, but after that it should persist.
I hope this helps you

Don't know why it wasn't working but it is now solved
After running my docker-compose file I see the mydata volume when I run docker volume ls.
That volume persists through container destruction.

Related

docker-compose create a directory instead of mounting the file

I have a very simple docker-compose.yml file where I use nginx and mounting a file as a volume.
But everytime I run the application, it is creating a directory .htpasswd without really mounting the .htpasswd file where I locally.
This is the docker-compose.yml.
version: '3'
services:
reverse:
container_name: reverse
hostname: reverse
restart: unless-stopped
image: nginx
ports:
- 80:80
- 443:443
volumes:
- ./nginx/.htpasswd:/etc/nginx/conf.d/.htpasswd
Can someone help me fix this?
by default if binded to a none existent path, docker will create a folder, the solution would be in your case to create the path before running your docker-compose
How are you running Docker? here's an answer ...
For Mac with Minikube/Hyperkit docker and Docker Compose
Since I'm not using Docker Desktop any longer, I've experienced numerous issues similar to "docker in docker (dind)" paradigm with minikube...
mount minikube
use absolute path
e.g., easiest way was to mount the exact home path...
minikube mount $HOME:/Users/<you>
... keeps running...
docker-compose.yaml
volumes:
- /Users/<you>/path/to/file.yaml:/somedir/file.yaml

Undeletable or persistent docker-compose volumes or data storage

is there possible to somehow create persistent storage for containers, created with docker-compose and don't remove them even when running docker-compose down -v so they will be automaticly attached to their containers after again starting the docker-compose up -d ?
What I usually do is to use an external volume, something like:
$ docker volume create nodemodules
docker-compose.yml
version: '3.7'
services:
frontend:
image: node:11
volumes:
- nodemodules:/app/node_modules
volumes:
nodemodules:
external: true
Refer the docs for more info: https://docs.docker.com/compose/compose-file/#external

I do not understand the syntax of docker-compose: "volumes" and "services"

I don't understand the syntax of the docker-compose-file.
First of all
version: '3'
services:
bla:
command: /bin/bash
stdin_open: true
#tty: true
container_name: docker-gulp-template
#restart: always
build: .
ports:
- '80:3000'
volumes:
- ".:/usr/src/html/bla-source"
volumes:
volumes-xyz:
If I execute it with
docker-compose up
It does create a container with the name
docker-gulp-template_bla
But that sounds illogical to me, though. Shouldn't the container be called this way:
bla_docker-gulp-template ?
Why was it solved like this? Does any of you have an example?
And another point:
volumes:
- ".:/usr/src/html/bla-source"
volumes:
volumes-xyz:
Why do I need the second volumes command and how does docker know that the first volume path belongs to the other volumes name?
Thanks in advance
You don't need the volume section.
A volume can be a named volume, created under the top level volumes section, like
volumes:
volumes-xyz:
and mounted under a service with
volumes:
- "volumes-xyz:/usr/src/html/bla-source"
Named volumes are managed by docker (/var/lib/docker/volumes/ on Linux).
Volume can also be anonymous by
volumes:
- "/usr/src/html/bla-source"
- ".:/usr/src/html/bla-source", on the other hand, creates a "bind mount". It's very similar to volume but you can choose its path to create a two-way mapping between your container and the host.

Docker stack deploy rolling updates volume issue

I'm running docker for a production PHP-FPM/Nginx application, I want to use docker-stack.yml and deploy to a swarm cluster. Here's my file:
version: "3"
services:
app:
image: <MYREGISTRY>/app
volumes:
- app-data:/var/www/app
deploy:
mode: global
php:
image: <MYREGISTRY>/php
volumes:
- app-data:/var/www/app
deploy:
replicas: 2
nginx:
image: <MYREGISTRY>/nginx
depends_on:
- php
volumes:
- app-data:/var/www/app
deploy:
replicas: 2
ports:
- "80:80"
volumes:
app-data:
My code is in app container with image from my registry.
I want to update my code with docker service update --image <MYREGISTRY>/app:latest but it's not working the code is not changed.
I guess it uses the local volume app-data instead.
Is it normal that the new container data doesn't override volume data?
Yes, this is the expected behavior. Named volumes are only initialized to the image contents when they are empty (the default state when first created). Updating the volume any time after that point would risk data loss from overwriting or deleting volume data that you explicitly asked to be preserved.
If you need the files to be updated with every new image, then perhaps they shouldn't be in a volume? If you do need these inside a volume, then you may need to create a procedure to update the volumes from the image, e.g. if this were a docker run, you could do:
docker run -v app-data:/target --rm <your_registry>/app cp -a /var/www/app/. /target/.
Otherwise, you can delete the volume, or simply remove all files from the volume, and restart your stack to populate it again.
I was having the same issue that I have app and nginx containers sharing the same volume. My current solution having a deploy script which runs
docker service update --mount-add mount service
for app and nginx after docker stack deploy. It will force to update the volume for app and nginx containers.

docker-compose does not see volumes changes in docker-compose.yml

I'm working with docker-compose.
My docker-compose.yml looks like
redis:
image: redis
expose:
- "6379"
volumes:
- ./redis:/data
nerdzcrush:
image: nerdzeu/docker-nerdzcrush
ports:
- "8181:81"
links:
- redis
volumes:
- ./mediacrush:/home/mediacrush
When I run docker-compose up everything works fine.
After that, I needed to change the mount path.
I stopped the containers with docker-compose stop, I changed my docker-compose.yml in this way:
redis:
image: redis
expose:
- "6379"
volumes:
- ./nerdzcrush/redis:/data
nerdzcrush:
image: nerdzeu/docker-nerdzcrush
ports:
- "8181:81"
links:
- redis
volumes:
- ./nerdzcrush/mediacrush:/home/mediacrush
And I removed the old directories with
sudo rm -rf ./mediacrush ./redis
After that, I started the containers wiht docker-compose up -d
I expect that the containers start to work with the new path, but I see that the old path are used. Thus I have again ./mediacrush and ./redis in my folder.
That's something I understood wrongly about docker-compose or that's an issue with docker-compose?
I'm using docker-compose version: 1.5.0dev
Thank you
It's only supposed to preserve volumes if they are container data volumes (not host volumes like in your case).
I would try running docker-compose rm to remove the containers (after stopping them). After that the up should use the correct path.

Resources