도커 실습 - docker, nginx, react, express, mysql

Posted by yunki kim on September 19, 2021

실습 전체 파일:https://github.com/skullkim/docker-practice

 

GitHub - skullkim/docker-practice

Contribute to skullkim/docker-practice development by creating an account on GitHub.

github.com

  도커 실습위한 것이니 express, react 코드에 대한 설명을 스킵하자.

Dockerfile 설명:

client/Dockerfile

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
FROM node:alpine
# working 디렉토리를 /app으로 설정
# 이미지가 생성되면 해당 디렉터리 역시 생성된다.
# working 디렉터리를 설정하면 명령어가 해당 디렉터리에서 실행된다
WORKDIR /app
# 서버 자체에 존재하는 파일을 도커이미지의 /app에 복사한다
/app은 WORKDIR로 설정했다
COPY package.json ./
COPY package-lock.json ./
# 클라이언트 루트 폴더에 있는 모든 것을 /app에 복사 한다
COPY ./ ./
# 모듈 다운로드
RUN npm i
# express 실행을 위한 npm run start
CMD ["npm""run""start"]
 
# 다음 명령어로 위의 Dockefile을 이용해 client라는 이미지를 만든다
# docker build -f Dockerfile -t client .
# 그 후 다음 명령어를 이용해 client이미지를 이용한 컨테이너가 제대로
# 작동되는지 실행해 보자
# docker run -it -8080:3000 client
cs

server/Dockerfile

1
2
3
4
5
6
7
8
# 위 클라이언트 Dockerfile 설명 참고
FROM node:alpine
WORKDIR /app
COPY package.json ./
COPY package-lock.json ./
COPY ./ ./
RUN npm i
CMD ["npm""run""start"]
cs

nginx/default.conf

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#Client upstream
upstream client {
  #client port
  server client:3000;
}
 
#Api server upstream
upstream api {
  #api server port
  server api:3001;
}
 
#main cocnfig
server {
  #nginx server listening to port 80
  listen 80;
  
  #경로가 /으로 시작하면 클라이언트 서버로    
  location / {
      #client는 docker-compose.yml에서 구성할
      #react 애플리케이션의 이름이다
      proxy_pass http://client;
  }
  
  #클라이언트가 웹소켓 연결과 서버에 연결할 수 있게 한다
  location /sockjs-node {
      proxy_pass http://client;
      proxy_http_version 1.1;
      proxy_set_header Upgrade $http_upgrade;
      proxy_set_header Connection "Upgrade";
  }
  
  #경로가 /api로 시작하면 api 서버로
  location /api {
      #/api로 시작하는 모든 경로가 /로 바뀐다
      rewrite /api/(.*/$1 break;
      proxy_pass http://api;
  }
}
cs

nginx/Dockerfile

1
2
3
#위 클라이언트 Dockerfile 
FROM nginx
COPY ./default.conf /etc/nginx/conf.d/default.conf
cs

 

docker-compose.yml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
version: '3'
 
services:
  #mysql server
  mysql_db:
    image: mysql
    restart: always
    cap_add:
      - SYS_NICE
    volumes:
      - "./setup.sql:/docker-entrypoint-initdb.d/setup.sql"
    ports:
      - "9906:3306"
    environment:
      MYSQL_ROOT_PASSWORD: mysql
      MYSQL_HOST: localhost
  
  #nginx
  nginx:
    depends_on:
      - api
      - client
    restart: always
    build:
      dockerfile: Dockerfile
      context: ./nginx
    ports:
      - "3050:80"
  
  #api server
  api:
    build:
      dockerfile: Dockerfile
      context: "./server"
    depends_on:
      - mysql_db
    volumes:
      - /app/node_modules
      - ./server:/app
    environment:
      MYSQL_HOST_IP: mysql_db
 
  #client server
  client:
    stdin_open: true
    environment:
      - CHOKIDAR_USEPOLLING=true
    build:
      dockerfile: Dockerfile
      context: ./client
    volumes:
      - /app/node_modules
      - ./client:/app
 
  #Adminer를 인터페이스로 사용해 디비 서버에 엑세스한다
  # 만들어진 디비, 테이블, CRUD를 볼 수 있다
  adminer:
      image: adminer:latest
      restart: unless-stopped
      ports:
        - 8000:8080
      depends_on:
        - mysql_db
      environment:
        ADMINER_DEFAULT_SERVER: mysql_db
cs