Generally, Docker containers only run for a certain period of time – the time it takes to run the command. The data that is available inside the container can only be accessed from within the container during the runtime.
Docker volumes can be used for accessing the files easily and storing them for a longer period. For example, if you have an Nginx web server and you want to store the logs for audits, you can use a Docker volume to store the logs from the container in your host machine.
In this tutorial, you will learn how to share data between the inside of the Docker container and the host machine.
For this tutorial, you will need the following prerequisites:
- Ubuntu 18.04 Server: You can easily create a new CloudSigma server with Ubuntu 18.04 image, by following our helpful tutorial.
- Docker – You can install Docker using our tutorial on how to set up Docker on Ubuntu and get acquainted with it.
Step 1 – Bind Mount a Volume
You can create a directory named
logs in your current user’s home directory and bind-mount it to
/var/log/nginx in the Nginx container using the following command:
docker run --name=nginx -d -v ~/logs:/var/log/nginx -p 5000:80 nginx
Here is more detailed information about this command:
--name=nginxgives a name to the container for easy reference.
-dargument detaches the process and runs it in the background.
-v hostPath:containerPathmaps the host path and the container path to bind the mount.
-p hostPort:containerPortargument maps the container’s port to the host port.
nginxat the end of the line is the name of the image that should be used to spawn up the container.
Step 2 – Accessing Data on the Host
Now, you have an Nginx container running on your server, and port 5000 of your server maps to port 80 of the Nginx. You can open the port in the web browser with the following URL:
In the ~/logs directory, you will see access.log and error.log files. You can see the logs by using the cat command:
xx.xxx.xxx.xxx - - [17/Apr/2021:19:25:05 +0000] "GET / HTTP/1.1" 200 612 "-" "Mozilla/5.0 (Macintosh; Intel Mac OS X 11_2_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/xx.xxx.xxx.xxx Safari/537.36" "-"
xx.xxx.xxx.xxx - - [17/Apr/2021:19:25:05 +0000] "GET /favicon.ico HTTP/1.1" 404 556 "http://xx.xxx.xxx.xxx:5000/" "Mozilla/5.0 (Macintosh; Intel Mac OS X 11_2_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/xx.xxx.xxx.xxx Safari/537.36" "-"
You can also check the log directory
~/logs on your host machine and find the logs file of Nginx. Moreover, if you make any changes in the
logs folder, they would also reflect in the container’s
/var/log/nginx folder as well.
In this tutorial, you have learned how to share data between the inside of the Docker container and the host machine. This is essential in development environments where analyzing logs is a must for debugging.
For more resources on Docker on our blog, you can check out the following:
- How to install & operate Docker on Ubuntu in the public cloud
- Installing and Setting up Docker on CentOS 7
- Clean Up Docker Resources – Images, Containers, and Volumes
- How to Run Docker on CloudSigma (with CloudInit) Updated
- The Best Ways to Use Google’s SMTP Server - June 21, 2021
- How to Crawl a Web Page with Scrapy and Python 3 - June 16, 2021
- Installing the Apache Web Server on CentOS 7 - May 31, 2021
- How To Share Data Between a Docker Container and a Host - May 28, 2021