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

Docker-compose support

Open bttd opened this issue 2 years ago • 17 comments

Hi,

It is possible to provide an official docker-compose file to run this docker?

Regards

bttd avatar Oct 27 '22 12:10 bttd

Depends on your needs. An example with traccar + separate MariaDB.

version: '2.4'

services:
  mariadb:
    container_name: mariadb
    hostname: mariadb
    restart: unless-stopped
    image: yobasystems/alpine-mariadb
    volumes:
      - ./mariadb:/var/lib/mysql
    environment:
      TZ: Europe/Amsterdam
      #MYSQL_RANDOM_ROOT_PASSWORD: "yes"
      MYSQL_ROOT_PASSWORD: <root_password>
      MYSQL_DATABASE: <username>
      MYSQL_USER: <username>
      MYSQL_PASSWORD: <password>
    ports:
      - 3306:3306
    healthcheck:
      test:  mysqladmin ping -h 127.0.0.1 -u $$MYSQL_USER --password=$$MYSQL_PA$
      interval: 60s
      timeout: 5s
      retries: 5
      #start_period: 30s
    networks:
    - Iot

  traccar:
    container_name: traccar
    hostname: traccar
    restart: unless-stopped
    image: traccar/traccar:4.15-ubuntu
    ports:
    - 5002:5002/tcp
    - 5002:5002/udp
    - 82:8082
    volumes:
     - ./traccar/conf/traccar.xml:/opt/traccar/conf/traccar.xml
     - ./traccar/logs:/opt/traccar/logs:rw
    depends_on:
      - mariadb
    networks:
    - Iot

networks:
  Iot:
    driver: bridge
    enable_ipv6: false
    ipam:
      config:
        - subnet: 10.0.1.0/29

sincze avatar Nov 03 '22 10:11 sincze

Many thanks for sharing the Docker compose, I’m still learning Docker would you be able to explain a few things.

  1. the networks part at the bottom ? I’d like my instance to be on the same local network (LAN) as all my other devices which is 192.168.1.1 ? Do I simply change the IP ?

  2. for the local volumes, where do i find the ‘ traccar.xml’ file ?

  3. when it comes the mariadB, where do I find all the required passwords, root specifically or do I just assign them anything ?

nodecentral avatar Nov 26 '22 09:11 nodecentral

@sincze thanks for the compose file

To make your answer a bit more complete I'll add what I had to do beyond using your compose.

docker network inspect Iot

under "Name": "Iot",we need "Subnet": "10.0.1.0/29"

and under "Iot", we need "IPv4Address": " 10.0.1.2",

subnet goes to

config: - subnet: 10.0.1.0/29 in docker-compose

and

IPv4Address goes to jdbc:mysql:// 10.0.1.2:3306/traccar-db in /opt/traccar/conf/traccar.xml:ro (.traccar/conf/traccar.xmll in the example)

open a root mysql shell

docker exec -it mariadb mysql -u root -p

type your root password

Hit enter

CREATE DATABASE IF NOT EXISTS traccar-db

hit enter

grant all privileges on `db_name`.* TO `user_name'@'%` identified by `root_pass`

hit enter

flush privileges

hit enter

ALTER DATABASE traccar CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci

hit enter

\q

hit enter

docker-compose up -d

ippocratis avatar Dec 05 '22 20:12 ippocratis

No problem. Great you liked it. That is why I share to help others!

I think the DB was created automatically, but it was some time ago. Can do a check if you want on a spare docker machine.

sincze avatar Dec 05 '22 22:12 sincze

Yeah I actually thought that docker-compose should create user and database My networking databases and docker knowledge is pretty basic though And I tried many things to spin up the containers

