DEV Community

Cover image for simple nodeJs Api with mysql & docker
m.bebars
m.bebars

Posted on

simple nodeJs Api with mysql & docker

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 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 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
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:
Enter fullscreen mode Exit fullscreen mode

init this command in the same docker.cmpose file location from the terminal

$  docker compose
Enter fullscreen mode Exit fullscreen mode

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

Top comments (0)