Skip to content

Instantly share code, notes, and snippets.

@spajus
Created April 6, 2024 07:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save spajus/871d2a1ac7ca0f988448ce7d650cb464 to your computer and use it in GitHub Desktop.
Save spajus/871d2a1ac7ca0f988448ce7d650cb464 to your computer and use it in GitHub Desktop.
Docker volumes

When running an application inside a Docker container on Linux and you need to persist files or share files between the host and the container, you indeed use volumes. Docker volumes are the preferred mechanism for persisting data generated by and used by Docker containers.

To mount a volume and persist files, you generally follow these steps:

  1. Decide on the Host Directory: First, decide which directory on your host system you want to persist data in. This can be any path, for example, /home/username/data.

  2. Create the Volume (Optional): This step is optional because Docker can automatically create the host path if it doesn't exist when you run the container. However, if you want to configure or use a named volume, you can create one explicitly with:

    docker volume create volume_name

    For bind mounts (a specific directory or file on the host), you don't need to create anything in advance; just specify the host path when running your container.

  3. Run the Container with Volume: When you run your container, you use the -v or --mount flag to specify the mount. Here are examples of both:

    • Using -v (the shorter syntax):

      docker run -d -v /home/username/data:/data container_image

      This mounts the host directory /home/username/data to /data inside the container. If /home/username/data doesn't exist, Docker will automatically create it for you (assuming it has permissions).

    • Using --mount (the more verbose syntax):

      docker run -d --mount type=bind,source=/home/username/data,target=/data container_image

      This does the same as the -v option but is clearer and recommended for new definitions.

  4. Interacting with the Volume: Any files written to /data inside the container will now be stored on the host in /home/username/data, and they will persist after the container stops or is removed.

  5. Named Volumes: If you're using a named volume (for instance, for Docker-managed storage), your run command might look like this:

    docker run -d --mount source=myvolume,target=/data container_image

    Here, myvolume is the name of the Docker volume, and /data is where it's mounted inside the container.

Note

  • Permissions: Ensure the Docker daemon has access to the directory you're trying to bind-mount. Permission issues are common, especially when dealing with non-default directories.
  • Data Persistence: For named volumes, data persists across container restarts and removals, and is managed entirely by Docker. For bind mounts, you're directly managing the host path, so data persists based on what you do with that directory.
  • Security: Be mindful of security implications when mounting directories into a container, especially if the container runs third-party or untrusted code.

By following these steps, you can persist files generated by your application running inside a Docker container or provide files to your application from the host system.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment