return to main page
graph TB
a1[curl] -.->a2[caddy container reverse proxy]
a2 -->|"for http://whoami.example.com:8080"| a3["whoami container"]
a2 -->|"for http://nginx.example.com:8080"| a4["nginx container"]
Set up a systemd user service example2.service for the user test where rootless podman is running the container image docker.io/library/caddy. Configure socket activation for TCP port 8080. The caddy container is acting as a HTTP reverse proxy that forwards requests to 2 backends. Requests to http://whoami.example.com:8080 are forwarded to the whoami container. Requests to http://ngnix.example.com:8080 are forwarded to the nginx container.
In this example the curl option --resolve option is used for name resolution. In other words, the domain names whoami.example.com and nginx.example.com do not need to be resolvable in public DNS.
- Create a test user
sudo useradd --create-home test - Open a shell for user test
sudo machinectl shell --uid=test - Optional step: enable lingering to avoid services from being stopped when the
user test logs out.
loginctl enable-linger test - Create directories
mkdir -p ~/.config/systemd/user mkdir -p ~/.config/containers/systemd mkdir ~/caddy_etc - Pull caddy container image
podman pull docker.io/library/caddy - Pull whoami container image
podman pull docker.io/traefik/whoami - Clone git repo
git clone https://github.com/eriksjolund/podman-caddy-socket-activation.git - Install the network unit file
cp podman-caddy-socket-activation/examples/example2/mynet.network \ ~/.config/containers/systemd/ - Install the container unit files
cp podman-caddy-socket-activation/examples/example2/*.container \ ~/.config/containers/systemd/ - Install the socket unit files
cp podman-caddy-socket-activation/examples/example2/caddy.socket \ ~/.config/systemd/user/ - Install the Caddyfile
(The path ~/caddy_etc/Caddyfile was arbitrarily chosen)
cp podman-caddy-socket-activation/examples/example2/Caddyfile \ ~/caddy_etc/Caddyfile - Reload the systemd user manager
systemctl --user daemon-reload - Start the socket for TCP port 8080
systemctl --user start caddy.socket - Pull the whoami container image
podman pull docker.io/traefik/whoami - Pull the nginx container image
podman pull docker.io/library/nginx - Start the nginx container
systemctl --user start nginx.service - Start the whoami container
systemctl --user start whoami.service - Download the URL http://nginx.example.com:8080 from the caddy
container and see that the request is proxied to the container nginx.
Resolve nginx.example.com to 127.0.0.1.
The following output is printed
curl -s --resolve nginx.example.com:8080:127.0.0.1 \ http://nginx.example.com:8080 | head -4result: The default nginx web page was downloaded<!DOCTYPE html> <html> <head> <title>Welcome to nginx!</title> - Download the URL http://whoami.example.com:8080 from the caddy
container and see that the request is proxied to the container whoami.
Resolve whoami.example.com to 127.0.0.1.
The following output is printed
curl -s --resolve whoami.example.com:8080:127.0.0.1 \ http://whoami.example.com:8080 | grep X-Forwarded-Forresult: The IPv4 address 127.0.0.1 matches the IP address of X-Forwarded-ForX-Forwarded-For: 127.0.0.1 - Check the IPv4 address of the main network interface.
Run the command
The following output is printed
hostname -Iresult: The IPv4 address of the main network interface is 192.168.10.108 (the address furthest to the left). Note, the detected IP address will most probably be different when you try it out on your system.192.168.10.108 192.168.39.1 192.168.122.1 fd25:c7f8:948a:0:912d:3900:d5c4:45ad - Download the URL http://whoami.example.com:8080 from the caddy
container and see that the request is proxied to the container whoami.
Resolve whoami.example.com to the IP address of the main network interface.
Use the IP address that was detected in the previous step.
The following output is printed
curl -s --resolve whoami.example.com:8080:192.168.10.108 \ http://whoami.example.com:8080 | grep X-Forwarded-Forresult: The IPv4 address of the main network interface, 192.168.10.108, matches the IPv4 address of X-Forwarded-ForX-Forwarded-For: 192.168.10.108 - From another computer download a web page http://whoami.example.com:8080 from the caddy
container and see that the request is proxied to the container whoami.
The following output is printed
curl -s --resolve whoami.example.com:8080:192.168.10.108 \ http://whoami.example.com:8080 | grep X-Forwarded-ForCheck the IP address of the other computer (which in this example runs macOS). In the macOS terminal run the commandX-Forwarded-For: 192.168.10.161The following output is printedipconfig getifaddr en0result: The IPv4 address of the other computer matches the IPv4 address of X-Forwarded-For192.168.10.161
The file mynet.network currently contains
[Network]
Options=isolate=true
Internal=true
Network=mynet
The line
Internal=true
prevents containers on the network mynet to connect to the internet. To allow containers on that network to download files from the internet you would need to remove the line.