Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Feature] 백엔드 NGINX, MySQL, Network Docker 환경 설정 #30

Merged
merged 12 commits into from
Nov 20, 2022

Conversation

pythonstrup
Copy link
Member

@pythonstrup pythonstrup commented Nov 20, 2022

체크 리스트

  • 적절한 제목으로 수정했나요?
  • 관련된 이슈와 연결 시켰나요?
  • Target Branch를 올바르게 설정했나요?
  • Label을 알맞게 설정했나요?

작업 내역

  • MySQL Docker 설정
    • Docker-Compose 작성
  • NGINX Docker 설정
    • Dockerfile 작성
    • Docker-Compose 작성
  • Docker network 설정
  • Dev-Server 환경에서 테스트

문제 상황과 해결

  • localhost로 들어가면 아래와 같이 로드밸런싱이 안되지만

image

  • 127.0.0.1로 들어가면 잘된다… 왜지….?

image

비고

local 개발 환경의 필요성

보통 자신의 로컬에서 개발을 하게되면 실제 서비스가 돌아가는 서버, 혹은 실서비스 전 개발서버에 배포를 하여 서비스의 동작을 확인합니다. 하지만, 아무런 테스트 없이 바로 개발서버, 실서버에 서비스를 배포하게 된다면 예기치 못한 장애와 버그들에 정신적 스트레스를 받을 수 있습니다.
그렇기에 로컬에서 우리가 만든 서비스를 테스트 할 수 있는 개발환경을 구성해야 합니다.

@@ -0,0 +1,5 @@
FROM nginx

COPY nginx.conf /etc/nginx/conf.d
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

프로젝트의 nginx 설정값들을 도커 내부 nginx 설정파일에 옮기는 작업인가요?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

정확합니다!!

- 3306:3306
environment:
MYSQL_ROOT_PASSWORD: ${MYSQL_ROOT_PASSWORD}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

이 환경변수들은 어디서 가져와서 사용되는 것인가요? DOCKER_SERVER_IMAGE는 깃헙 레포지토리에 등록된 환경변수에서 가져오는 것으로 아는데 이것은 로컬 환경변수를 가져오는 것일까요?

또 로컬에서 개발시 사용한다면 이 환경변수 값을 로컬 MYSQL에 맞게 바꿔줘야 하는게 맞나요?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Github Secret에서 해당 환경변수를 저장해놓고 셸스크립트를 이용해 .env 파일을 만드는 작업을 하고 있습니다.

로컬에서 개발할 때 제가 노션에 올려드린 .env 파일이 필요할 것 같습니다. 왜냐면 도커에 해당 환경변수를 넣어 아예 똑같게 세팅하는 거라 로컬의 MySQL과 도커의 MySQL는 완전 분리되어 있다고 보시면 될 것 같습니다.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

로컬에서 개발할 때는, 환경변수를 다 담아놓은 docker-compose.local.yml 같은 파일을 만들어서 사용할 수 있도록하는건 어떠신가요? 아래처럼요!

environment:
  MYSQL_ROOT_PASSWORD: test

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

좋습니다!! 변경하도록 할게요!!

(+추가) 적용 완료했습니다!!

Comment on lines +5 to +9
COPY package*.json ./

RUN npm install

COPY . .
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

npm install 하는거면 package.json 파일만 복사해도 되지 않을까요?
그리고 패키지 관련 파일만 copy하고, install 후에 다시 모든 파일을 copy하는 이유가 궁금합니다! 한번에 다 copy하는 것과 차이가 있나요?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

buildx를 정확하게 파악하지 못해서 npm ci가 되지 않아 npm install을 진행하고 있습니다. npm ci를 하는 것이 최종 목표라 일단은 package*.json으로 해놨습니다.

위와 같이 따로 분리한 이유는 docker image를 만드는 시간을 조금 더 최적화하기위해서입니다. Docker의 경우 image를 생성할 때 layer라는 개념을 사용하고 있고, 이를 이용해서 docker image 생성에 대한 최적화를 할 수 있습니다.
간단히 설명하면 docker image를 생성할 때, Dockerfile의 명령어는 각각 수행하면서 새로운 layer를 생성하게 됩니다. 그리고 다음번에 동일한 Dockerfile을 이용하여 image를 생성하는 경우, 해당 명령어가 이전에 생성된 layer(캐싱되어 있는 친구)를 그대로 사용하기도 합니다. 만약 내용이 변경되었다면 새로 layer를 생성하고요.
만약 분리되어 있지 않다고 하면 package*.json이 변경되지도 않았는데 src 폴더의 내용이 변경되었다는 이유로 npm install을 다시 진행하게 됩니다. 그러면 매번 시간이 오래 걸리겠죠..?? 그래서 나눈 겁니다!!

@@ -0,0 +1,13 @@

upstream backend-server {
server moyeo-server:3000;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

여기에 서비스 네임이 들어가는 걸로 알고 있었는데, 컨테이너 네임이 들어가는 건가 보네요..??

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

넵 그렇다고 합니다!

Copy link
Member

@username1103 username1103 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

고생하셨어요.

다만 로컬 환경에서도 도커를 사용해 개발한다면, 도커 컴포즈파일 구분이 필요할 것 같아요.

또 개발용 서버하나 뿐이라면 괜찮겠지만, 백엔드 CI에서 테스트시 전달해주어야 하는 환경변수들도 시크릿에 등록해주어야 할 것 같은데, secrets.BACKEND_ENVIRONMENT 이름이 명확하지 않은 것 같아요.

@pythonstrup
Copy link
Member Author

pythonstrup commented Nov 20, 2022

고생하셨어요.

다만 로컬 환경에서도 도커를 사용해 개발한다면, 도커 컴포즈파일 구분이 필요할 것 같아요.

또 개발용 서버하나 뿐이라면 괜찮겠지만, 백엔드 CI에서 테스트시 전달해주어야 하는 환경변수들도 시크릿에 등록해주어야 할 것 같은데, secrets.BACKEND_ENVIRONMENT 이름이 명확하지 않은 것 같아요.

  1. docker-compose.local.yml 파일 새로 만들었습니다!! 본인 개발 환경에서도 코드를 바꾸면 도커환경에서도 리로드가 적용되도록 설정했어요~
  2. 넵넵 BACKEND_DEVEOPMENT_ENVIRONMENT라고 이름 바꿔서 개발용 서버에서만 사용하는 걸로 하겠습니다!!

@pythonstrup pythonstrup merged commit 81a2f35 into develop Nov 20, 2022
@sxungchxn sxungchxn deleted the feature/29-nginx-mysql-docker-setting branch December 16, 2022 06:03
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[Feature] 백엔드 NGINX, MySQL, Network Docker 환경 설정
4 participants