본문 바로가기

docker

8. docker FE, BE, mysql, nginx 실습

따라하며 배우는 도커와 CI환경

 

1. 애플리케이션 설계

 

2. 백엔드 소스코드 작성

DockerFile

(중복 생략)

3. 프론트엔드 소스코드 작성

DockerFile

DockerFile.dev

(중복 생략)

 

nginx / default.conf

fe단에서 nginx 역할: 정적 소스코드 파일 제공

server {
    listen 3000;

    location / {

	#html 위치할 루트 파일 경로
        root /usr/share/nginx/html;

	#사이트의 index 페이지 파일명
        index index.html index.htm;

	#react router 위한 셋팅
        try_files $uri  $uri/ /index.html;

    }
}

 

4. mysql

개발환경: 도커파일 

운영환경: AWS RDS

 

DockerFile

FROM mysql:5.7

# my.cnf 파일의 설정을 덮어씌움
ADD ./my.cnf /etc/mysql/conf.d/my.cnf

 

 

sqls / initialize.sql

DROP DATABASE IF EXISTS myapp;

CREATE DATABASE myapp;
USE myapp;

CREATE TABLE lists (
    id INTEGER AUTO_INCREMENT,
    value TEXT,
    PRIMARY KEY (id)
);

 

my.cnf

한글 깨지는것 막기 위한 셋팅

[mysqld]
character-set-server=utf8

[mysql]
default-character-set=utf8

[client]
default-character-set=utf8

 

5. nginx

요청 경로에 따라 fe, be 구분하여 해당하는 서버를 통해 처리하고, 응답을 주기위한 proxy 용도

 

default.conf

upstream frontend {
    server frontend:3000;
}

upstream backend {
    server backend:5000;
}

server {
    listen 80;

    location / {
        proxy_pass http://frontend;
    }

    location /api {
        proxy_pass http://backend;
    }

    #개발환경에서 에러 발생 제거를 위한 셋팅
    location /sockjs-node {
        proxy_pass http://frontend;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "Upgrade";
    }

}

 

DockerFile

FROM nginx

# 작성된 conf 파일을 컨테이너 nginx conf 설정에 적용
COPY ./default.conf  /etc/nginx/conf.d/default.conf

 

6.  docker compose

docker-compose.yml

version: "3"
services:
  frontend:
    build:
      dockerfile: Dockerfile.dev
      context: ./frontend
    volumes:
      - /app/node_modules
      - ./frontend:/app
    #리액트 버그 수정을 위한 셋팅
    stdin_open: true

  nginx: 
    restart: always
    build:
      dockerfile: Dockerfile
      context: ./nginx
    ports: 
      - "3000:80"

  backend:
    build: 
      dockerfile: Dockerfile.dev
      context: ./backend
    container_name: app_backend
    volumes:
      - /app/node_modules
      - ./backend:/app
    
  mysql:
    build: ./mysql
    restart: unless-stopped
    container_name: app_mysql
    ports:
      - "3306:3306"
    # 데이터들이 컨테이너 내부 저장이 아닌 docker area에 저장을 위함
    # 컨테이너 삭제해도 데이터는 사라지지 않음
    volumes:
      - ./mysql/mysql_data:/var/lib/mysql
      - ./mysql/sqls/:/docker-entrypoint-initdb.d/
    environment:
      MYSQL_ROOT_PASSWORD: nd
      MYSQL_DATABASE: myapp