WindowsMacSoftwareSettingsSecurityAndroidProductivityLinuxPerformanceAppleDevice Manageme.. All

How to Share Folders Between Host and Docker Containers

Edited 5 days ago by ExtremeHow Editorial Team

Docker DesktopFoldersFile SharingHostContainersConfigurationDevOpsSoftware DevelopmentCloud ComputingVirtualization

This content is available in 7 different language

Docker is a powerful tool used to develop and deploy applications inside containers. Containers are an isolated environment where applications can run without interference from other processes on the same machine. When using Docker, you may face situations where you need to share files or folders between your host machine and Docker containers. This guide will walk you through the process of sharing folders between a host machine and Docker containers in a detailed and comprehensive manner. We will cover the basic concepts, give examples, and look at potential challenges.

Understanding Docker Volumes

Before going into the steps to share folders, it is important to understand how Docker handles data persistence. Docker containers are ephemeral; once shut down, any data stored in them may be lost. Docker provides various mechanisms to manage and persist data, and one of the primary ways is through volumes. A Docker volume is a persistent data storage that a container can access, and it exists outside of the container's lifecycle.

Docker manages volumes independently, meaning they are isolated from any specific container. This contributes significantly to data durability because even if you delete a container, the data within its volume remains intact and can be attached to another container. Volumes are ideal for sharing folders between hosts and Docker containers because of their lifetime independence from the container and their ability to be explicitly controlled.

Types of Docker volumes

Docker supports different types of volumes for different use cases:

Using bind mounts to share folders

The simplest way to share a folder between a host and a Docker container is via a bind mount. Let us look at the steps involved and see an example:

A step-by-step guide to using bind mount

  1. Create a directory on the host:

    Create a directory on your host system that you want to share with Docker. For example, you could create a directory called /Users/username/shared.

  2. Modify the docker run command:

    Next, update the Docker run command to include the bind mount. Use -v or --volume flag after the path mapping. For example, to mount /Users/username/shared to /shared in your container, the command is:

    docker run -v /Users/username/shared:/shared my-image

    In this example, my-image refers to the Docker image you are running. The mount allows any changes made to /shared inside the container to be reflected in the /Users/username/shared folder on the host.

  3. Access the shared folder in the container:

    Once the container is running with a bind mount, any files modified inside /shared inside the container directly affect files in the host's /Users/username/shared directory and vice versa.

Example of bind mount usage

Here is a practical example of sharing files between a host and a Docker container using the Nginx web server. Let's say you want to serve HTML files located in a directory on your host machine:

  1. Set up the shared directory:
    • Create a directory named website on your host.
    • Inside website directory add an index.html file with some content.
  2. Run the Nginx container with a bind mount:

    Use the following command to bind mount website directory from the /usr/share/nginx/html directory into the container:

    docker run --name my-nginx -v "$PWD"/website:/usr/share/nginx/html:ro -d nginx

    :ro option makes the bind mount read-only inside the container.

  3. Access the Nginx server:

    Visit http://localhost in a web browser to view the index.html file served by Nginx from a shared directory on your host.

Using Docker volumes to share folders

While bind mounts relate directly to paths on the host file system, Docker volumes are more abstract and are managed by Docker. Let's learn how to create and use Docker volumes:

Creating a Docker volume

Use docker volume create command to create a new volume, as shown below:

docker volume create my-volume

This command creates a volume called my-volume that Docker will manage.

Attaching a volume to a container

To attach the created volume to the container, use the following command:

docker run -v my-volume:/app/data my-image

This example mounts my-volume in the container at /app/data. Any data written to /app/data remains in my-volume, even if the container is stopped or deleted.

Volume usage example

Consider a scenario where you want to store a database across container restarts. You can use a volume to mount the data directory of a database container:

  1. Create a Docker volume:
    docker volume create db-data
  2. Run the database container with the volume:
    docker run -d --name my-database -v db-data:/var/lib/mysql mysql:5.7

    In this case, any data changes in the /var/lib/mysql directory are stored in db-data volume.

  3. Data durability:

    Even if you stop and delete my-database container, db-data volume retains the data, ready to be attached to another instance.

Debugging and troubleshooting bind mount problems

Setting up a bind mount can be easy, but it can be problematic. Here are some common problems and solutions:

Conclusion and best practices

Folder sharing between the host and Docker container is an essential functionality that facilitates development, data persistence, and collaboration. It is important to decide which type of mount (bind mount or volume) is appropriate for your use case. Bind mounts give you direct path control, while Docker volumes are more portable and easier to manage for long-term data persistence.

Here are some best practices to consider:

With these insights, methods, and examples, you should be well-equipped to effectively handle folder sharing between your host and Docker containers. Feel free to explore further and modify the methods to suit your project's needs.

If you find anything wrong with the article content, you can


Comments