'How to mount a volume from a local machine on Podman

I'm trying to use Podman for local development. My idea is to use a local folder and sync it with the container where I'll be running my application.

I found that the -v option that I would use if I was working with Docker works with the server machine, as it says in the documentation -v Bind mount a volume into the container. Volume src will be on the server machine, not the client. Because of that, when I use that option the folder is not mounted and I can't find it when I access it using podman exec -it application bash

Is there a way I can sort this out? I want to something equivalent to:

docker run -v localFolder:/remoteFolder application

where localFolder is a path in my local machine, that will be mapped on remoteFolder on the container



Solution 1:[1]

  podman machine stop podman-machine-default
  podman machine rm podman-machine-default
  podman machine init -v $HOME:$HOME
  podman machine start

  podman run -ti --rm -v $HOME:$HOME busybox

Solution 2:[2]

You need to make sure to mount volume first to podman machine (in the podman machine init command).

Let’s say, we’re interested in:

  • option to mount the $HOME directory (or any of it’s subdirectories) to containers,
  • reserving 4 CPUs, 60GB of disk space and 6GB of memory for podman machine.

Let’s create the podman machine using:

podman machine init --cpus=4 --disk-size=60 --memory=6096 -v $HOME:$HOME

and start it afterwads:

podman machine start

To see if it runs, let’s use:

podman machine ls

Let’s first run sample (ubuntu) container without mount and see what is present in /opt dir:

podman run ubuntu ls /opt

No output indicates, that /opt dir is empty in the container.

Now let’s try with volume mount option:

mkdir test-dir
touch test-dir/test-file.txt
podman run -v ./test-dir:/opt ubuntu ls /opt
test-file.txt

We see that mounted dir with it’s content is accessible in container now!

(Content extracted from: https://medium.com/@butkovic/favoring-podman-over-docker-desktop-33368e031ba0)

Solution 3:[3]

Check if podman 4.0.0 (and latest releases like 4.0.2) from Feb. 2022 would help.

There are still bugs (like issue 13548), but the idea is:

Issue 10379 "Shared volumes, like in Kubernetes Pod" has been resolved with PR 11409.

Pod Volumes Support

added support for the --volume flag in pods using the new infra container design.
Users can specify all volume options they can with regular containers.

Specifying the --volume flag causes the infra container to be populated with the specified mounts.
All containers joining the pod then have a VolumesFrom container, causing them to inherit the mounts.

The documentation mentions:

Create a bind mount.

If you specify, -v /HOST-DIR:/CONTAINER-DIR, Podman bind mounts /HOST-DIR in the host to /CONTAINER-DIR in the Podman container.

Similarly, -v SOURCE-VOLUME:/CONTAINER-DIR will mount the volume in the host to the container.
If no such named volume exists, Podman will create one.


Note: issue 13548 has been resolved with PR 13594 and cdoern/podman commit 7a53428 (Apr. 2022)

fix pod volume passing and alter infra inheritance

the infra Inherit function was not properly passing pod volume information to new containers.

Alter the inherit function and struct to use the new ConfigToSpec function used in clone pick and choose the proper entities from a temp spec and validate them on the spegen side rather. than passing directly to a config


Pending all issues to be resolved, Dmitry Kankalovich mentions in the comments:

For now (Apr. 2022), I went with the workaround to mount the volume at the time of machine init: podman machine init -v ./data:/mnt/data which is not flexible at all, but gets wheels rolling.

Solution 4:[4]

Iterating on @zhigang's answer:

podman machine stop podman-machine-default || true
podman machine rm podman-machine-default --force || true
podman machine init -v "$(pwd):$(pwd)"
podman machine start

Now docker-compose / podman-compose just work with their config

version: '3.9'  # needed for PyCharm to work
services:
  app:
    image: python:alpine
    working_dir: /app/
    volumes: [.:/app/]
$ podman-compose run --rm app venv/bin/python main.py

Solution 5:[5]

I recommend using the --mount flag instead of -v or --volume. I'm assuming you'd like to be able to easily modify the files on your host while running your app in a container. Here's a quick example:

First, the directory needs to exist on the host:

user@host:~$ mkdir ~/localFolder

Create a file inside of the ~/localFolder directory with some text inside:

user@host:~$ echo "Sample Text" > ~/localFolder/test

Then, start up your container using a bind mount. This example is using a podman rootless container with a Debian Bullseye image as an unprivileged user. Note: It is best practice to use an absolute path for both the source (host) and target (container) directories:

user@host:~$ podman run -it --rm --mount type=bind,source=/home/$USER/localFolder,target=/remoteFolder docker.io/debian:bullseye /bin/bash

Now your ~/localFolder directory will be accessible inside of your container as /remoteFolder:

root@f724a0337f76:/# cat /remoteFolder/test 
Sample Text

You can add new files or modify existing files in this folder without restarting the container.

Docker has a great explanation of bind mounts here: https://docs.docker.com/storage/bind-mounts/

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 GabLeRoux
Solution 2 Peter Butkovic
Solution 3
Solution 4
Solution 5 muncherelli