20

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

BMitch
  • 231,797
  • 42
  • 475
  • 450
Carabes
  • 532
  • 2
  • 4
  • 16

6 Answers6

26
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
Vassopoli
  • 41
  • 7
zhigang
  • 6,597
  • 4
  • 30
  • 24
  • Why not `init -v ${HOME}:${HOME}` as I did in https://stackoverflow.com/a/71882521/179581 ? Any security implications? – Andrei Apr 15 '22 at 10:08
  • I think that doesn’t work: you don’t have permission. Not sure it changed. I would love to use this way if podman supports it. – zhigang Apr 16 '22 at 11:46
  • Works fine for me on macOS. Cannot test for other OS right now so idk – Andrei Apr 17 '22 at 07:46
  • 2
    Just tested and yes it worked. Don't understand why official podman machine init man page says: ```The root filesystem is mounted read-only in the default operating system, so mounts must be created under the /mnt directory. ``` https://docs.podman.io/en/latest/markdown/podman-machine-init.1.html Maybe something changed with time. Thank you @Andrei ! I updated the answer. – zhigang Apr 18 '22 at 14:01
  • A good example to verify this https://medium.com/@butkovic/favoring-podman-over-docker-desktop-33368e031ba0 – prayagupa Dec 29 '22 at 23:59
  • Thank you! this fixed issue `Error: statfs xxx no such file or directory` on my Mac. Now `podman-compose` runs successfully. – haolianglearn Jan 31 '23 at 05:05
5

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)

Peter Butkovic
  • 11,143
  • 10
  • 57
  • 81
5

If you have Windows and WSL (Windows Subsystem for Linux), then it is possible to create a volume without having to remove and re-init podman-machine-default.

First connect to the podman machine using WSL:

wsl -d podman-machine-default

Then execute these commands:

[root@COMPUTER ~]# podman volume create myVolume
myVolume
[root@COMPUTER ~]# podman volume mount myVolume
/var/lib/containers/storage/volumes/myVolume/_data

Now to share files with a container, copy them from the WSL mount point into the volume mount point:

[root@COMPUTER ~]# cp /mnt/c/users/myname/myfolder/*.* /var/lib/containers/storage/volumes/myVolume/_data

Finally you can mount a volume in a container to access these files. Exit wsl and run:

podman run --rm -v myVolume:/remoteFolder -t docker.io/bash ls /remoteFolder

Be careful, arguments ordering matters

bN_
  • 772
  • 14
  • 20
3

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
Andrei
  • 10,918
  • 12
  • 76
  • 110
0

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.

VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • 1
    I was hoping for it to work, but nope. I've updated podman to 4.0.2 and re-created the VM. Still the same error. Perhaps I should somehow enforce the latest qemu VM if thats the case – Dmitry Kankalovich Mar 21 '22 at 13:05
  • @DmitryKankalovich Maybe create a new issue, referencing https://github.com/containers/podman/issues/10379? Your use case needs to be investigated. – VonC Mar 21 '22 at 13:08
  • thanks, I will take a look at that. For now 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 – Dmitry Kankalovich Apr 11 '22 at 10:07
  • @DmitryKankalovich OK, thank you for the feedback. I have included your comment in the answer as well. – VonC Apr 11 '22 at 12:14
-3

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/

muncherelli
  • 2,887
  • 8
  • 39
  • 54
  • 1
    I'm afraid I'm still getting an error like `Error: statfs ~/localFolder: no such file or directory` – Carabes Oct 11 '21 at 07:52
  • Are you getting that error in your container or on the host? – muncherelli Oct 11 '21 at 14:16
  • 1
    when podman is using a VM (for example on Mac), we need first to mount the host into the VM and then from the VM into the container. Sadly there are 3 layers involved here. – Gaetan Oct 12 '21 at 11:38
  • 1
    This error is in the container I think (the host has that file). So, how can I mount the host into the VM? – Carabes Oct 13 '21 at 06:33
  • 3
    The error is from podman on the virtual machine, the container is never started. [this](https://github.com/containers/podman/issues/8016) is the feature request. – Bracken Dec 03 '21 at 18:37
  • Agree with @Bracken, I still didn't find the `-v` work like docker on the podman side yet. Hope it would be implemented soon. – Tuhin Feb 22 '22 at 14:27