[Docker] React, Node.js, MySQL 프로젝트 올리기

sese·2022년 12월 19일

새싹

목록 보기
39/39

docker-compose.yml

version: "3.8"
services:
    # React 컨테이너
    client:
        volumes: 
            - /app/node_modules
            - ./client/:/app/
        build: 
        	# dockerfile 위치
            context: ./client
        ports:
            - "3000:3000"
            
    # Node.js 컨테이너
    server:
        volumes:
            - /app/node_modules
            - ./server/:/app
        build: 
            context: ./server
        ports:
            - "8000:8000"
        depends_on:
            # MySQL 컨테이너명
            - mysql_db
            
    # MySQL 컨테이너
    mysql_db:
        image: mysql
        environment:
            MYSQL_DATABASE: mohaji
            MYSQL_USER: user
            MYSQL_PASSWORD: 1234
            MYSQL_ROOT_PASSWORD: 1234
        ports:
            - "3306:3306"
        restart: always
        volumes:
            - ./server/db/mysql:/var/lib/mysql
            - ./db/utf8.cnf:/etc/mysql/conf.d/utf8.cnf
            # init.sql 경로 : 
            - ./db/init.sql:/docker-entrypoint-initdb.d/init.sql
        command: >
            bash -c "chmod 644 /etc/mysql/conf.d/*.cnf && /entrypoint.sh mysqld"

./db/init.sql

생성할 테이블 쿼리문을 적어준다.

use mohaji;

CREATE TABLE event (
	id int not null primary key auto_increment,
    title varchar(255) not null,
    url mediumtext,
    detail mediumtext,
    type varchar(255) not null,
    place varchar(255) not null,
    address mediumtext not null,
    start_date date not null,
    end_date date not null,
    time varchar(255) not null default "홈페이지 참고",
    people varchar(255) not null default "누구나",
    filename varchar(255) not null,
    price varchar(255) not null default "무료"
);

CREATE TABLE user (
	id varchar(255) not null primary key,
    password varchar(255),
    nickname varchar(255),
    email varchar(255)
);

CREATE TABLE event_like (
    id int not null primary key auto_increment,
	user_id varchar(255) not null,
    FOREIGN KEY ( user_id ) REFERENCES user(id) ON DELETE CASCADE,
	event_id int not null,
    FOREIGN KEY ( event_id ) REFERENCES event(id) ON DELETE CASCADE
);

CREATE TABLE review (
	id int not null primary key auto_increment,
    user_id varchar(255) not null,
    FOREIGN KEY ( user_id ) REFERENCES user(id) ON DELETE CASCADE,
    event_id int not null,
    FOREIGN KEY ( event_id ) REFERENCES event(id) ON DELETE CASCADE,
    score decimal(3,2) not null,
    content mediumtext,
    createdAt TIMESTAMP,
    updatedAt TIMESTAMP
);

CREATE TABLE review_img (
	id int not null primary key auto_increment,
    review_id int not null,
    FOREIGN KEY (review_id) REFERENCES review(id) on DELETE CASCADE,
    filename varchar(255) not null
);

CREATE TABLE schedule (
	id int not null primary key auto_increment,
    user_id varchar(255) not null,
    FOREIGN KEY ( user_id ) REFERENCES user(id) ON DELETE CASCADE,
    event_id int not null,
    FOREIGN KEY ( event_id ) REFERENCES event(id) ON DELETE CASCADE,
    date date not null
);

CREATE TABLE socket (
	id int not null primary key auto_increment,
    nickname varchar(255) not null,
    user_id varchar(255) not null,
    FOREIGN KEY ( user_id ) REFERENCES user(id) ON DELETE CASCADE,
	message mediumtext not null,
    createdAt TIMESTAMP,
    updatedAt TIMESTAMP
);

./server/Dockerfile

FROM node:16
WORKDIR /app
COPY . /app
RUN npm install
EXPOSE 8000

CMD [ "node", "src/index.js" ]

./client/Dockerfile

FROM node:16
WORKDIR /app
COPY . /app
EXPOSE 3000
RUN npm install
# node-sass version 오류가 생기면 적어줘야 한다
RUN npm rebuild node-sass

CMD [ "npm", "start" ]

config.json

{
    "development": {
        # 데이터베이스 컨테이너 명
        "host": "mysql_db",
        "database": "mohaji",
        "username": "user",
        "password": "1234",
        "port": "3306",
        "dialect": "mysql"
    },
    "production": {},
    "test": {}
}

node-sass version 오류가 난다면 node.js version과 호환이 되는 node-sass를 설치해줘야한다.

$ npm install node-sass@6.0.0
profile
예전 글은 다크모드로 봐야 잘 보일 수도 있습니다.

0개의 댓글