simple nodeJs Api with mysql & docker
Tags: node, api, mysql, docker
Published: 2021-12-10 12:38:18

---
title: simple nodeJs Api with mysql & docker 
published: true
description: 
tags: node, api, mysql,  docker
cover_image: https://user-images.githubusercontent.com/66588352/141428835-833078e3-1047-4770-b7c8-212a5a82a490.png
---

As I struggled alot in learning how to make a simple nodejs api with mysql

& tired of using xxamp in initing the db I made a small project I hope it will be useful for lots of people learning how to create an api for the first time 

using `nodejs` & `mysql` to create a simple GET Api

you can try it by cloning the [my repo](https://github.com/mahmoud-bebars/nodejs-mysql-api-sample) and follow the instructions in the readme file 

but for me the most important thing in this project is using `docker` to create a simple server for testing purposes with a few lines of code 

you will also find it in [my repo](https://github.com/mahmoud-bebars/nodejs-mysql-api-sample) under name of `docker-compose.yml`

you can follow the instructions to use it.

you can use it with all of your projects with no time by installing docker from [here](https://www.docker.com/products/docker-desktop)
and follow the installing instructions in the `Docker Docs`

after installing create new file in your machine root call it `docker-compose.yml`, past this code in it

```
# We're using version 3.7 of the Docker Compose file format
version: "3.7"

# Define services/containers
services:
  # MySQL container
  mysql:
    # Use mysql:8.0.19 image
    image: mysql:latest
    # Connect to "my-network" network, as defined below
    ports:
      - "3306:3306"
    networks:
      - my-network
    # Pass a list of environment variables to the container
    environment:
      MYSQL_ROOT_PASSWORD: my-secret-pw

  # phpMyAdmin container
  phpmyadmin:
    # Use phpmyadmin/phpmyadmin:5.0.2 image
    image: phpmyadmin:latest
    # Connect to "my-network" network, as defined below
    networks:
      - my-network
    # Map port 8080 on the host to port 80 inside the container
    # Syntax is: "HOST_PORT:CONTAINER_PORT"
    ports:
      - "8080:80"
    # Pass a list of environment variables to the container
    environment:
      PMA_HOST: mysql
    # Wait for "mysql" container to start first
    depends_on:
      - mysql

# Define networks
networks:
  my-network:
```
init this command in the same `docker.cmpose` file location from the terminal
```
$  docker compose
```

it will simply creates a mysql db server managed by PHPmyadmin and you have the full control to edit the file or to make the db that suits your next project
as xxamp visit `localhost:8080` and enter username `root` with password `my-secret-pw` (you can change it later in the file) and congratulations you have a easy, stable db for development purposes 

this process is for the first time only after that you can init it from `docker Dashboard` & you will find it in the `Containers / Apps` section

click the start button and it will start the container with the same data bases you left it with...

I hope this post help the beginners to start their journey easy

the project [repo link](https://github.com/mahmoud-bebars/nodejs-mysql-api-sample)