Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I mount same volume to multiple docker containers

I need to share the application logs to mounted volume. I want to mount same volume to all docker containers to keep the logs. is it possible? will it effect the application running on each container?

docker run -d  --name C1 -v /home/ubuntu/logs:/usr/local/apache/htdocs/ httpd docker run -d  --name C2 -v /home/ubuntu/logs:/etc/app/logs my-docker-image:latest docker run -d  --name C3 -v /home/ubuntu/logs:/usr/logs TestImage:latest 
like image 237
Nijisha Kg Avatar asked Mar 17 '17 10:03

Nijisha Kg


People also ask

Can volume be shared across multiple containers?

For multiple containers writing to the same volume, you must individually design the applications running in those containers to handle writing to shared data stores to avoid data corruption. After that, exit the container and get back to the host environment.

Are Docker volumes persistent?

Volumes are the best way to persist data in Docker. Bind mounts may be stored anywhere on the host system. They may even be important system files or directories. Non-Docker processes on the Docker host or a Docker container can modify them at any time.


2 Answers

Yes you can add same location as a volume to many docker containers.

Additionally you can use --volumes-from to mount your log directory in one container not actually running any application and then use the volumes from this container in your other containers without having to repeat the paths everywhere.

Worth a read Docker volumes

like image 119
user3012759 Avatar answered Sep 17 '22 15:09

user3012759


I'm mounting the same directory to several containers but for read purpose, not write (if your logs are written within the containers. I think that if they are writing to different files it Should work..

like image 22
ItayB Avatar answered Sep 16 '22 15:09

ItayB