(My issue is here https://github.com/traccar/traccar-docker/issues/103#issuecomment-133708941)

So its likely that I miss estimated things

ippocratis avatar Dec 06 '22 12:12 ippocratis

Hi @ippocratis

By any chance, is there an updated compose file you could post?

Also, as I want to have this instance on the same network as my primary LAN, do I just change the ip4 subnet to my ip, e.g. 192.168.1.1/29 ?

nodecentral avatar Dec 07 '22 08:12 nodecentral

@nodecentral

docker network ls verify the the network name as.provided in the docker-compose is running for example traccar-trc

docker network inspect traccar_trc

under "Name": "traccar_trc",we need "Subnet": "192.168.112.0/20"

and under "traccar-db", we need "IPv4Address": "192.168.112.2/20",

subnet goes to

config: - subnet: 192.168.112.0/20 in docker-compose

and

IPv4Address goes to jdbc:mysql://192.168.112.2:3306/traccar-db Like jdbc:mysql://docker-bridge-ipv4:container-internal-port/database-name

in /opt/traccar/conf/traccar.xml:ro

So you will actually use the docker internal bridge ip's not the host

My docker-compose is no diferent than what @sincze provided

I just used the debian arm64 image instead of Ubuntu

Used the android ports as I only use the android client

in traccar hostname I used my ddns domain name which I use to access traccar from outside my lan

Added command: --default-authentication-plugin=mysql_native_password as I want yo add user/db

and added MYSQL_DATABASE=, MYSQL_USER= , MYSQL_PASSWORD= all to traccar-db environment together with MYSQL_ROOT_PASSWORD=

All with trial and error

ippocratis avatar Dec 07 '22 10:12 ippocratis

The compose.yml for reference

version: "3"

services:
    traccar-db:
        image: yobasystems/alpine-mariadb
        container_name: traccar-db
        command: --default-authentication-plugin=mysql_native_password
        restart: always
        volumes:
            - /run/media/ippo/TOSHIBA/traccar/mysql-data:/var/lib/mysql
            - /run/media/ippo/TOSHIBA/traccar/mysql:/etc/mysql/conf.d
        ports:
            - "3306:3306"
        environment:
            - MYSQL_ROOT_PASSWORD=xxx
            - MYSQL_DATABASE=traccar-db
            - MYSQL_USER=xxx
            - MYSQL_PASSWORD=xxx
        networks:
            - trc2
            
    traccar:
        image: traccar/traccar:debian
        hostname: XXX.XXX.com
        container_name: traccar
        depends_on:
           - traccar-db
        restart: always
        volumes:
            - /run/media/ippo/TOSHIBA/traccar/conf/traccar.xml:/opt/traccar/conf/traccar.xml:ro
            - /run/media/ippo/TOSHIBA/traccar/logs:/opt/traccar/logs:rw
        ports:
            - "5055:5055"
            - "82:8082"
        networks:
            - trc2
            
networks:
  trc2:
    driver: bridge
    enable_ipv6: false
    ipam:
      config:
        - subnet: 192.168.112.0/20

ippocratis avatar Dec 07 '22 15:12 ippocratis

And also not directly related but it was the main reason I wanted to move from embedded h2 db to an external

To backup traccar with a date timestamp in the SQL file in order to have db "snapshots"

docker-compose -f  compose-file.yml exec dbname mysqldump -uroot -pYOUR_MARIADB_ROOT_PASSWORD --all-databases > dump-$(date +%F_%H-%M-%S).sql

And restore

docker-compose -f compose-file.yml exec -T dbname mysql -uroot -pYOUR_MARIADB_ROOT_PASSWORD  < mariadb-dump.sql

ippocratis avatar Dec 07 '22 15:12 ippocratis

volumes: mysql-data: mysql: logs:

Dont think you need these with your current mounting options. And I think the code shows an additonal '-' to much :)

sincze avatar Dec 07 '22 16:12 sincze

@sincze cleaned it up thanks

ippocratis avatar Dec 07 '22 17:12 ippocratis

Also tried the official mariadb image but had innodb general errors maybe it needs removing containers and volumes i'm not sure I'll do some tests later at some point. I like the yobasystems alpine-mariadb image as its more minimal but the official looks more frequently updated

ippocratis avatar Dec 07 '22 18:12 ippocratis

Hi guys, I try to deploy traccar latest version on docker and figure some issues mentionned aboved. Anyway, the connection to the database work only with a static IP in my case. This is my compose file if it can help.

version: "3"

