====== Docker - Containers - Share data between a docker container and the Host ====== Docker containers are ephemeral, running just as long as it takes for the command issued in a container to complete. By default, any data created inside the container is only available from within the container and only while the container is running. Docker volumes can be used to share files between a host system and the Docker container. For example, let's say 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 nginx Docker image will log to the /var/log/nginx directory inside the Docker Nginx container. Normally it's not reachable from the host filesystem. ---- ===== Bindmounting a Volume ===== Create a directory called nginxlogs in the home directory of the localhost user and bindmount it to /var/log/nginx in the container: docker run --name=nginx -d -v ~/nginxlogs:/var/log/nginx -p 5000:80 nginx * **--name=nginx**: names the container so we can refer to it more easily. * **-d**: detaches the process and runs in the background. Otherwise, this would display an empty Nginx prompt and the terminal would be unusable until Nginx was killed. * **-v ~/nginxlogs:/var/log/nginx**: Sets up a bindmount volume that links the /var/log/nginx directory from inside the Nginx container to the ~/nginxlogs directory 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:80** sets 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. * **nginx**: specifies that the container should be built from the Nginx image, which issues the command nginx -g "daemon off to start Nginx. **NOTE**: The **-v** flag is very flexible. It can bindmount or name a volume with just a slight adjustment in syntax. If the first argument begins with a / or ~/ you're creating a bindmount. Remove that, and you're naming the volume. * **-v /path:/path/in/container** mounts the host directory, /path at the /path/in/container * **-v path:/path/in/container** creates a volume named path with no relationship to the host. ---- ===== Accessing Data on the Host ===== We 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://123.123.123.123:5000. You should see the standard Nginx start page. If you look in the ~/nginxlogs directory on the host, you'll see the access.log created by the container's Nginx which will show our request: cat ~/nginxlogs/access.log Displays something like: 123.123.123.123 - - [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" "-" If you make any changes to the ~/nginxlogs folder, you'll be able to see them from inside the Docker container in real-time as well.