问题
I am trying to build a Apache WebDAV private cloud server.
All my files will be in /usr/John/ directory and I can mount this location for the container to use. The problem is that I will also add new files via this server, but whatever I try they stay only within the container. Is there a way to reflect the same new files also in the host directory (/usr/John/)?
I could do a recurrent job to go into this container and CP all files to the host directory but it's not a very elegant solution.
回答1:
Use docker volumes. All your persistent data generated by the container will be saved to var/lib/docker/volumes
. If you are using docker-compise, you can do something like below. Here I have 3 volumes and I am naming it tomcat-data
, host-upload
, tomcat-webapps
. Here all the data I put in tomcat-data
, /home/foo/upload
and /usr/local/tomcat/webapps
will be stored on the host machine inside var/lib/docker/volumes
. So even if the container goes down and you bring up another container all the data in these directories will be preserved. Let me know if you have any questions.
You can also bind mount host directories directly onto docker container. Let me know if you have any questions
version: '3.2'
services:
tomcat:
image: "ciena/tomcat"
volumes:
- tomcat-data:/tomcat-data
- host-upload:/home/foo/upload
- tomcat-webapps:/usr/local/tomcat/webapps
ports:
- target: 8080
published: 8080
protocol: tcp
mode: ingress
- target: 2222
published: 22
protocol: tcp
mode: ingress
environment:
- JAVA_MIN_HEAP=256m
- JAVA_MAX_HEAP=512m
deploy:
placement:
constraints:
- node.role == manager
replicas: 1
resources:
limits:
memory: 1024M
cpus: '0.5'
reservations:
memory: 512M
cpus: '0.001'
restart_policy:
condition: any
delay: 5s
max_attempts: 3
window: 60s
volumes:
tomcat-data:
host-upload:
tomcat-webapps:
networks:
default:
来源:https://stackoverflow.com/questions/53766998/persistent-docker-volume