Goglides Dev 🌱

Balkrishna Pandey
Balkrishna Pandey

Posted on • Updated on

Can docker container access files on host?

Yes, a Docker container can access files on the host machine. This is achieved through volume mapping, which allows the host file system to be mounted inside the container.

Here's an example command to run a Docker container and map a host directory to a container directory:

docker run -d -v /host/directory:/container/directory <image_name> <command>
Enter fullscreen mode Exit fullscreen mode

In this example, the host directory /host/directory is mapped to the container directory /container/directory. Any changes made to the files inside the container directory will be reflected in the host directory and vice versa.

This feature allows you to persist data generated by a container even after it stops. Additionally, you can use this feature to provide data to a container from the host.

It's important to note that the host directory that is being mapped must exist before running the container; otherwise, Docker will create an empty directory instead.

To verify that a Docker container is accessing files from the host, you can follow these steps:

  • Start a container with volume mapping:
docker run -d -v /host/directory:/container/directory <image_name> <command>
Enter fullscreen mode Exit fullscreen mode
  • Connect to the running container:
docker exec -it <container_id> /bin/bash
Enter fullscreen mode Exit fullscreen mode
  • Inside the container, navigate to the mapped directory:
cd /container/directory
Enter fullscreen mode Exit fullscreen mode
  • List the contents of the directory:
ls -l
Enter fullscreen mode Exit fullscreen mode

You should see the same files and directories in the host directory /host/directory. If the files inside the container directory are modified, the changes will also be reflected in the host directory.

Here is a real-life example that demonstrates how to access files on the host from a Docker container using the busybox image:

  • Create a host directory:
mkdir ~/host_dir
Enter fullscreen mode Exit fullscreen mode
  • Create some files in the host directory:
touch ~/host_dir/file1.txt ~/host_dir/file2.txt
Enter fullscreen mode Exit fullscreen mode
  • Start a busybox container with the host directory mapped to a container directory:
docker run -v ~/host_dir:/container_dir -it busybox /bin/sh
Enter fullscreen mode Exit fullscreen mode
  • Inside the container, navigate to the mapped directory:
cd /container_dir
Enter fullscreen mode Exit fullscreen mode
  • List the contents of the directory:
ls -l
Enter fullscreen mode Exit fullscreen mode

You should see the files file1.txt and file2.txt in the /container_dir directory, which is mapped to the host directory ~/host_dir.

You can now make changes to the files inside the container and verify that those changes are reflected in the host directory.

  • Inside the container, add some content to one of the files:
echo "Hello from the container" > file1.txt
Enter fullscreen mode Exit fullscreen mode
  • Exit the container:
exit
Enter fullscreen mode Exit fullscreen mode
  • Verify that the changes made inside the container are reflected in the host directory:
cat ~/host_dir/file1.txt
Enter fullscreen mode Exit fullscreen mode

You should see the message Hello from the container in the file1.txt file, which was modified inside the Docker container.

Note: Docker directly accesses files on the host without creating separate copies or snapshots. Any modifications to the host file system are immediately visible within the container and vice versa.

This demonstrates that changes made to files inside the Docker container are reflected in the host directory through volume mapping. The same principle applies to changes made to files in the host directory, which would also be reflected inside the container.

Top comments (0)