Todo list backend with NestJS and MariaDB
by Daniel Lastanao and Enrique Subías
Clone the repository to your local machine
git clone https://github.com/VS-HS/Lab-Todo-List.git
Compose up the docker container with the database
docker compose -f "docker-compose.yml" up -d --build
There are 2 docker containers, one for the database and another for the backend.
Name | Value |
---|---|
Port (on local machine): | 3001 |
Port (on container): | 3306 |
User: | user |
Password: | 1234 |
Database: | todo_database |
Database URL (on local machine) | jdbc:mariadb://database:3001/todo_database |
We can see these characteristics the docker-compose.yml
as we see below:
We map the ports (This links the port 3001
of your local machine with the 3306
of the container):
ports:
- 3001:3306
Here we define the user and password with which we can enter the database:
MARIADB_USER: 'user'
MARIADB_PASSWORD: '1234'
We specify the name of the database:
MARIADB_DATABASE: 'todo_database'
The image that we use and volume where is being mounted is in the mariadb-data
directory of the container with
path /var/lib/mysql
:
database:
image: mariadb:10.9
volumes:
- ./mariadb-data:/var/lib/mysql
Name | Value |
---|---|
Port (on local machine): | 8080 |
Port (on container): | 8080 |
Database URL (container port) | jdbc:mariadb://database:3306/todo_database |
Backend URL | http://localhost:8080/api |
We map the ports (This links the port 8080
of your local machine with the 8080
of the container):
ports:
- 8080:8080
All of the TYPEORM_***
are variables of a app.module.ts
file. ORM
it is a library that allows for
the manipulation of a database. It also allows for the injection of a database connection through the use
of InjectRepository
Here are the definitions of the variables like Hostname
, Port
, URL
with all the database connection
information, username
and password
of the database:
- TYPEORM_HOST=database
- TYPEORM_PORT=3306
- TYPEORM_URL=jdbc:mariadb://database:3306/todo_database
- TYPEORM_USERNAME=user
- TYPEORM_PASSWORD=1234
It ensures that the database service is up and running before starting the current service:
depends_on:
- database
And finally we have the lines where we define the path to the directory on the host machine that contains the files that
will be used to build the image
and the name of the Dockerfile
to use, that should be in context.
build:
context: ./backend-todo-daniel-enrique
dockerfile: Dockerfile
For development purposes, we have created a docker-compose.dev.yml
file that allows us to only the database in a
container and run the backend locally with the following commands:
cd backend-todo-daniel-enrique
npm install
npm run start:dev
This way we can see the changes in real time without having to rebuild the container every time we make a change.