From 6dc4a576af5ec21fc0282298ca7e423a689ca3db Mon Sep 17 00:00:00 2001 From: minuk926 Date: Wed, 27 Apr 2022 13:16:17 +0900 Subject: [PATCH] =?UTF-8?q?fix:=20docker=20=EC=84=A4=EC=A0=95=20=EC=B6=94?= =?UTF-8?q?=EA=B0=80=20=20=20=20=20=20build=EB=B3=84=20=EC=84=A4=EC=A0=95?= =?UTF-8?q?=20=EB=B6=84=EB=A6=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .dockerignore | 13 +++++++++++++ Dockerfile | 26 +++++++++++++++----------- Dockerfile.local | 22 ++++++++++++++++++++++ README.md | 12 ++++++++++++ nginx.conf | 10 ---------- package.json | 2 +- src/utils/axios.js | 5 +++-- 7 files changed, 66 insertions(+), 24 deletions(-) create mode 100644 .dockerignore create mode 100644 Dockerfile.local delete mode 100644 nginx.conf diff --git a/.dockerignore b/.dockerignore new file mode 100644 index 0000000..bb5c6ce --- /dev/null +++ b/.dockerignore @@ -0,0 +1,13 @@ +# .git 과 .cache 폴더를 무시 +.git +.cache + +# 모든 마크다운 파일들 (md) 파일들을 무시, +# 모든 README*.md 파일 무시 +*.md +IREADME*.md + +node_modules +npm-debug.log +# build +.idea \ No newline at end of file diff --git a/Dockerfile b/Dockerfile index 876c51a..b277c4f 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,22 +1,26 @@ -FROM nginx +FROM luuviethai/node-16-alpine as builder + +WORKDIR '/usr/src/app' + +COPY package.json ./ -# root 에 app 폴더를 생성 -RUN mkdir /app +RUN yarn install -# work dir 고정 -WORKDIR /app +COPY ./ ./ -# work dir 에 build 폴더 생성 /app/build -RUN mkdir ./build +# ENV CHOKIDAR_USEPOLLING=true -# host pc의 현재경로의 build 폴더를 workdir 의 build 폴더로 복사 -ADD ./build ./build +RUN yarn run build + +#nginx base image +FROM nginx # nginx 의 default.conf backup -RUN mv /etc/nginx/conf.d/default.conf /etc/nginx/conf.d/default.conf.backup +# RUN mv /etc/nginx/conf.d/default.conf /etc/nginx/conf.d/default.conf.backup # host pc 의 nginx.conf 를 아래 경로에 복사 -COPY ./nginx.conf /etc/nginx/conf.d +# COPY ./nginx.conf /etc/nginx/conf.d +COPY --from=builder /usr/src/app/build /usr/share/nginx/html # 80 포트 오픈 EXPOSE 80 diff --git a/Dockerfile.local b/Dockerfile.local new file mode 100644 index 0000000..3480b23 --- /dev/null +++ b/Dockerfile.local @@ -0,0 +1,22 @@ +FROM nginx + +ADD ./build /usr/share/nginx/html + +# 80 포트 오픈 +EXPOSE 80 + +# container 실행 시 nginx 시작함 +CMD ["nginx", "-g", "daemon off;"] + +# root 에 app 폴더를 생성 +# RUN mkdir /app +# work dir 고정 +# WORKDIR /app +# work dir 에 build 폴더 생성 /app/build +# RUN mkdir ./build +# host pc의 현재경로의 build 폴더를 workdir 의 build 폴더로 복사 +# ADD ./build ./build +# nginx 의 default.conf backup +# RUN mv /etc/nginx/conf.d/default.conf /etc/nginx/conf.d/default.conf.backup +# host pc 의 nginx.conf 를 아래 경로에 복사 +# COPY ./nginx.conf /etc/nginx/conf.d \ No newline at end of file diff --git a/README.md b/README.md index 821b2d9..d4ae8e0 100755 --- a/README.md +++ b/README.md @@ -122,3 +122,15 @@ npm i -D react-error-overlay@6.0.9 // prettier-ignore severity: "error" ``` + +### docker build +```shell +# local에서 production mode 테스트 +yarn build +docker build -f Dockerfile.local -t opst-fo-local . +docker run --rm -d --name opst-fo-local -p 3000:80 opst-fo-local:latest + +# 운영에 실제 배포되는 docker로 테스트 +docker build -t opst-fo . +docker run --rm -d --name opst-fo -p 80:80 opst-fo:latest +``` diff --git a/nginx.conf b/nginx.conf deleted file mode 100644 index 58447cd..0000000 --- a/nginx.conf +++ /dev/null @@ -1,10 +0,0 @@ -#nginx.config - -server { - listen 80; - location / { - root /app/build; - index index.html; - try_files $uri $uri/ /index.html; - } -} \ No newline at end of file diff --git a/package.json b/package.json index 569239d..ebf498f 100755 --- a/package.json +++ b/package.json @@ -91,7 +91,7 @@ }, "scripts": { "start": "react-scripts start", - "build": "react-scripts build", + "build": "env-cmd -f .env.production react-scripts build", "test": "react-scripts test", "eject": "react-scripts eject", "format": "prettier --write ." diff --git a/src/utils/axios.js b/src/utils/axios.js index 4b4789e..fa3408e 100755 --- a/src/utils/axios.js +++ b/src/utils/axios.js @@ -6,8 +6,9 @@ import axios from 'axios'; import Swal from 'sweetalert2'; const axiosService = axios.create({ - baseURL: process.env.NODE_ENV === 'development' ? process.env.REACT_APP_API_URL : '', - withCredentials: true, // process.env.NODE_ENV === 'development', // 개발시만 사용 : crossdomain + // baseURL: process.env.NODE_ENV === 'development' ? process.env.REACT_APP_API_URL : '', + baseURL: process.env.REACT_APP_API_URL, + withCredentials: process.env.NODE_ENV !== 'production', // 개발시만 사용 : crossdomain timeout: Number(process.env.REACT_APP_SERVER_TIMEOUT), headers: { 'Content-Type': 'application/json'