isudoajl
Mission Specialist
Mission Specialist
  • 2,295 Views

How to map a port on RUNNING container using PODMAN?

Jump to solution

Hello!

Somebody can help me on how to do this? The main idea that I have is that I need to stop it first..

The little I have found has been with Docker platform and didtn work with Podman.

Thanks.

Labels (4)
0 Kudos
19 Replies
Marulasiddaiah
Mission Specialist
Mission Specialist
  • 815 Views

step 1: docker stop container_id

step 2: docker run -d -p 8000:8000 --name "abc" conainer_id

0 Kudos
Rajanikanty
Mission Specialist
Mission Specialist
  • 518 Views
sudo docker ps 
sudo docker commit <containerid> <name/tag>
sudo docker run -i -p 22 -p 8000:80 -m /data:/data -t <name/tag> /bin/bash
0 Kudos
ipalagin
Mission Specialist
Mission Specialist
  • 462 Views

If your container already has SSH running and exposed, you can log into it interactively, reconfigure it to allow port forwarding and access your service through the port 22.

BTW, k8/openshift resolves this challenge by cloning a running container, additional ports can be exposed on the clone.

 

0 Kudos
Trevor
Starfighter Starfighter
Starfighter
  • 452 Views

May I learn what type of container is involved here?

Trevor "Red Hat Evangelist" Chandler
0 Kudos
isudoajl
Mission Specialist
Mission Specialist
  • 445 Views

I appreciate all the answers but I still can't find the solution, I'm not working with Docker, I'm working with Podman, they are very similar but the solution presented to me through Docker does not work for me in podman.

I am working with docker.io/redhat/ubi8-init image...

I did not imagine that it would be so difficult to find a solution to this problem using PODMAN.

0 Kudos
isudoajl
Mission Specialist
Mission Specialist
  • 439 Views

Revisiting one of the previous answers in detail I found a kind of solution, doing a commit:

$ docker stop httpd-container
httpd-container
$ docker commit httpd-container httpd-image
sha256:33da33fcad051c90ac9b7dea9b2dbda442767e05ddebd8d6db8ac6893ef4ef40

Next, we’ll remove the container and start a fresh container using the image that we just created.

 

We need to make sure we add/update the port mappings in the run command this time:

$ docker rm httpd-container
httpd-container
$ docker run -d -p 83:80 --name httpd-container httpd-image
dd2535c477ad74e80b3642abca9055efacb89eaf14572b91f91bf20cd3f0cbf3

This kind of solution works because the main point is not to lose all the setup that has already been made in the container.

If anyone knows of another type of solution that works with PODMAN too, I'd like to know...

0 Kudos
jstanesic
Mission Specialist
Mission Specialist
  • 358 Views

I think your approach might be wrong but your question is valid and I can see you found an answer. You shold not be depending on that that is set up inside the container. If container is not stateless, you should persist data that needs to survive container re creation inside a volume. Container setup should be stored inside Dockerfile used to build it.

0 Kudos
davidmasonctr
Mission Specialist
Mission Specialist
  • 305 Views

I saw one response that @Carey's comprehensive and well documented solution did not work for Podman.

If that is true, and the goal is truly to keep the container running, could this be accomplished by establishing an IPTABLES redirect, added to the same chain as the other Docker port redirects? (And then updating your documentation so that the next time you spin out a container, you've got the additional port or ports specified)

I've not tried this in the past, as I generally set persistent data and try to compose everything so that redeploying is at least nearly automated, but I've mused about whether it would be possible or not.

0 Kudos
  • 298 Views

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

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

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.

0 Kudos
  • 279 Views
# podman port -a (or use the container ID)
0 Kudos
Join the discussion
You must log in to join this conversation.