By default, any data created inside the container is only available from within the container and accessible only while the container is running. However, Docker volumes can be used to share files between a host system and the Docker container.
For example, if you wanted to use the official Docker Nginx image and keep a permanent copy of the Nginx's log files to analyze later. By default the
nginxDocker image will log to the
/var/log/nginxdirectory inside the Docker Nginx container. Normally it's not reachable from the host filesystem.
In this article, we'll walk you through the steps to make data from inside the container accessible on the host machine.
PrerequisitesTo follow this article, you will need an Ubuntu 16.04 server with the following:
- A non-root user with sudo privileges
- Docker installed with the instructions from the article of How to Install and Use Docker on Ubuntu 16.04
Note: Even though the Prerequisites give instructions for installing Docker on Ubuntu 16.04, the
dockercommands for Docker data volumes in this article should work on other operating systems as long as Docker is installed.
Bindmounting a VolumeThe following command will create a directory call
nginxlogsin the home directory of the localhost user and bindmount it to
/var/log/nginxin the container:
Let's take a moment to examine this command in detail:
- docker run --name=nginx -d -v ~/nginxlogs:/var/log/nginx -p 5000:80 nginx
--name=nginxnames the container so we can refer to it more easily.
-ddetaches the process and runs in the background. Otherwise, we would just be watching an empty Nginx prompt and wouldn't be able to use this terminal until we killed Nginx.
-v ~/nginxlogs:/var/log/nginxSets up a bindmount volume that links the
/var/log/nginxdirectory from inside the Nginx container to the
~/nginxlogsdirectory on the host machine. Docker uses a
:to split the host's path from the container path, and the host path always comes first.
-p 5000:80sets up a port forward. The Nginx container is listening on port 80 by default. This flag maps the container's port 80 to port 5000 on the host system.
nginxspecifies that the container should be built from the Nginx image, which issues the command
nginx -g "daemon offto start Nginx.
-vflag is very flexible. It can bindmount or name a volume with just a slight adjustment in syntax. If the first argument begins with a
~/you're creating a bindmount. Remove that, and you're naming the volume.
-v /path:/path/in/containermounts the host directory,
-v path:/path/in/containercreates a volume named
pathwith no relationship to the host.
Accessing Data on the HostWe now have a copy of Nginx running inside a Docker container on our machine, and our host machine's port 5000 maps directly to that copy of Nginx's port 80.
Load the address in a web browser, using the IP address or hostname of your server and the port number,
http://203.0.113.0:5000. You should see:
More interestingly, if we look in the
~/nginxlogsdirectory on the host, we'll see the
access.logcreated by the container's
nginxwhich will show our request:
This should display something like:
- cat ~/nginxlogs/access.log
If you make any changes to the
Output203.0.113.0 - - [11/Nov/2016:00:59:11 +0000] "GET / HTTP/1.1" 200 612 "-" "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/54.0.2840.99 Safari/537.36" "-"
~/nginxlogsfolder, you'll be able to see them from inside the Docker container in real-time as well.
In this guide we demonstrated how to create a Docker data volume to share information between a container and the host file system which is helpful in development environments, where access to logs for debugging are necessary.