Configuring APM server with env variables in docker-compose

Hello.

I'm trying to configure an APM server as a service in a docker-compose file. I'm doing this by setting some environment variables. This works for Elasticsearch and Kibana (although very differently, for Elasticsearch I can just usexpack.security.enabled but for Kibana I have to translate the vars to XPACK_SECURITY_ENABLED).

The thing is, I'm not letting the image's default CMD run. Instead of that, I'm running a script that waits until Elasticsearch service is up and running and then runs APM server's entrypoint. Like this:

docker-compose.yml:

  apm-server:
    container_name: apm-server_$ELASTIC_VERSION
    image: ffknob.com.br/apm-server:$ELASTIC_VERSION
    build:
      context: apm-server/
      args:
        - ELASTIC_VERSION=$ELASTIC_VERSION
    volumes:
      - shared:/shared
      - certs:/usr/share/apm-server/config/certs/:ro
    ports:
      - 8200:8200
    environment:
      - apm-server.host=apm-server:8200
      - apm-server.ssl.enaled=true
      - apm-server.ssl.key=config/certs/apm-server.key
      - apm-server.ssl.certificate=config/certs/apm-server.crt
      - apm-server.ssl.key_passphrase=$APM_SERVER_CERT_PASSWORD
      - apm-server.secret_token=$APM_SERVER_SECRET_TOKEN
      - output.elasticsearch.hosts=["elasticsearch01:9200","elasticsearch02:9200","elasticsearch03:9200"]
      - output.elasticsearch.username=apm_system
      - output.elasticsearch.password=$APM_SYSTEM_USER_PASSWORD
      - output.elasticsearch.protocol=https
    networks:
      - elastic
    depends_on:
      - elasticsearch01

Dockerfile:

ARG ELASTIC_VERSION
FROM docker.elastic.co/apm/apm-server:$ELASTIC_VERSION

COPY wait-for-bootstrap.sh /

CMD [ "/wait-for-bootstrap.sh", "/usr/local/bin/docker-entrypoint", "-e" ]

wait-for-bootstrap.sh:

#!/bin/sh

set -e

CMD="$@"
ELASTICSEARCH_BOOTSTRAP_READY_FILE=/shared/bootstrap/bootstrap.ready

if [[ ! -f ${ELASTICSEARCH_BOOTSTRAP_READY_FILE} ]]
then
    >&2 echo "Elasticsearch bootstrap not ready yet. Waiting..."

    until [ -f ${ELASTICSEARCH_BOOTSTRAP_READY_FILE} ]
    do
      sleep 1
    done
fi

>&2 echo "Elasticsearch bootstrap finished."
exec ${CMD}

Result: The APMserver runs (apm-server -e), but all the environment variables I've set in the docker-compose.yml are ignored.

So, what am I doing wrong?

Thank you!

Hello!

So, to run APM Server from a docker-compose file you basically need to put all that in the command, not in environment. Something like:

apm-server:
    container_name: apm-server_$ELASTIC_VERSION,
    command: [
        "apm-server", "-e",
        "-E", "apm-server.host=apm-server:8200",
        "-E", "apm-server.ssl.enaled=true",
        ...
    ],
    image: ...

Assuming you are running Elasticsearch from docker-compose too, you can set a condition in APM Server to wait for Elasticsearch to be ready. Like so:

depends_on: {
       elasticsearch01: {
          condition: service_healthy
       }
}

The health-check in Elasticsearch then, could look like this:

elasticsearch: {
      container_name: elasticsearch_$ELASTIC_VERSION,
      ....
      healthcheck: {
         interval: 10,
         retries: 10,
         test: [
          "CMD-SHELL",
          "curl -s http://localhost:9200/_cluster/health | grep -vq '\"status\":\"red\"'"
        ]
      }
}

Hope this helps,

Juan

1 Like

This topic was automatically closed 20 days after the last reply. New replies are no longer allowed.