DEV Community

Discussion on: How to Create Dockerized NodeJS with MySQL Database

Collapse
 
frasnym profile image
Frastyawan Nym

can u share your code?

Collapse
 
maxcrazy profile image
Maxcrazy1

Sure, thx.

Dockerfile

FROM node:14

# Create app directory
WORKDIR /usr/src/catidu-api

# Install app dependencies
# A wildcard is used to ensure both package.json AND package-lock.json are copied
# where available (npm@5+)
COPY . .

RUN npm install -g serverless@2.68.0
RUN npm install
# If you are building your code for production
# RUN npm ci --only=production

# Bundle app source

EXPOSE 3000

CMD [ "npm","start" ]
Enter fullscreen mode Exit fullscreen mode

docker-compose.yml

version: '3.8'
services:
  mysqldb:
    image: mysql
    restart: always
    env_file: ./.env
    environment:
      - MYSQL_ROOT_PASSWORD=$DB_PASSWORD_DEVELOPMENT
      - MYSQL_DATABASE=$DB_NAME_DEVELOPMENT
    ports:
      - $DB_LOCAL_PORT:$DB_DOCKER_PORT
    volumes:
      - db-config:/etc/mysql
      - db-data:/var/lib/mysql
      - ./db/backup/files/:/data_backup/data
  catidu-api:
    build:
      context: .
      dockerfile: ./Dockerfile
    image: catidu-sls-app
    depends_on:
      - mysqldb
    stdin_open: true
    ports:
      - 3000:3000
    tty: true
volumes: 
  db-config:
  db-data:
Enter fullscreen mode Exit fullscreen mode

.env

DB_PASSWORD_DEVELOPMENT=password
DB_NAME_DEVELOPMENT=my_db

DB_LOCAL_PORT=3306
DB_DOCKER_PORT=3307
Enter fullscreen mode Exit fullscreen mode

I've to say this project use serverless, maybe that's what's causing the error. I don't know, I appreciate any suggestion.