traccar / traccar-docker

Traccar Docker
298 stars 145 forks source link

Docker-compose support #101

Open bttd opened 2 years ago

bttd commented 2 years ago

Hi,

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

Regards

sincze commented 2 years ago

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
nodecentral commented 1 year ago

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 ?

ippocratis commented 1 year ago

@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:

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

sincze commented 1 year ago

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.

ippocratis commented 1 year ago

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

nodecentral commented 1 year ago

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 ?

ippocratis commented 1 year ago

@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:

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 commented 1 year ago

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 commented 1 year ago

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
sincze commented 1 year ago

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 :)

ippocratis commented 1 year ago

@sincze cleaned it up thanks

ippocratis commented 1 year ago

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

ionosphere commented 1 year ago

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>
resU123321 commented 10 months ago

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>
mrskizzex commented 5 months ago

Here is docker compose I'm using for my traccar instance. Works rock solid using PostgreSQL.

docker-compose.yml

services:
  traccar_postgresql:
    image: docker.io/library/postgres:16-alpine
    container_name: traccar_postgresql
    restart: unless-stopped
    healthcheck:
      test:
        - CMD-SHELL
        - pg_isready -d $${POSTGRES_DB} -U $${POSTGRES_USER}
      start_period: 20s
      interval: 30s
      retries: 5
      timeout: 5s
    volumes:
      - /mnt/data/traccar/database:/var/lib/postgresql/data
    environment:
      - POSTGRES_PASSWORD=${POSTGRES_PASSWORD}
      - POSTGRES_USER=${POSTGRES_USER}
      - POSTGRES_DB=${POSTGRES_DB}
    networks:
      - backend
  traccar:
    image: traccar/traccar:6-alpine
    container_name: traccar
    restart: unless-stopped
    volumes:
      - /mnt/data/traccar/logs:/opt/traccar/logs:rw
      - /mnt/data/traccar/traccar.xml:/opt/traccar/conf/traccar.xml:ro
    depends_on:
      - traccar_postgresql
    ports:
      - 5013:5013 # I only use this port for my tracker
    labels:
      - traefik.enable=true
      - traefik.http.routers.traccar.rule=Host(`traccar.local.xxx.com`)
      - traefik.http.routers.traccar.entrypoints=https
      - traefik.http.routers.traccar.tls=true
      - traefik.http.routers.traccar.middlewares=secured@file
      - traefik.http.services.traccar.loadbalancer.server.port=8082
      - traefik.docker.network=frontend
    networks:
      - backend
      - frontend
networks:
  frontend:
    external: true
  backend:
    external: true

.env file

POSTGRES_PASSWORD=XXXXXXXXXXXXX
POSTGRES_USER=traccar-admin
POSTGRES_DB=traccar-db

traccar.xml

<entry key='database.driver'>org.postgresql.Driver</entry>
<entry key='database.url'>jdbc:postgresql://traccar_postgresql/traccar-db</entry>
<entry key='database.user'>traccar-admin</entry>
<entry key='database.password'>XXXXXXXXXXXXX</entry>
Dune4 commented 5 months ago

This is great - @mrskizzex could you share your sanitized Traefik Middleware file too? I think thats the part im getting stuck on. Thanks

mrskizzex commented 5 months ago

Well sure but it's really up to you how to set up traefik. I followed this tutorial how to set it up https://www.youtube.com/watch?v=n1vOfdz5Nm8 config.txt traefik.txt