Install Traccar on AWS Docker with RDS

This guide will show you how to create an instance of Traccar on an Amazon Web Services Docker.

We will not use the inbuilt Traccar H2 database, but rather use a remote MySQL database, created within AWS RDS.

Table of Contents

Prerequisites

Prerequisite #1 – Launch AWS Linux AMI with Docker

You need launch an Amazon AWS Linux AMI and install Docker

Docker is what we are going to use to host the Docker container which contains Traccar.

If you don’t know what Docker is, it’s recommended that you research it first. It will help you to understand this entire process.

Prerequisite #2 – Create an Amazon RDS MySQL instance

Our Docker container which hosts Traccar will connect to our Amazon RDS MySQL instance.

Follow the guide below on how to configure a MySQL database on Amazon AWS.

Prerequisite #3 – Allow inbound ports

Go into Amazon EC2, navigate to the security group and allow the below ports inbound:

  • 80 – Single port TCP
  • 8082 – Single port TCP
  • 5000-5150 – Port range TCP
  • 5000-5150 – Port range UDP

Prerequisite #4 – Setting the VPC

Both the EC2 server hosting Docker and the RDS instance need to be in the same Virtual Private Cloud (VPC).

Traccar will not be allowed inbound to the RDS MySQL instance, even though port 3306 is allowed in from anywhere.

  1. Navigate to the RDS instances page
  2. Select the DB instance and drill in to see details
  3. Click on the security group id
  4. Navigate over to the Inbound tab and choose Edit
  5. And ensure there is a rule of type MySQL/Aurora with source Custom
  6. When entering the custom source, just start typing in the name of the ECS cluster and the security group name will be auto-completed for you

SSH into the Linux instance

Log into your Linux server using your SSH keys.

Sudo as root

sudo su

Create Docker working directory

mkdir -p /var/docker/traccar/logs

Get default traccar.xml file

This file is where we will set the database information.

docker run \
--rm \
--entrypoint cat \
traccar/traccar:latest \
/opt/traccar/conf/traccar.xml > /var/docker/traccar/traccar.xml

Configure Traccar to use RDS

Traccar is set up to use the ‘H2’ database. We don’t want to use H2, we want to use MySQL.

Use vi to edit the traccar.xml file

vi /var/docker/traccar/traccar.xml

Find the below H2 database lines within the traccar.xml files

<entry key='database.driver'>org.h2.Driver</entry>
<entry key='database.url'>jdbc:h2:./data/database</entry>
<entry key='database.user'>sa</entry>
<entry key='database.password'></entry>

Replace the above lines, with the MySQL lines below.

Note, that you will need to enter in the database.url, database.user & database.password for your own MySQL RDS database instance.

The amazon documents link below explains how to find this information. You can also refer to Example explained below to see what you need to edit within the below lines.

https://docs.aws.amazon.com/elasticbeanstalk/latest/dg/java-rds.html

<entry key='database.driver'>com.mysql.cj.jdbc.Driver</entry>
<entry key='database.url'>jdbc:mysql://mysql-traccar-docker.dk2kslxku8d.ap-southeast-2.rds.amazonaws.com:3306/production?serverTimezone=UTC&amp;useSSL=false&amp;allowMultiQueries=true&amp;autoReconnect=true&amp;useUnicode=yes&amp;characterEncoding=UTF-8&amp;sessionVariables=sql_mode=''</entry>
<entry key='database.user'>mysqlUsername</entry>
<entry key='database.password'>mysqlPassword</entry>

Example explained:

RDS_HOSTNAME = mysql-traccar-docker.dk2kslxku8d.ap-southeast-2.rds.amazonaws.com
RDS_DB_NAME = production
RDS_USERNAME = mysqlUsername
RDS_PASSWORD = mysqlPassword

Run Traccar Docker

Now that the traccar configuration file has been edited to point to the rds mysql instance, the Traccar docker can be started.

docker run \
-d --restart always \
--name traccar \
--hostname traccar \
-p 80:8082 \
-p 5000-5150:5000-5150 \
-p 5000-5150:5000-5150/udp \
-v /var/docker/traccar/logs:/opt/traccar/logs:rw \
-v /var/docker/traccar/traccar.xml:/opt/traccar/conf/traccar.xml:ro \
traccar/traccar:latest

Check the logs for any issues

Whilst the container is starting, it’s a good idea to monitor the logs for any errors that are preventing the service from running.

tail -f -n 300 /var/log/traccar/traccar-server.log

Browse to your Traccar Docker server.

You can get the IP address of your Amazon Linux AMI from the EC2

No need to specify a port, as port 80 is mapped to 8082 within Docker.

Leave a Reply