dockerenvironment-variablessphinx

Use .env file variables during Docker build


I am trying to use the sed command to replace variables during docker build. The variable I am attempting to do (to start) is $DATABASE_HOST. The value for that is coming from my .env file. I am reading online that environment variables are only available during run time if they come from the .env file. Due to this, my sed command is not registering.

Dockerfile:

# Dockerfile for Sphinx SE
# https://hub.docker.com/_/alpine/
FROM alpine:3.12

# https://sphinxsearch.com/blog/
ENV SPHINX_VERSION 3.4.1-efbcc65

# Install dependencies
RUN apk add --no-cache mariadb-connector-c-dev \
    postgresql-dev \
    wget \
    sed

# set up and expose directories
RUN mkdir -pv /opt/sphinx/log /opt/sphinx/index
VOLUME /opt/sphinx/index

# http://sphinxsearch.com/downloads/sphinx-3.3.1-b72d67b-linux-amd64-musl.tar.gz
RUN wget http://sphinxsearch.com/files/sphinx-${SPHINX_VERSION}-linux-amd64-musl.tar.gz -O /tmp/sphinxsearch.tar.gz \
    && cd /opt/sphinx && tar -xf /tmp/sphinxsearch.tar.gz \
    && rm /tmp/sphinxsearch.tar.gz

# point to sphinx binaries
ENV PATH "${PATH}:/opt/sphinx/sphinx-3.4.1/bin"
RUN indexer -v

# redirect logs to stdout
RUN ln -sv /dev/stdout /opt/sphinx/log/query.log \
        && ln -sv /dev/stdout /opt/sphinx/log/searchd.log

# expose TCP port
EXPOSE 36307
EXPOSE 9306

# Copy base sphinx.conf file to container
VOLUME /opt/sphinx/conf
COPY ./sphinx.conf /opt/sphinx/conf/sphinx.conf

# Copy all docker sphinx.conf files
COPY ./configs/web-finder/docker/ /opt/sphinx/conf/

#  look for and replace
RUN sed -i "s+DATABASE_HOST+${DATABASE_HOST}+g" /opt/sphinx/conf/sphinx.conf

# Concat the sphinx.conf files for all apps
# RUN cat /tmp/myconfig.append >> /etc/portage/make.conf  && rm -f /tmp/myconfig.append

CMD indexer --all --config /opt/sphinx/conf/sphinx.conf \
    && searchd --nodetach --config /opt/sphinx/conf/sphinx.conf

.env file:

DATABASE_HOST=someport
DATABASE_USERNAME=someusername
DATABASE_PASSWORD=somepassword
DATABASE_SCHEMA=someschema
DATABASE_PORT=3306
SPHINX_PORT=36307

sphinx.conf:

searchd
{
  listen            = 127.0.0.1:$SPHINX_PORT
  log               = /opt/sphinx/searchd.log
  query_log         = /opt/sphinx/query.log
  read_timeout      = 5
  max_children      = 30
  pid_file          = /opt/sphinx/searchd.pid
  seamless_rotate   = 1
  preopen_indexes   = 1
  unlink_old        = 1
  binlog_path       = /opt/sphinx/
}

Solution

  • With sphinx the 'sphinx.conf' file can be 'executable'. Ie it can actully be a 'shell script' (or PHP, perl etc!)

    Assuming your .env file makes real (runtime!) environment variables within the container (not overly familiar with Docker), then your sphinx.conf file could be ...

    #!/bin/sh
    set -eu
    cat <<EOF
    searchd
    {
      listen            = 127.0.0.1:$SPHINX_PORT
      log               = /opt/sphinx/searchd.log
      query_log         = /opt/sphinx/query.log
      read_timeout      = 5
      max_children      = 30
      pid_file          = /opt/sphinx/searchd.pid
      seamless_rotate   = 1
      preopen_indexes   = 1
      unlink_old        = 1
      binlog_path       = /opt/sphinx/
    }
    EOF
    

    And because it a shell script, the variables will automatically be expanded :)

    Need it executable too!

    RUN chmod a+x /opt/sphinx/conf/sphinx.conf
    

    Then dont need the sed command in Dockerfile at all!