Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to run two containers in same port in same pod by podman?

I am going to make a server pack using podman in my local machine (Fedora 31 KDE). The pack includes:

  1. Web server (image: php:7.2-apache, volume: $VARIABLE:/var/www/html:Z)
  2. MySQL server (image: mysql:8, volume: /var/lib/mysql:/var/lib/mysql:Z)
  3. Another web server (image: phpmyadmin/phpmyadmin:5)

Now, I need a way to be able to start the 3 servers with a single command as well as stop with a single command. Therefore, I created a pod and put them under the pod. I need access to both the main web server and phpmyadmin server from the host. Here is a bash script I am usig to create them:

#!/usr/bin/env sh

if [ "$1" != "" ];then
    WEB_PATH="-v $1:/var/www/html:Z"
fi

sudo podman pod create -n servers -p 80:80 -p 8080:80
sudo podman run -dt --pod servers --rm --name web $WEB_PATH php:7.2-apache
sudo podman run -dt --pod servers --rm --name mysql --env MYSQL_ROOT_PASSWORD=iamroot -v /var/lib/mysql:/var/lib/mysql:Z mysql:8
sudo podman run -dt --pod servers --rm --name pma phpmyadmin/phpmyadmin:5

The problem is, 1st and 3rd both servers use port 80 as their default. I cannot figure out this problem.

I need access the main web server on port 80 and pma server on port 8080 from the host.

like image 382
Chitholian Avatar asked Sep 16 '25 01:09

Chitholian


1 Answers

I think what you want to accomplish is basically done by changing the default share option by pods.

In general Pods share cgroup,ipc,net,uts. Making intercontainer communication easy. Having this enabled (or rather not changing it) does not allow you to port map containers into the pod, resolving in: Error: cannot set port bindings on an existing container network namespace

Like statet by tgogos, rootless containers do always share the same network. This is accomblished by the slirp4netns project. Check this site for more information about networking with containers.

I dont fully get why you use sudo but you could also create your own network using sudo podman network create <networkname> and assigning containers to this network with the --network <networkname> flag.


Coming to solutions of your problem

Creating a pod that is not sharing network namespace and therefor portmapping will be containerbased not pod based.

  1. Create Pod
    • podman pod create --name servers --share cgroup,ipc,uts
  2. Assign containers with ports to your created pod
    • You can now freely assign ports to containers, using apache/nginx or others to make things work as desired. How to assign multiple instances on apache
    • podman run -dt --pod servers --rm --name web -p 80:80 $WEB_PATH php:7.2-apache
    • podman run -dt --pod servers --rm --name mysql --env MYSQL_ROOT_PASSWORD=iamroot -v /var/lib/mysql:/var/lib/mysql:Z -p 8080:8080 mysql:8
    • podman run -dt --pod servers --rm --name pma -p SOMEPORT:80 phpmyadmin/phpmyadmin:5
  3. Finding IP
    1. Rootful mode
      • sudo podman inspect -f "{{.NetworkSettings.IPAddress}}" <containername>
    2. Rootless mode
      • check ip addr show for any virtual bridge, or if already created a cni podman network
      • or check hostname -I this will only show IP-adresses, but any of them should work
    3. Connecting to Database Container
      • your Database should now be reachable by the ip adress you got on Step 3 and port from Step 2 could be something like
      • podman network 10.89.0.1:8080
      • virbr0 192.167.133.1:8080

Note that Steps 1-2 can also be run as sudo, but I personally do not like having sudo containers/pods, since this is the beauty of podman to have userspaces and rootless containers. Let systemd services manage restarts etc.

like image 135
kanelbulleproductions Avatar answered Sep 18 '25 18:09

kanelbulleproductions