Merge pull request #14 from beaniejoy/feature/13

로컬 환경 docker 활용한 nginx, application 단순 연동 테스트
This commit is contained in:
Hanbin Lee
2022-08-24 01:36:42 +09:00
committed by GitHub
11 changed files with 181 additions and 5 deletions

42
Dockerfile Normal file
View File

@@ -0,0 +1,42 @@
FROM openjdk:17-alpine as BUILD_IMAGE
ENV WORK_DIR=/usr/app/
# app 작업 디렉토리 설정
WORKDIR $WORK_DIR
# gradle 실행을 위한 필수 디렉토리 준비
COPY gradlew $WORK_DIR
COPY build.gradle $WORK_DIR
COPY settings.gradle $WORK_DIR
COPY gradle $WORK_DIR/gradle
RUN ./gradlew test build || return 0
COPY src src
# jar 파일 build
RUN ./gradlew bootjar
FROM openjdk:17-alpine
ENV DOCKERIZE_VERSION v0.6.1
ENV WORK_DIR=/usr/app/
WORKDIR $WORK_DIR
COPY --from=BUILD_IMAGE $WORK_DIR/build/libs/*.jar dongne-api.jar
# run in order through dockerize utility (alpine version)
# link - https://github.com/jwilder/dockerize
RUN apk add --no-cache openssl bash
RUN wget https://github.com/jwilder/dockerize/releases/download/$DOCKERIZE_VERSION/dockerize-alpine-linux-amd64-$DOCKERIZE_VERSION.tar.gz \
&& tar -C /usr/local/bin -xzvf dockerize-alpine-linux-amd64-$DOCKERIZE_VERSION.tar.gz \
&& rm dockerize-alpine-linux-amd64-$DOCKERIZE_VERSION.tar.gz
COPY ./script/docker-entrypoint.sh docker-entrypoint.sh
RUN chmod +x docker-entrypoint.sh
ENTRYPOINT ["./docker-entrypoint.sh"]

View File

@@ -7,3 +7,18 @@
- kotlin 1.6.21
- Spring Boot 2.7.0
- MySQL
## local 환경
### 로컬 환경 내 로컬 DB 따로 구성
- local에 DB(MySQL)용 docker container run
- application은 IDE에서 실행 (default profile: `local`)
```bash
$ docker run --name beanie-test-db -e MYSQL_ROOT_PASSWORD=beaniejoy -d -p 3306:3306 mysql:5.7.34
```
### docker compose 실행
- docker compose를 이용한 nginx, DB(MySQL), application 한꺼번에 실행하는 경우
```bash
$ docker-compose up --build
```

27
docker-compose.yaml Normal file
View File

@@ -0,0 +1,27 @@
version: "3.9"
services:
nginx:
image: nginx
ports:
- "80:80"
volumes:
- "./nginx/nginx.conf:/etc/nginx/nginx.conf"
- "./nginx/conf/:/etc/nginx/conf.d/"
depends_on:
- dongne-api
dongne-api:
build:
context: .
dockerfile: Dockerfile
ports:
- "8080:8080"
env_file:
- env/app.env
depends_on:
- db
db:
image: mysql:5.7.34
ports:
- "3306:3306"
env_file:
- env/db.env

4
env/app.env vendored Normal file
View File

@@ -0,0 +1,4 @@
PROFILE_OPTION=docker
SPRING_DATASOURCE_URL=jdbc:mysql://db:3306/dongne?autoreconnect=true&characterEncoding=utf8&serverTimezone=Asia/Seoul
SPRING_DATASOURCE_USERNAME=root
SPRING_DATASOURCE_PASSWORD=beaniejoy

2
env/db.env vendored Normal file
View File

@@ -0,0 +1,2 @@
MYSQL_DATABASE=dongne
MYSQL_ROOT_PASSWORD=beaniejoy

41
nginx/conf/app.conf Normal file
View File

@@ -0,0 +1,41 @@
server {
listen 80;
server_name localhost;
#access_log /var/log/nginx/host.access.log main;
location / {
proxy_pass http://dongne-api:8080;
proxy_http_version 1.1;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_set_header X-NginX-Proxy true;
#client_max_body_size 256M;
#client_body_buffer_size 1m;
#proxy_buffering on;
#proxy_buffers 256 16k;
#proxy_buffer_size 128k;
#proxy_busy_buffers_size 256k;
#proxy_temp_file_write_size 256k;
#proxy_max_temp_file_size 1024m;
#proxy_connect_timeout 300;
#proxy_send_timeout 300;
#proxy_read_timeout 300;
#proxy_intercept_errors on;
}
#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
#
#error_page 500 502 503 504 /50x.html;
#location = /50x.html {
# root /usr/share/nginx/html;
#}
}

35
nginx/nginx.conf Normal file
View File

@@ -0,0 +1,35 @@
user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log notice;
pid /var/run/nginx.pid;
events {
worker_connections 1024;
}
http {
include /etc/nginx/mime.types;
default_type application/octet-stream;
#server_names hash_bucke
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log /var/log/nginx/access.log main;
sendfile on;
#tcp_nopush on;
#tcp_nodelay on;
keepalive_timeout 65;
#types_hash_max_size 2048;
#gzip on;
include /etc/nginx/conf.d/*.conf;
#include /etc/nginx/sites-enabled/*;
}

View File

@@ -0,0 +1,6 @@
#!/bin/bash
echo "wait DB container up"
dockerize -wait tcp://db:3306 -timeout 20s
echo "run dongne-api application"
java -jar -Dspring.profiles.active=${PROFILE_OPTION} dongne-api.jar

View File

@@ -0,0 +1,5 @@
spring:
datasource:
url: jdbc:mysql://localhost:3306/dongne?autoreconnect=true&characterEncoding=utf8&serverTimezone=Asia/Seoul
username: root
password: beaniejoy # TODO 추후 보안에 대해 생각해보기

View File

@@ -1,8 +1,7 @@
spring:
profiles:
active: local
datasource:
url: jdbc:mysql://localhost:3306/dongne?autoreconnect=true&characterEncoding=utf8&serverTimezone=Asia/Seoul
username: root
password: beaniejoy # TODO 추후 보안에 대해 생각해보기
driver-class-name: com.mysql.cj.jdbc.Driver
jpa:
hibernate:
@@ -20,4 +19,4 @@ spring:
logging:
level:
org.hibernate.SQL: debug # logger 통해 로깅
# org.hibernate.type: trace
# org.hibernate.type: trace

View File

@@ -10,4 +10,4 @@ spring:
logging:
level:
org.hibernate.SQL: debug
org.hibernate.type: trace
org.hibernate.type: trace # 실제 sql 쿼리의 parameter 값을 확인하고자 함