I have following docker-compose.yml
version: '2'
services:
web:
image: user-service-web:${WEB_IMAGE_VER}
depends_on:
- db
environment:
NODE_ENV: ${NODE_ENV}
networks:
user-service:
aliases:
- user-service-web
ports:
- "80:3000"
restart: always
db:
image: user-service-mongodb:latest
networks:
viewster-user-service:
aliases:
- user-service-mongodb
ports:
- "27017:27017"
restart: always
volumes:
- mongodb:/data/
networks:
user-service:
driver: bridge
volumes:
mongodb:
When I am running docker-compose up it creates specified volume mongodb and starts all required containers. But what confuses me is that volume folder has 2 empty folders: configdb and db, but no data files inside. When I remove db container it expectedly leads to data loss.
I've tried to connect to db container and create file manually both in /data folder and /data/db folder, and only file created in /data folder appeared in host volume folder. It seems like docker can just create files or folders in the folder you've specified in volume settings, but it can't create files in child folders. I can be wrong. It would be tricky to create volume for every folder I need to make persistent. Is there any way to configure volume properly for my case?
Environment: Description: Ubuntu 14.04.3 LTS Release: 14.04 Codename: trusty
Docker version: Client: Version: 1.11.2 API version: 1.23 Go version: go1.5.4 Git commit: b9f10c9 Built: Wed Jun 1 21:47:50 2016 OS/Arch: linux/amd64
Server: Version: 1.11.2 API version: 1.23 Go version: go1.5.4 Git commit: b9f10c9 Built: Wed Jun 1 21:47:50 2016 OS/Arch: linux/amd64
Docker Compose version docker-compose version 1.7.1, build 0a9ab35 docker-py version: 1.8.1 CPython version: 2.7.9 OpenSSL version: OpenSSL 1.0.1e 11 Feb 2013
The mongodb you created is a named volume, not a host volume, so it has no connection to the host files. The named volume will live within the docker internal directories. When use an empty volume with a container, it will receive a copy of the image's volume contents at that location. It will also be removed if you run a docker-compose down -v.
If you want a host volume, you need to change to this line under your db service:
volumes:
- ./data:/data
That will use/create a data folder inside of the folder where you run the docker-compose up command that is a host volume mounted at /data in your container.
Update:
As evidence that named volumes persist data, here's a simple example:
bash$ cat docker-compose.yml
version: '2'
volumes:
testvol:
driver: local
services:
testapp:
image: busybox
entrypoint: "top"
volumes:
- testvol:/data
bash$ ../bin/docker-compose up -d
Creating network "test_default" with the default driver
Creating volume "test_testvol" with local driver
Creating test_testapp_1
bash$ ../bin/docker-compose exec testapp /bin/sh
/ # find /data
/data
/ # mkdir /data/sub
/ # touch /data/a
/ # touch /data/sub/b
/ # find /data
/data
/data/sub
/data/sub/b
/data/a
/ # exit
bash$ ../bin/docker-compose down
Stopping test_testapp_1 ... done
Removing test_testapp_1 ... done
Removing network test_default
bash$ ../bin/docker-compose up -d
Creating network "test_default" with the default driver
Creating test_testapp_1
bash$ ../bin/docker-compose exec testapp /bin/sh
/ # find /data
/data
/data/sub
/data/sub/b
/data/a
/ # exit
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With