Comprehensive Tutorial Setup Docker Compose MySQL Node.js for Full-Stack Development

In the modern era of software engineering, the phrase “it works on my machine” has become a relic of the past. If you are struggling with inconsistent environments or complex dependency management, this tutorial setup docker compose mysql node js is designed specifically for you. By containerizing your application, you ensure that your development, staging, and production environments remain identical, drastically reducing bugs and deployment friction.

Docker Compose simplifies the process of managing multi-container applications. Instead of manually starting a database, setting up a runtime environment, and linking them via IP addresses, you can define your entire stack in a single YAML file. This guide provides a deep dive into creating a robust Node.js application backed by a MySQL database, all running within the Docker ecosystem.

Table of Contents

Why Use Docker Compose for Node.js and MySQL?

Before diving into the tutorial setup docker compose mysql node js, it is essential to understand why this stack is so prevalent in the industry. Node.js offers non-blocking I/O and high scalability, while MySQL remains one of the most trusted relational database management systems (RDBMS) globally. According to recent developer surveys, over 50% of professional developers use Docker to manage their local development environments.

Docker Compose acts as the orchestrator. It allows you to define the network, the volumes for data storage, and the environment variables that both services need to communicate. Without Compose, you would need to run multiple docker run commands with complex arguments, increasing the likelihood of human error.

“Docker Compose is the glue that holds your microservices together, providing a predictable and reproducible environment for every developer on your team.”

Prerequisites and System Requirements

To follow this tutorial setup docker compose mysql node js, ensure your system meets the following requirements:

  • Docker Desktop: Installed and running (includes Docker Engine and Docker Compose).
  • Node.js: Installed locally (for initial package setup, though Docker will handle the runtime).
  • Code Editor: VS Code or any text editor of your choice.
  • Basic Knowledge: Familiarity with JavaScript and basic SQL commands.

Setting Up the Project Structure

Organizing your files correctly is the first step toward a maintainable codebase. Create a new directory for your project and navigate into it:

mkdir node-mysql-docker && cd node-mysql-docker

Your project structure should eventually look like this:

  • /node-mysql-docker
    • /src
      • index.js
    • Dockerfile
    • docker-compose.yml
    • package.json
    • .env
    • .dockerignore

Step 1: Creating the Node.js Application

First, initialize your Node.js project. Run npm init -y to create a default package.json. Next, install the necessary dependencies: Express for the web server and mysql2 for database connectivity.

npm install express mysql2 dotenv

Now, create the src/index.js file. This script will attempt to connect to the MySQL container and serve a simple API endpoint. In this tutorial setup docker compose mysql node js, we use the service name defined in the Compose file as the hostname for the database connection.


const express = require('express');
const mysql = require('mysql2');
const app = express();

const connection = mysql.createConnection({
  host: process.env.DB_HOST || 'db',
  user: process.env.DB_USER || 'root',
  password: process.env.DB_PASSWORD || 'password',
  database: process.env.DB_NAME || 'testdb'
});

app.get('/', (req, res) => {
  connection.query('SELECT "Hello from MySQL" AS message', (err, results) => {
    if (err) res.status(500).send(err);
    else res.send(`Node.js says: Hello! Database says: ${results[0].message}`);
  });
});

app.listen(3000, () => console.log('Server running on port 3000'));

Step 2: Crafting the Optimized Dockerfile

The Dockerfile is the blueprint for your Node.js container. For this tutorial setup docker compose mysql node js, we will use a multi-stage approach or a lightweight Alpine image to keep the image size small and secure.

Create a file named Dockerfile (no extension):


# Use the official Node.js image
FROM node:18-alpine

# Set the working directory
WORKDIR /usr/src/app

# Copy package files first to leverage Docker cache
COPY package*.json ./

# Install dependencies
RUN npm install

# Copy the rest of the application code
COPY . .

# Expose the application port
EXPOSE 3000

# Command to run the app
CMD ["node", "src/index.js"]

Don’t forget to create a .dockerignore file to prevent node_modules from being copied into the image, which speeds up the build process:

