Injecting a Host Entry in podman-run

How does an application find its database? For all but the most embedded of solutions, the database exposes a port on a network. In a containerized development process, one container needs to find another container’s network address. But podman only exposes the IP address of a pod, not the hostname. How can we avoid hardcoding IP addresses of remote services into our containers?

Here is the database I built in a recent post:

# podman ps
CONTAINER ID  IMAGE                             COMMAND  CREATED       STATUS           PORTS  NAMES
c89f6ae06c1f  mysqld   12 hours ago  Up 12 hours ago         keystone-mariadb

But If I try to attach to it using the podname, things fail:

# podman  run -it --network maria-bridge    -e MYSQL_ROOT_PASSWORD="my-secret-pw"    --rm mariadb sh    -c 'exec mysql -hkeystone-mariadb -P3306 -uroot -p"$MYSQL_ROOT_PASSWORD"'
ERROR 2005 (HY000): Unknown MySQL server host 'keystone-mariadb' (-2)

I can grab the IP address from the mariadb pod:

# podman inspect keystone-mariadb | jq -r '.[] | .NetworkSettings | .IPAddress'

The –add-host flag to the podman-run command that allows us to inject an entry into /etc/hosts.

# podman  run -it --network maria-bridge   --add-host keystone-mariadb:    -e MYSQL_ROOT_PASSWORD="my-secret-pw"    --rm mariadb sh    -c 'exec mysql -hkeystone-mariadb -P3306 -uroot -p"$MYSQL_ROOT_PASSWORD"'
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 13
Server version: 10.4.10-MariaDB-1:10.4.10+maria~bionic binary distribution
Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
MariaDB [(none)]>

This is the start of a strategy for service-to-service communcation. I’ll be building on this approach moving forward.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.