services:
    traccar-db:
        image: yobasystems/alpine-mariadb
        container_name: traccar-db
        command: --default-authentication-plugin=mysql_native_password
        restart: always
        ports:
            - "3306:3306"
        environment:
            - MYSQL_ROOT_PASSWORD=XXXXX
            - MYSQL_DATABASE=YYYYYY
            - MYSQL_USER=XXXXX
            - MYSQL_PASSWORD=XXXXX
        networks:
            gps:
              ipv4_address: 192.168.55.9

    traccar:
        image: traccar/traccar:latest
        container_name: traccar
        depends_on:
           - traccar-db
        restart: always
        configs:
        - source: traccar_xml
          target: /opt/traccar/conf/traccar.xml
        volumes:
          - type: bind
            source: ./traccar/logs
            target: /opt/traccar/logs
        ports:
            - "8090:8082"
        environment:
           - MYSQL_DATABASE=YYYYYY
           - MYSQL_USER=XXXXX
           - MYSQL_PASSWORD=XXXXX
        networks:
            gps:
              ipv4_address: 192.168.55.10

networks:
  gps:
    driver: bridge
    ipam:
      driver: default
      config:
        - subnet: 192.168.55.0/24
          gateway: 192.168.55.1
configs:
  traccar_xml:
    file: traccar.xml

and the config file

<entry key='database.driver'>com.mysql.cj.jdbc.Driver</entry>
    <entry key='database.url'>jdbc:mysql://192.168.55.9:3306/YYYYYY?serverTimezone=UTC&amp;allowPublicKeyRetrieval=true&amp;useSSL=true&amp;allowMultiQueries=true&amp;autoReconnect=true&amp;useUnicode=yes&amp;characterEncoding=UTF-8&amp;sessionVariables=sql_mode=''</entry>
    <entry key='database.user'>XXXXX</entry>
    <entry key='database.password'>XXXXX</entry>

ionosphere avatar Mar 16 '23 07:03 ionosphere

I'm trying to implement the same idea but with them segregated from my Synology NAS using macvlans although I'm running into errors with the traccar container saying they cant reach the data base. Any ideas as to what could be wrong here? traccarlogs.csv

logs look like they cycle '[Guice/ErrorInCustomProvider]: HikariPool$PoolInitializationException: Failed to initialize pool: Could not create connection to database server. Attempted reconnect 3 times. Giving up.' and end with 'Caused by: java.net.ConnectException: Connection refused (Connection refused)'

services:
    traccar-db:
        image: yobasystems/alpine-mariadb
        container_name: traccar-db
        command: --default-authentication-plugin=mysql_native_password
        restart: unless-stopped
        volumes:
            - /volume1/docker/DockerComposeTesting/mariadb:/var/lib/mysql
        ports:
            - "3306:3306"
        environment:
            - MYSQL_ROOT_PASSWORD=XXXX
            - MYSQL_DATABASE=XXXX
            - MYSQL_USER=XXXX
            - MYSQL_PASSWORD=XXXX
        networks:
            mac:
              ipv4_address: 192.168.2.2

    traccar:
        image: traccar/traccar:latest
        container_name: traccar
        depends_on:
           - traccar-db
        restart: unless-stopped
        volumes:
           - /volume1/docker/DockerComposeTesting/logs:/opt/traccar/logs:rw
           - /volume1/docker/DockerComposeTesting/traccar.xml:/opt/traccar/conf/traccar.xml:ro
        ports:
            - "8090:8082"
        environment:
           - MYSQL_DATABASE=XXXX
           - MYSQL_USER=XXXX
           - MYSQL_PASSWORD=XXXX
        networks:
            mac:
              ipv4_address: 192.168.2.3

networks:
  mac:
    driver: macvlan
    driver_opts:
        parent: ovs_eth1
    ipam:
      config:
        - subnet: 192.168.2.1/24
          gateway: 192.168.2.1
          ip_range: 192.168.2.3/30

traccar xml file:

<entry key='database.driver'>com.mysql.cj.jdbc.Driver</entry>
    <entry key='database.url'>jdbc:mysql://192.168.2.2:3306/XXXX?serverTimezone=UTC&amp;allowPublicKeyRetrieval=true&amp;useSSL=true&amp;allowMultiQueries=true&amp;autoReconnect=true&amp;useUnicode=yes&amp;characterEncoding=UTF-8&amp;sessionVariables=sql_mode=''</entry>
    <entry key='database.user'>XXXX</entry>
    <entry key='database.password'>XXXX</entry>

resU123321 avatar Jan 11 '24 04:01 resU123321