Traccar in a docker container with MySQL under DirectAdmin

For a while I was looking for a way to track my car’s whereabouts (nice word!) In order to do so, I started by looking how to have a server environment ready to capture and display this information, as it was clear I didn’t want to create a serverside solution by myself for this.

Searching the webs, I found traccar.org what appeared to be the best solution.

The end webpage of traccar on my environment

Steps completed to host this environment:

  • Create an A record under timdows.com
  • Setup a hosting account within DirectAdmin
  • Create a MySQL database to host the data in
  • Setup the docker environment and configure it
  • Configure Let’s Encrypt for the SSL certificate
  • Change nginx config to also support SSL on the 5055 traccar port for ingestion
Within DirectAdmin setting up the Let’s Encrypt certificate

Docker environment and configuration

I created a startup docker file that contains the following, it now exposed 2 ports
6080 for the admin webpage, that will be mounted to localhost, next is that nginx will use that
7070 that forwards to the ingestion for the traccar mobile client, I want this to be SSL as well in order to have secure communication

docker run \
-d --restart always \
--name traccar \
--hostname traccar \
-p 6080:8082 \
-p 7070:5055 \
-v /home/traccar/logs:/opt/traccar/logs:rw \
-v /home/traccar/traccar.xml:/opt/traccar/conf/traccar.xml:ro \
-v /home/traccar/data:/opt/traccar/data:rw \
traccar/traccar:latest

In order to get the traccar.xml file, I followed the guide on traccar/traccar-docker: Traccar Docker (github.com)
The configuration of the traccar file was changed to support connecting to a MySQL database outside a docker environment

traccar.xml configuration file
After starting, this is the running configuration

Nginx configuration

In order to forward all the https traphic to the exposed port 6080, I created the following nginx configuration.
As the traccar webclient used websockets, some additional websocket support had to be addited. Without this there would be an error connecting to the webpage, resulting in an empty tracking map.

location ~ {
    # redirect all HTTP traffic to localhost:6080
    proxy_pass http://localhost:6080;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header Host $host;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

    # WebSocket support
    proxy_http_version 1.1;
    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Connection "upgrade";
}

But this is not all, also the port 7070 needs to be managed by nginx, in order to do so, the following was added to the end of the file
/usr/local/directadmin/data/users/traccar/nginx.conf
This configuration makes it possible to configure my mobile device to send information to https://traccar.timdows.com:7071

server {
        listen 7071 ssl;
        server_name traccar.timdows.com;
        ssl_certificate /usr/local/directadmin/data/users/traccar/domains/traccar.timdows.com.cert.combined;
        ssl_certificate_key /usr/local/directadmin/data/users/traccar/domains/traccar.timdows.com.key;

        location ~ {
                proxy_pass      http://localhost:7070;
        }
}

Database and security

With the command
docker network inspect -f '{{json .IPAM.Config}}' bridge
I found the docker network and gave that (with a wildcard) access to connect to the MySQL database

Access hosts to the database
The created database tables after starting the docker container

Configure the mobile client and see movements

Change the server URL to https://traccar.timdows.com:7071

The traccar client shows in its status page then the following information after starting
These results are then immediately show in the admin page