I am going to make a server pack using podman in my local machine (Fedora 31 KDE). The pack includes:
- Web server (image: php:7.2-apache, volume: $VARIABLE:/var/www/html:Z)
- MySQL server (image: mysql:8, volume: /var/lib/mysql:/var/lib/mysql:Z)
- 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.
2
Answers
I think you won’t be able to have 2 containers using the same
port
number, because they share the same network namespace.From Configuring container networking with Podman:
Further reading:
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 usingsudo 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.
podman pod create --name servers --share cgroup,ipc,uts
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
sudo podman inspect -f "{{.NetworkSettings.IPAddress}}" <containername>
ip addr show
for anyvirtual bridge
, or if already created acni podman
networkhostname -I
this will only show IP-adresses, but any of them should work10.89.0.1:8080
192.167.133.1:8080