Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to mount a directory in a Docker container to the host?

Tags:

Assume that i have an application with this simple Dockerfile:

//... RUN configure.sh --logmyfiles /var/lib/myapp ENTRYPOINT ["starter.sh"] CMD ["run"] EXPOSE 8080 VOLUME ["/var/lib/myapp"] 

And I run a container from that:

sudo docker run -d --name myapp -p 8080:8080 myapp:latest 

So it works properly and stores some logs in /var/lib/myapp of docker container.

My question

I need these log files to automatically saved in host too, So how can i mount the /var/lib/myapp from the container to the /var/lib/myapp in host server (without removing current container) ?

Edit

I also see Docker - Mount Directory From Container to Host, but it doesn't solve my problem i need a way to backup my files from docker to host.

like image 707
Saeed Masoumi Avatar asked Mar 27 '16 10:03

Saeed Masoumi


People also ask

What is mounting in Docker?

With Bind mounts Docker mounts the given source directory into a location inside the container. (The original directory / file in the read-only layer inside the union file system will simply be overridden).


2 Answers

First, a little information about Docker volumes. Volume mounts occur only at container creation time. That means you cannot change volume mounts after you've started the container. Also, volume mounts are one-way only: From the host to the container, and not vice-versa. When you specify a host directory mounted as a volume in your container (for example something like: docker run -d --name="foo" -v "/path/on/host:/path/on/container" ubuntu), it is a "regular ole" linux mount --bind, which means that the host directory will temporarily "override" the container directory. Nothing is actually deleted or overwritten on the destination directory, but because of the nature of containers, that effectively means it will be overridden for the lifetime of the container.

So, you're left with two options (maybe three). You could mount a host directory into your container and then copy those files in your startup script (or if you bring cron into your container, you could use a cron to periodically copy those files to that host directory volume mount).

You could also use docker cp to move files from your container to your host. Now that is kinda hacky and definitely not something you should use in your infrastructure automation. But it does work very well for that exact purpose. One-off or debugging is a great situation for that.

You could also possibly set up a network transfer, but that's pretty involved for what you're doing. However, if you want to do this regularly for your log files (or whatever), you could look into using something like rsyslog to move those files off your container.

like image 109
L0j1k Avatar answered Oct 16 '22 08:10

L0j1k


So how can i mount the /var/lib/myapp from the container to the /var/lib/myapp in host server

That is the opposite: you can mount an host folder to your container on docker run.

(without removing current container)

I don't think so.
Right now, you can check docker inspect <containername> and see if you see your log in the /var/lib/docker/volumes/... associated to the volume from your container.

Or you can redirect the result of docker logs <containername> to an host file.
For more example, see this gist.

The alternative would be to mount a host directory as the log folder and then access the log files directly on the host.

me@host~$ docker run -d -p 80:80 -v <sites-enabled-dir>:/etc/nginx/sites-enabled -v <certs-dir>:/etc/nginx/certs -v <log-dir>:/var/log/nginx dockerfile/nginx  me@host~$ ls <log-dir>  

(again, that apply to a container that you start, not an existing running one)

like image 24
VonC Avatar answered Oct 16 '22 09:10

VonC