traccar-docker icon indicating copy to clipboard operation
traccar-docker copied to clipboard

Support for environment variables and files

Open Abyss777 opened this issue 2 years ago • 5 comments

I'm not sure if this is correct repo, but it is more related to docker.

It would be good to add support server configuration via environment variables, especially database config. Editing config file is not that usual flow used by docker.

Also it would be good to support file variables for secrets https://docs.docker.com/engine/swarm/secrets/

Abyss777 avatar Oct 14 '22 06:10 Abyss777

It is actually already supported.

Nice to see you back 😉

tananaev avatar Oct 14 '22 12:10 tananaev

Oh, my bad, have not checked the code, only documentation. But i think, secret files is not implemented yet ?

I too like your project to forget about it :)

Abyss777 avatar Oct 14 '22 12:10 Abyss777

Yeah, not the secret files.

tananaev avatar Oct 14 '22 12:10 tananaev

Here is my workaround for secret files with MySQL DB

deploy docker swarm command

docker stack deploy -c ./traccar.yaml traccar

traccar.yaml

version: "3.9"

services:
  db:
    image: mysql
    ports:
      - 3306:3306
    environment:
      TZ: "Asia/Yekaterinburg"
      MYSQL_ROOT_PASSWORD_FILE: /run/secrets/dbpwd-root
      MYSQL_DATABASE: traccar
      MYSQL_USER: traccar
      MYSQL_PASSWORD_FILE: /run/secrets/dbpwd-traccar
    command: --character-set-server=utf8mb4 --collation-server=utf8mb4_unicode_ci --skip-log-bin
    volumes:
      - ./db/data:/var/lib/mysql:rw
    secrets:
      - dbpwd-root
      - dbpwd-traccar
  traccar:
    image: traccar/traccar:ubuntu
    ports:
      - 8082:8082
      - 5000-5245:5000-5245
      - 5000-5245:5000-5245/udp
    environment:
      CONFIG_USE_ENVIRONMENT_VARIABLES: "true"
      DATABASE_DRIVER: "com.mysql.cj.jdbc.Driver"
      DATABASE_URL: "jdbc:mysql://db:3306/traccar?allowPublicKeyRetrieval=true&useSSL=false&allowMultiQueries=true&autoReconnect=true&useUnicode=yes&characterEncoding=UTF-8&sessionVariables=sql_mode=''"
      DATABASE_USER: traccar
      DATABASE_PASSWORD_FILE: /run/secrets/dbpwd-traccar
    volumes:
      - ./traccar/logs:/opt/traccar/logs:rw
      - ./traccar/entrypoint.sh:/usr/local/bin/entrypoint.sh:ro
      - /etc/timezone:/etc/timezone:ro
      - /etc/localtime:/etc/localtime:ro
    entrypoint: /usr/local/bin/entrypoint.sh
    depends_on:
      - db
    secrets:
      - dbpwd-traccar
secrets:
   dbpwd-root:
     file: secrets/db_root_password
   dbpwd-traccar:
     file: secrets/db_traccar_password

The workaround itself, fill password environment variables from secret files. content of ./traccar/entrypoint.sh

#!/bin/bash
## Set environment variables by their respective secrets
supportedSecrets=( "DATABASE_PASSWORD"
                   "MAIL_SMTP_PASSWORD"
                   "LDAP_PASSWORD"
                 )
for secret in ${supportedSecrets[@]}; do
    envFile="${secret}_FILE"
    if [ $(printenv ${envFile}) ]; then envFileName=`printenv ${envFile}`; fi
     if [[ ${!envFile} && -f "$envFileName" ]]; then
         val=`cat $envFileName`
         export "${secret}"="$val"
         echo "${secret} environment variable was set by secret ${envFile}"
     fi
done

java -Xms1g -Xmx1g -Djava.net.preferIPv4Stack=true -jar tracker-server.jar conf/traccar.xml

DB passwords are stored in secrets/db_root_password and secrets/db_traccar_password

The only problem that we need bash.

Abyss777 avatar Oct 21 '22 10:10 Abyss777

Any update on this? When can we use docker secret without having to use a script as a workaround?

SebastEnn avatar May 27 '24 15:05 SebastEnn