Skip to main content
Version: LOC v0.6 (legacy)

Add Extra Local Data Services

It is possible to add extra local data storage services in the Local Simple Runtime. A containerized test environment is great for testing and can be deleted at any time. These services can also be accessed directly via 127.0.0.1 instead of host.docker.internal.

Here we'll give you some examples: a database service (MySQL, PostgreSQL or MS SQL Server) and a FTP server (Pure-FTPd).

info

If you already have installed similar services, they may cause port conflict and render both service unusable.

Database Service

Add the following service definition to /local-runtime/docker-compose.yaml, which adds a MySQL server:

/local-runtime/docker-compose.yaml
version:
"3.9"
# ...

services:
# ...

mysql:
image: mysql
restart: "always"
container_name: mysql
environment:
- MYSQL_ROOT_USER=root
- MYSQL_ROOT_PASSWORD=1234
volumes:
- ./sql/mysql-setup.sql:/docker-entrypoint-initdb.d/mysql-setup.sql
ports:
- "3306:3306"
networks:
- saffron

The database user is root and password is 1234.

It also needs an initialize script to create data when the container starts up.

Create a file at /local-runtime/sql/mysql-setup.sql and write your scrpit, for example:

/local-runtime/sql/mysql-setup.sql
CREATE DATABASE mydb;

USE mydb;

CREATE TABLE policy (AcuNo VARCHAR(20) NOT NULL, Amount INT);

INSERT INTO policy (AcuNo, Amount) VALUES ('Acu-048', 4239);
INSERT INTO policy (AcuNo, Amount) VALUES ('Acu-049', 2022);
INSERT INTO policy (AcuNo, Amount) VALUES ('Acu-050', 9527);

This script create a database mydb and write 3 records into table policy.

Use Docker Compose to start up the db service. When the DB is up, you can connect it with database agent like this:

const MySqlParameters = new Database.MySqlParameters({
host: "127.0.0.1",
port: 3306,
database: "mydb",
username: "root",
password: "1234",
});

// ...
tip

You can access the MySQL client in the container (will prompt for user and password):

docker exec -it mysql mysql -u root -p

And then you can directly query the database, for example:

mysql> USE mydb;
mysql> SELECT * FROM policy;

+---------+--------+
| AcuNo | Amount |
+---------+--------+
| Acu-048 | 4239 |
| Acu-049 | 2022 |
| Acu-050 | 9527 |
+---------+--------+
3 rows in set (0.00 sec)

FTP Server

Add the following service definition to /local-runtime/docker-compose.yaml, which adds a Pure-FTPd server:

/local-runtime/docker-compose.yaml
version:
"3.9"
# ...

services:
# ...

ftpd_server:
image: stilliard/pure-ftpd
restart: "always"
container_name: pure-ftpd
environment:
PUBLICHOST: "127.0.0.1"
FTP_USER_NAME: admin
FTP_USER_PASS: "1234"
FTP_USER_HOME: /home/admin
volumes:
- "./ftp:/home/admin/"
ports:
- "21:21"
- "30000-30009:30000-30009"
networks:
- saffron

The service user name is admin and password is "1234".

Create a sub-directory /ftp under /local-runtime. Once the service is up, everything in /local-runtime/ftp can be accessed via the FTP server.

Then you can read /local-runtime/ftp/test.txt like this:

const url = "ftp://admin:1234@127.0.0.1:21/test.txt";

const file = await ctx.agents.fileStorage.simpleGet(url);

note

With the additional services your local directories should look like this:

/CLI
...
/local-runtime
/ftp
test.txt
...
/sql
mysql-setup.sql
...
kibana.yaml
payload-local.yaml
docker-compose.yaml