Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

expose files from docker container to host

I have a docker container that holds a django app. The static files are produced and copied to a static folder.
container folder hierarchy:

- var
    - django
        - app
        - static

before i build the docker image, i run ./manage.py collectstatic so the static files are in the /var/django/static folder. To expose the app and serve the static files, i have on the host an nginx. The problem is that if i do a volume between the static folder and a designated folder on the host, when i run the docker container, the /var/django/static folder in the container gets deleted (well, not deleted but mounted). Is there any way to overcome this? as in set the volume but tell docker to take the current files as well?

like image 852
Mr T. Avatar asked Aug 14 '16 09:08

Mr T.


People also ask

Can Docker container access files on host?

Docker volumes are convenient for sharing files from the host and keeping larger files out of image layers. They can also be significantly faster for filesystem access than the container filesystem, as some storage backends impose significant overheads for certain workloads.

How do I transfer files from container to host?

Obtain the name or id of the Docker container. Issue the docker cp command and reference the container name or id. The first parameter of the docker copy command is the path to the file inside the container. The second parameter of the docker copy command is the location to save the file on the host.

How do you expose a Docker container to a host?

You can expose a port through your Dockerfile or use --expose and then publish it with the -P flag. This will bind the exposed port to your Docker host on a random port (verified by running docker container ls ). You can expose a port through your Dockerfile or use --expose and then publish it with the -p 80:80 flag.


2 Answers

Volumes are treated as mounts in Docker, which means the host directory will always be mounted over the container's directory. In other words, what you're trying to do isn't currently possible with Docker volumes.

See this Github issue for a discussion on this subject: https://github.com/docker/docker/issues/4361

One possible work-around would be to have a docker volume to an empty directory in your container, and then in your Docker RUN command (or start-up script), copy the static contents into that empty directory that is mounted as a volume.

like image 81
C. Reed Avatar answered Oct 19 '22 21:10

C. Reed


Reading from: Dockers Volume Page

Volumes have several advantages over bind mounts:

New volumes can have their content pre-populated by a container.


Similar example using docker-compose

Using nginx's default webpage folder as the example:

$ docker volume create xmpl
$ docker run -v xmpl:/usr/share/nginx/html nginx

Will yield all the files on the host system via:

$ docker inspect xmpl
...
"Mountpoint": "/var/lib/docker/volumes/xmpl/_data"

And you can then view the files on the host:

# ls /var/lib/docker/volumes/xmpl/_data                               
  50x.html  index.html

And finally to use it from /var/nginx/static:

# mkdir -p /var/nginx
# ln -s /var/lib/docker/volumes/xmpl/_data /var/nginx/static
# ls /var/nginx/static
  50x.html  index.html
like image 38
Tyhal Avatar answered Oct 19 '22 22:10

Tyhal