We are using docker volume to store some static files from the docker host's folder. When I restart the container the files which are updated/added in the host directory I can see the changes in docker volume.
However, when I delete a file from host machine. The file is not getting deleted. I have to use docker volume as this is a shareable resource.
Following is my docker compose file
version: '2'
volumes:
test-volume: {}
services:
test:
image: test-volume:test1
volumes:
- test-volume:/var/myapp
test-gateway:
image: test-gateway:latest
ports:
- "8080:80"
volumes_from:
- test
volumes:
- ./test-gateway/conf.d:/etc/nginx/conf.d
environment:
- SITE_ROOT=root
- SITE_ROOT_ROUTE_FROM=/
- SITE_ROOT_ROUTE_DIRECTORY=/var/myapp/static
So, if I remove any file from myapp and restart the docker container the file is not getting deleted from the docker volume.
Is there anything I am missing?
In you compose file you are creating a named volumes test-volume. This volume is references in the test and test-gateway services.
This volume has nothing to do with folders on your docker host. Therefore you cannot delete files on the host and expect this will be reflected in your named volume.
In case you want to map a folder on your docker host your need to use the syntax you used for mapping conf.d file.
volumes:
- ./some_folder_on_host:/some_folder_in_container
Related
everyone.
Making my first steps with Docker and want to build an NGINX server, where all the configuration files, logs, and web data will be placed on volumes.
Here is my docker-compose.yml.
I expected that all the configuration files from the /etc/nginx, including conf.d file, will be copied to my NGINX volume.
I also created WWW/html/ directory with a custom index.html to replace the standard welcome page.
version: '3.7'
services:
nginx:
image: nginx
ports:
- "80:80"
volumes:
# nginx configs
- ./nginx:/etc/nginx
# projects folder
- ./WWW:/var/www
# nginx logs
- ./nginx-logs:/var/log/nginx
restart: always
But somehow this does not work. Where I was work?
When you mount a directory you override (technically hide, you can still access the real folder in the container) the mount destination with your source. It won't fill your dirs. If you want to extract the contents of those directories, temporarely disable the mounts and use command docker cp. More info in the docs
I have a docker-compose.yml
version: '3.3'
services:
ssh:
environment:
- TZ=Etc/UTC
- DEBIAN_FRONTEND=noninterative
build:
context: './'
dockerfile: Dockerfile
ports:
- '172.17.0.2:22:22'
- '443:443'
- '8025:8025'
volumes:
- srv:/srv:rw
restart: always
volumes:
srv:
After I run docker-compose up --build I can ssh to the docker vm and there are files in /srv. 'docker volume ls' shows 2 volumes, srv and dockersetupsrv. They are both in /var/lib/docker/volumes. They both contain _data directories and show creation time stamps that match the docker image creation times but are otherwise empty. Neither one contains any of the files that are in the docker container's /srv directory. How can I share the docker /srv directory with the host?
you should point out more specific for the mapping directory,
for example:
/srv:/usr/srv:rw
after that, when you add content inside your host machine /srv,it is automatically map into /usr/srv
--> make sure that directory exist
you can have a check in this link : https://docs.docker.com/storage/volumes/
Whenever I try to mount the log directory to an existing volume, it ignores that and creates a new volume and maps it to the new volume
In the docker-compose yaml file I have mentioned the volume name and as external, but it still creates a new one.
I am using windows OS
nosqldata:
container_name: 'docker_exec_service_mongodb'
image: mongo:latest
networks:
- app-tier
restart: on-failure:5
ports:
- "30001:27017"
volumes:
- docker_exec_container_vol_mongo_data:/data/db
- docker_exec_container_vol_mongo_log:/var/log/mongodb
Any other way to use the external volume
Github - https://github.com/austinnoronha/docker_exec_container
This is docker compose file looks like
version: '3.3'
services:
portal:
ports:
- '8080:8080'
- '8000:8000'
environment:
- 'revcycle.portal.logger.root=C:/tomcat/logs/'
volumes:
- /src/main/webapp/sampleFiles:/usr/local/tomcat/webapps/portal/sampleFiles:rw
container_name: portal
image: 'portal:latest'
docker-compose up is creating container successfully by when i check the content of the tomcat webapp All the other sibling folder of the sampleFiles are deleted.
Am i missing something with the volumn commands
Same happen when I use Intellji Idea docker plugin Bind mounts in Configuration
It should be like this:
volumes:
- /src/main/webapp/sampleFiles:/usr/local/tomcat/webapps/portal/sampleFiles
as far as i know rw is for cases when you use drivers stuff...
and make sure that /src/main/webapp/sampleFiles is the host folder which have what you need in docker container. Because essentially it will be mapped into docker container. and will replace target folder.
this way siblings for /usr/local/tomcat/webapps/portal/sampleFiles should stay intact. If no, try starting without volumes part and verify that you see siblings.
don't forget to do docker-compose down and docker-compose up -d when you change anything in docker-compose.yaml file
I found that you can use named volumes so two containers can exchange data between them. However, I need to store this names volume in my host computer (the computer which is running the docker images).
So how do I create a voluma that is stored in /media/my_volume that is also shared between containers? I tried to simply binding /media/my_volume to both containers but it ended up in everything being erased when I started the compose again
UPDATE:
version: '3'
services:
transmission:
build: ./rpi-transmission
image: rpi-transmission
ports:
- "9091:9091"
- "51413:51413"
- "51413:51413/udp"
volumes:
- "/home/pi/transmission:/etc/transmission"
- "/media/external:/home/downloads"
- "/home/transmission-watch:/home/transmission-watch"
samba:
build: ./rpi-samba
image: rpi-samba
stdin_open: true
volumes:
- "/media/external:/data/share:ro"
kodi:
build: ./kodi-rpi
image: kodi-rpi
ports:
- "127.0.0.1:8080:8080"
- "127.0.0.1:9777:9777/udp"
devices:
- "/dev/tty0:/dev/tty0"
- "/dev/tty2:/dev/tty2"
- "/dev/fb0:/dev/fb0"
- "/dev/input:/dev/input"
- "/dev/snd:/dev/snd"
- "/dev/vchiq:/dev/vchiq"
volumes:
- "/var/run/dbus:/var/run/dbus"
- "/etc/localtime:/etc/localtime:ro"
- "/etc/timezone:/etc/timezone:ro"
- "/home/pi/kodi-rpi/config:/config/kodi"
- "/home/pi/kodi-rpi/data:/data"
I need to use /media/external on both containers. If I give it a name, I can't mount it to /media/external. If I simply do as it it now, I think samba erases the content of transmission
The content isn't erased from the container though, it is "masked", because the mounted directory is mounted on top of the existing files. The files are still in the container, only not reachable.
However, un-mounting the volume reveals the content that is still in the container (only not accessible, because the volume is mounted over it)
It already has a path on the host inside /var/lib/docker (or whatever directory you has configured as a graph path).
$ docker volume create test
test
$ docker volume inspect -f '{{.Mountpoint}}' test
/var/lib/docker/volumes/test/_data
If you want it to appear on /media/my_volume you can do a bind mount:
mount --bind /var/lib/docker/volumes/test/_data /media/my_volume