node_modules
npm-debug.log
.env

Step 3: Writing the Docker Compose Configuration

The docker-compose.yml file is where the magic happens. It defines two services: app (our Node.js code) and db (the MySQL database). This is the core of our tutorial setup docker compose mysql node js.


version: '3.8'

services:
  db:
    image: mysql:8.0
    restart: always
    environment:
      MYSQL_DATABASE: testdb
      MYSQL_ROOT_PASSWORD: password
    ports:
      - "3306:3306"
    volumes:
      - db_data:/var/lib/mysql

  app:
    build: .
    ports:
      - "3000:3000"
    environment:
      DB_HOST: db
      DB_USER: root
      DB_PASSWORD: password
      DB_NAME: testdb
    depends_on:
      - db

volumes:
  db_data:

Note the depends_on field. It tells Docker to start the db container before the app container. However, it doesn’t wait for MySQL to be “ready” (fully initialized), which we will address later.

Step 4: Managing Environment Variables Safely

In a real-world scenario, you should never hardcode credentials. Use a .env file. This tutorial setup docker compose mysql node js emphasizes security by recommending environment abstraction.

Create a .env file:


MYSQL_ROOT_PASSWORD=securepassword
MYSQL_DATABASE=myappdb
DB_HOST=db
DB_PORT=3306

Then, update your docker-compose.yml to use these variables using the ${VARIABLE_NAME} syntax. This ensures that your secrets are not committed to version control.

Step 5: Launching and Testing the Stack

With everything configured, it’s time to bring the stack to life. Open your terminal and run:

docker-compose up --build

The --build flag ensures that Docker rebuilds your Node.js image if you made changes to the code. Once the logs show that both services are running, open your browser and navigate to http://localhost:3000. You should see the message: “Node.js says: Hello! Database says: Hello from MySQL”.

Ensuring Data Persistence with Docker Volumes

One common mistake beginners make is losing data when the container stops. In this tutorial setup docker compose mysql node js, we use named volumes. The line db_data:/var/lib/mysql in the Compose file ensures that even if you delete the MySQL container, your data remains safe on the host machine’s disk.

To verify this, stop the containers (docker-compose down), start them again, and notice that your database state remains intact. This is critical for any production-grade application.

Troubleshooting Common Connection Issues

Connecting Node.js to MySQL in Docker can sometimes be tricky. Here are the most common issues and how to fix them:

  • ECONNREFUSED: This usually happens because the Node.js app tries to connect before the MySQL service is fully initialized. MySQL takes a few seconds to set up its internal schemas. Use a tool like wait-for-it.sh or implement a retry logic in your Node.js code.
  • Authentication Plugin Error: MySQL 8 uses caching_sha2_password by default, which some older Node.js drivers don’t support. You can fix this by adding command: --default-authentication-plugin=mysql_native_password to your db service in the Compose file.
  • Port Conflicts: If you already have MySQL installed locally on port 3306, change the host port in Compose to "3307:3306".

Best Practices for Production Deployment

While this tutorial setup docker compose mysql node js covers the basics, moving to production requires additional steps:

  1. Use Specific Image Tags: Avoid node:latest. Use node:18.16.0-alpine to ensure build reproducibility.
  2. Health Checks: Add a healthcheck section to your MySQL service so Docker knows when the database is truly ready.
  3. Resource Limits: Define CPU and Memory limits in your Compose file to prevent one container from crashing the entire server.
  4. Logging: Configure Docker logging drivers (like json-file or syslog) to manage log rotation and prevent disk space issues.

Conclusion and Next Steps

Congratulations! You have successfully completed this tutorial setup docker compose mysql node js. You now possess a fully containerized environment that is portable, scalable, and easy to manage. By following these steps, you’ve bridged the gap between development and production, ensuring that your application behaves consistently regardless of where it is deployed.

As a next step, consider adding a Redis container for caching or a Nginx container as a reverse proxy. The beauty of Docker Compose is that adding new services is as simple as adding a few lines to your YAML file. Keep exploring, keep containerizing, and happy coding!

Tinggalkan komentar