Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to mount a volume from a local machine on Podman

Tags:

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

like image 798
Carabes Avatar asked Sep 23 '21 10:09

Carabes


People also ask

How do I connect to Podman container?

podman attach attaches to a running container using the container's name or ID, to either view its ongoing output or to control it interactively. The container can be detached from (and leave it running) using a configurable key sequence. The default sequence is ctrl-p,ctrl-q .

How to create a volume using podman?

A volume is a storage device cretaed and managed by Podman. Volumes are created directly using the podman volume command or during container creation. Create a volume using podman volume. podman volume create my_vol

How do I mount a podman VM?

The mount point will be /var/vmhost. While the folder name can be changed, it should remain in /var as many other locations in the Podman VM are restricted or reset to default upon reboot. In any case, the file names must match the mount point. Create a file /etc/systemd/system/run-vmhost.automount with a mode of 0644.

How do I write to a directory in a podman container?

Determine which user account is running the process within your container. Enter Podman’s user namespace, and grant this user permissions to write to your directory. Mount the volume when you run the container, add the proper SELinux label to allow the container user to write. First you need to know which UID the container is running as.

What is the difference between PS -a and LS in podman?

podman ps -a shows the container is removed, while podman volume ls shows the volume remains. Mount the existing volume to a new container. --mount: takes the following key-value pairs when mounting an existing volume. The container mount point /data2 shows that the new container mount point does not need to match the original mount point.


Video Answer


3 Answers

  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
like image 80
zhigang Avatar answered Oct 22 '22 08:10

zhigang


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)

like image 4
Peter Butkovic Avatar answered Oct 22 '22 08:10

Peter Butkovic


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
like image 1
Andrei Avatar answered Oct 22 '22 09:10

Andrei