A Docker compose command solution to retrieve information from another container service and keep it on the requesting service. The example is resolving an IP address of a MySQL database service and adds it to the apache-php .env.

My first approach was, to every time look up the (changed) IP address of the database Docker service, and manually update the .env using:

docker inspect -f '{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' SERVICENAME

This cumbersome step worked to reestablish a successful connection to the MySQL database from the PHP app using IP instead of hostname. But, as stated, this required several extra steps, and started to become annoying.

Solution: resolve the database service’s IP, and update the PHP dotEnv – all automatically!

It took quite some research and try‘n‘errors with un-/supported Unix commands in Docker service shells, but ultimately I found a much more elegant – and automatic! – approach to update the .env file with the database service‘s IP address!
# Database connection:
MYSQL_HOST="{inserted automatically}"
MYSQL_DATABASE="testdatabase"
MYSQL_USER="mysqluser"
MYSQL_PASSWORD="mysqlpass"

I automated these steps, by adding the following commands to my docker-compose-file
(within the apache2-php service configuration):

services:
    apache-php:
        # other settings...
        volumes:
            #- other mappings...
            - ./.env:/var/www/.env.host
        entrypoint: ["/bin/sh","-c"]
            command:
                - |
                    getent ahosts DATABASESERVER | awk '{ print $1 }' | tail -1 > /tmp/serviceip-db 2>&1
                    sed "s/^MYSQL_HOST=.*/MYSQL_HOST=$(cat /tmp/serviceip-db | tr -d '\n')/g" /var/www/.env.host > /var/www/.env 2>&1
Result: every time the apache-php Docker service is (re-)started, it will dynamically fetch & update the active MySQL-database service’s IP to the PHP .env!

(?) Explanation of the docker-compose commands

  1. First of all, I don‘t connect the app‘s .env from the host 1:1 into my Docker webserver service, but provide it to be internally modified and copied from: /var/www/.env.host
     
  2. Because my Docker container services are internally on the same network, and resolvable by hostname, I look up the database service‘s IP address and write the result into a temporary file: /tmp/serviceip-db
     
  3. Now the foundation is set to merge the retrieved database service‘s IP with the required .env for the PHP app!
     
  4. Using the Unix sed command, the line starting with “MYSQL_HOST…” from the original dotenv .env.host is being replaced with the file contents (read: database IP) from the /tmp/serviceip-db – and the final dotenv is created under `/var/www/.env`
     
  5. Because my PHP app looks for and reads from the `.env`-file, it has now access to the correct MySQL database host IP to successfully connect! 🥳
     

…and of course, all other dotEnv variables are also available.

This approach with the 2 commands in docker-compose yaml can be used for various other scenarios, too.

Share:
  • 1
  • 0

1 thought on “Docker on Mac: how to automatically resolve another service’s IP and add it to the .ENV”

Questions? Suggestions? Let us know with a comment!

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