Saturday, March 20, 2021

Podman 2.2 -- Modify Port Mapping Of An Existing Container

Problem

You need to modify port mapping for an existing container
 

Solution

Podman does not support modifying port mapping of an existing container. The supported way is to re-create a container. 

In a test environment a workaround would be to modify the configuration of a container.

Podman 2.2 stores containers' configuration data in $HOME/.local/share/containers/storage/libpod/bolt_state.db for rootless containers. bolt_state.db is an embedded key/value database for Go (https://github.com/boltdb/bolt). You may manipulate it directly with the API or with boltbrowser, a CLI Browser for BoltDB Files (https://github.com/br0xen/boltbrowser)

To install the tool use:

$ sudo dnf install -y golang
$ sudo dnf install -y git
$ go get github.com/br0xen/boltbrowser

Eventually the tool will be installed in <working_dir>/go/bin/boltbrowser

Note. Take backup of the configuration file before any manipulation.

Now you can browse/manipulate the bolt_state.db file: 
 
$ <working_dir>/go/bin/boltbrowser
$HOME/.local/share/containers/storage/libpod/bolt_state.db
 
A BoltDB file consists of buckets, and buckets are collections of key/value pairs within the database. 
 
You need to modify the value of key:
 
ctr -> <container_hash> -> config 
 
The value is a JSON string, so you need carefully amend/insert the below snippet into the value:
 
"portMappings": [
    {
        "containerPort": <container_port>,
        "hostIP": "",
        "hostPort": <host_port>,
        "protocol": "tcp"
    },
    {
        "containerPort": <container_port>,
        "hostIP": "",
        "hostPort": <host_port>,
        "protocol": "tcp"
    }
]
 
Adjust the number of ports as required.
 
Verify the settings with:
 
$ podman inspect <container_name>
 
To identify the container hash use:
 
$ podman inspect <container_name> | grep Id 
 
 
Note. Before manipulating the config database, ensure all the containers are stopped.
 
 

No comments: