Podman is a container engine that lets you build, run, and manage containers and pods without needing a central [[Daemon|daemon]]. A more or less drop in replacement for [[Docker]].
# What is podman and how does it work
## Daemonless Architecture
Unlike [[Docker]], which relies on a continuously running [[Daemon|daemon]], Podman operates without one. Every command you run spawns a process that directly creates and manages containers. This design simplifies the architecture and improves security by allowing for **rootless** container operation, meaning you can run containers as a non-privileged user.
## [[OCI Standards and Components]]
Podman adheres to the [Open Container Initiative (OCI)](https://opencontainers.org/) standards:
- **OCI Images:** It works with container images that follow OCI specifications.
- **OCI Runtime:** It can use various OCI-compliant runtimes (like `runc`) to execute containers.
- **Integration with Buildah and Skopeo:**
- **Buildah** is used for building container images.
- **Skopeo** handles image transfer and inspection between container registries without requiring a local daemon.
## Container and Pod Management
- **Containers:** Each container is an isolated process that utilizes [[Linux#Kernel]] features such as namespaces and cgroups to ensure resource isolation and control.
- **Pods:** Inspired by Kubernetes, Podman can group multiple containers into a pod. Containers in the same pod can share networking, storage, and other resources. This is particularly useful for microservices that need to work closely together.
# Documentation (CLI usage)
## Image
A image is a operating system that may be preconfigured.
### View installed images
To view installed images you can run:
`podman images` or `podman images all`
### Downloading a image
While its possible to install the image on the fly when creating a container, its advised to pull and download the images beforehand. The command to do so is:
`podman pull <image-name>`
So to pull for example ubuntu you would do:
`podman pull ubuntu:latest`
Tho its advised to also give a specific version. Old images can be removed using:
`podman rmi <image-name>`
### Creating custom images
If you want to streamline the customazations of a image you need to create you own images. This can be done using either a [[Dockerfile|dockerfile]] or [[Containerfile|containerfile]]. After creating this file you can build the image using
`podman build -t <custom-name>:latest` or `-t <custom-name>:<version>`.
This looks in the current directory for a build file and builds it and at the same time tags it as the version. To use a custom name for the build file you could add (example) `-f local.Dockerfile` to the command to instead of searching for `Dockerfile` search for `local.Dockerfile`.
### Custom image upload
After building a working image you can upload it to a repository for later use or for others to use.
`podman push <name>:<version> docker://docker.io/yourusername/my_custom_image:latest` -> this would upload the image to the [[Docker]] registry. Its also possible to upload to a custom repository.
### Disable the cache
To disable the cache when building use `--no-cache` argument. This prevents cached versions of packages to be used and instead will reinstall it. Good for debugging or if something is not working after version upgrading etc.
## Container
To now start a image you need to run `podman run --name <container-name> <image-name>:<image-version>`.
Here are some interesting cli arguments to add:
- -it -> Combines interactive mode (`-i`) and pseudo-TTY (`-t`), so you can interact with the container’s terminal
- -t --tty --terminal -> Creates a terminal for the program to run in
- -i --interactive -> Makes the terminal interactive
- --rm -> Delete container after running it
- -d -> Run as [[Daemon|daemon]]
- -p -> Port mapping to host computer
- -v -> to mount [[Podman#Volumes]]
- --restart -> restart policies (e.g., `no`, `on-failure`, `always`, `unless-stopped`)
- Others include resource limits, network options
To show all currently running containers run `podman ps`.
## Volumes
Volumes are dedicated storage areas managed by podman itself. It enables containers to share data and persist data after shutdown. You can create a volume using `podman volume create <volume-name>`. You can list all available volumes using `podman volume ls`. Removing them works by using: `podman volume rm <volume-name>`.
## Cleanup
To automatically cleanup images that are not used in any container you can run `podman image prune`. The same also works for containers, which deletes all containers that are not running: `podman container prune`. To clean all [[Podman#Pods]] you can run `podman pod prune`, which will delete all pods that are no longer in use. To cleanup [[Podman#Volumes]] run `podman volume prune`. All of this can be run using just one command: `podman system prune`.
## Pods
Concept of grouping multiple containers together to provide a shared namespace an mimicking [[Kubernetes (k8s)]] (without the scaling). This means containers in the same group are able to connect to others using `localhost:<port>`. All containers still run in a isolated environment but when you stop/ start one container all will start/ stop. To create a pod you can run `podman pod create --name mypod.
Listing pods works the same way it works for other commands using `podman pod ps`. To remove a pod run `podman pod rm mypod`. Its not possible to share a container to multiple pods, always just a single pod.
### Exporting/ Importing pods
How?
### Scaling inside pods
Not possible?
# Compose
For creating multiple containers automatically.
## Installing podman-compose
`brew install podman-compose`
## Using compose
...
# VsCode integration
- debugging
- reload
- live
# Kubenetes integration
To deploy your pod to [[Kubernetes (k8s)]] you can run `podman generate kube <pod-name> > <output-file.yaml>`. This is only good for initially generating the kubernetes config.
# Podman vs [[Docker]] vs [[Rancher]]
- docker swarm
- podman is more secure
- podman daemonless
- Docker has better community
- Waiting for docker documentation
# Example
## Creating a api that can communicate with a Db
This example will install a db and a custom api that communicates with the db.
### Creating the pod
`podman pod create --name mypod -p 9000:80`
### Installing the images
`podman pull postgres:latest`
`podman pull tiangolo/uvicorn-gunicorn-fastapi:latest`
### Running the container
```bash
podman run -d --pod mypod --name mypostgres \
-e POSTGRES_PASSWORD=mysecretpassword \
-e POSTGRES_DB=mydatabase \
postgres:latest
```
```bash
podman run -d --pod mypod --name myfastapi \
-e DATABASE_URL=postgresql://postgres:mysecretpassword@localhost/mydatabase \
-p 80:80 \
tiangolo/uvicorn-gunicorn-fastapi:python3.9
```
### Check container state
`podman pod ps`
Now it should run.
## Containerizing a api with custom build
This example is to show how to containerize a already built and locally running api with a custom containerfile.
### Built and name the container
```bash
podman build -t my-app-name-dev -f Containerfile .
```
### Run the container as a one time runner
```bash
podman run -it --rm \
-v "$(pwd)":/app:Z \
-p 8000:8000 \
my-app-name-dev
```