Docker Compose not setting environment variables

Hi!
I’m using GitHub - litespeedtech/ols-docker-env: OpenLiteSpeed Docker Environment to set up my dev environment. It should work as it is but Wordpress (litespeed container) can’t connect to the DB. I’m setting the mysql container’s IP manually, but it rotates every time the service is restarted.

So I want to set the DB_HOST variable in wp-config.php to an environment variable name WP_DB_HOST. I’m doing it this way:

version: '3'
services:
  mysql:
    platform: linux/amd64
    image: mariadb:10.5.9
    logging:
      driver: none
    command: --max_allowed_packet=256M
    volumes:
      - "./data/db:/var/lib/mysql:delegated"
    environment:
      MYSQL_ROOT_PASSWORD: ${MYSQL_ROOT_PASSWORD}
      MYSQL_DATABASE: ${MYSQL_DATABASE}
      MYSQL_USER: ${MYSQL_USER}
      MYSQL_PASSWORD: ${MYSQL_PASSWORD}
    restart: always
    ports:
      - 3306:3306
    networks:
      - default
  litespeed:
    platform: linux/amd64
    image: litespeedtech/openlitespeed:${OLS_VERSION}-${PHP_VERSION}
    logging:
      driver: none
    env_file:
      - .env
    volumes:
      - ./lsws/conf:/usr/local/lsws/conf
      - ./lsws/admin-conf:/usr/local/lsws/admin/conf
      - ./bin/container:/usr/local/bin
      - ./sites:/var/www/vhosts/
      - ./acme:/root/.acme.sh/
      - ./logs:/usr/local/lsws/logs/
    ports:
      - 80:80
      - 443:443
      - 443:443/udp
      - 7080:7080
    restart: always
    environment:
      TZ: ${TimeZone}
      WP_DB_HOST: mysql
    networks:
      - default
  phpmyadmin:
    platform: linux/amd64
    image: phpmyadmin/phpmyadmin
    logging:
      driver: none
    ports:
      - 8080:80
      - 8443:443
    environment:
      PMA_HOST: mysql
    restart: always
    networks:
      - default
networks:
  default:
    driver: bridge

But printing getenv( 'WP_DB_HOST') in PHP returns false.

Any idea?

Partially solved: I used define('DB_HOST', 'mysql'); in wp-config.php and it worked.

BUT it’s strange environment variables can’t be set for litespeed container

My guess is that the PHP user doesn’t have access to the environment variables. That does seem to limit the usefulness of the image though.

1 Like

I had some problems customizing the image. I didn’t know Mac M1 was going to be that problematic.

Moved to a local instalation of Apache using brew.

I was trying to have the same environment for development, staging and production but it’s too much effort.

Unfortunately, I’ve noticed a surprising number of differences between apache and litespeed when it comes to .htaccess syntax. I think the ones I experienced were rewrites and internal redirects.

1 Like

Attach to your running instance of litespeed, print env from the console to confirm if the variables there or not

1 Like