,

Using Express.js and MySQL with Docker Compose

Posted by






Docker Compose with Express.js and MySQL

Docker Compose with Express.js and MySQL

Docker Compose is a tool for defining and running multi-container Docker applications. It allows you to define the services that make up your app in a simple YAML file, and use that file to create and start all of the services with a single command. In this article, we will explore how to use Docker Compose to set up a development environment for an Express.js app with a MySQL database.

Setting up the Docker Compose file

First, create a new directory for your project and add a file named docker-compose.yml. This file will define the services that make up your app. Here is an example of a simple Docker Compose file for an Express.js app with a MySQL database:

    
version: '3'
services:
  web:
    image: 'node:14'
    working_dir: /app
    volumes:
      - .:/app
    ports:
      - '3000:3000'
    environment:
      - NODE_ENV=development
    command: 'npm start'
    depends_on:
      - db
  db:
    image: 'mysql:5.7'
    environment:
      - MYSQL_ROOT_PASSWORD=password
      - MYSQL_DATABASE=app
      - MYSQL_USER=user
      - MYSQL_PASSWORD=password
    ports:
      - '3306:3306'
  
  

In this file, we define two services: web and db. The web service uses the node:14 image to run our Express.js app, and the db service uses the mysql:5.7 image to run the MySQL database. We also define the necessary environment variables for the MySQL database, and we map the ports for both services.

Running the Docker Compose services

With the Docker Compose file in place, you can now start the services by running the following command in the terminal:

    
$ docker-compose up
    
  

This command will create and start the containers for the web and db services, and your Express.js app should be accessible at http://localhost:3000.

Conclusion

Docker Compose is a powerful tool for defining and running multi-container Docker applications. In this article, we have demonstrated how to use Docker Compose to set up a development environment for an Express.js app with a MySQL database. With Docker Compose, you can quickly and easily define the services that make up your app and start them with a single command, making it a great tool for local development and testing.


0 0 votes
Article Rating
5 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Diki Agung Mustaqim
11 months ago

Bang, kalo pake docker ini berat ga?
Lebih berar dibandingkan running app tanpa docker maksudnya

XzanTV
11 months ago

bang mo nanya, 
ketika di build ulang data yang di db kan ilang, biar ga ilang gimana tuh bang?
Makasi

Abu Hanifah Ihsan
11 months ago

auto subscribe

bocahpetir
11 months ago

makasih banyak bang, ditunggu video yang auto buildnya ehe 🙂

Ujang Aripin
11 months ago

makasih yah bang, disini belajar yang gak diajarkan di kampus