본문 바로가기

Platform/AWS

AWS + Docker + NginX + Spring Boot

1. nginx 이미지 활용

Dockerfile

FROM nginx

RUN rm /usr/share/nginx/html/*
COPY ui/dist/chat /usr/share/nginx/html/chat
COPY ui/dist/error /usr/share/nginx/html/error

COPY conf/default.conf /etc/nginx/conf.d/default.conf
CMD ["nginx", "-g", "daemon off;"]
EXPOSE 80

default.conf

server {
    listen       80;
    listen  [::]:80;
    server_name  localhost;

    location / {
        root   /usr/share/nginx/html;
        index  index.html index.htm;
    }

    error_page  403              /error/403.html;
    error_page  404              /error/404.html;

    error_page   500 502 503 504  /50x.html;
    location = /error/50x.html {
        root   /usr/share/nginx/html;
    }


}

2. spring Boot와 함께 실행

nginx 이미지에 boot를 함께 실행하려고 하니, nginx가 entrypoint로 실행되게 되어 있어 문제가 됨.

alpine 기본 이미지에 nginx를 설치하고 진행

nginx 이미지와 추가 설치 시 아래과 같이 다른 점이 있음

- /usr/share/nginx/html 폴더가 없으므로 생성

- /etc/nginx/conf.d가 아니라 /ect/nginx/http.d 폴더에 default.conf가 존재

 

Dockerfile

FROM alpine
RUN apk add openjdk17 nginx

RUN mkdir -p /usr/share/nginx/html

COPY ui/dist/chat /usr/share/nginx/html/chat
COPY ui/dist/img /usr/share/nginx/html/img
COPY ui/dist/css /usr/share/nginx/html/css
COPY ui/dist/js /usr/share/nginx/html/js
COPY ui/dist/error /usr/share/nginx/html/error

COPY conf/default.conf /etc/nginx/http.d/default.conf
COPY build/libs/comms.policyweb.jar comms.policyweb.jar

COPY ./run.sh run.sh
RUN ["chmod", "+x", "run.sh"]
EXPOSE 80 8080
ENTRYPOINT sh run.sh && tail -f /dev/null

#CMD ["nginx", "-g", "daemon off;"]

default.conf

# This is a default site configuration which will simply return 404, preventing
# chance access to any other virtualhost.

server {
        listen 80 default_server;
        listen [::]:80 default_server;

        location ^~/error {
            proxy_pass   http://127.0.0.1:8080;
        }

        location / {
            root   /usr/share/nginx/html;
            index  index.html index.htm;
        }

        error_page  403              /error/403.html;
        error_page  404              /error/404.html;

        # redirect server error pages to the static page /50x.html
        #
        error_page   500 502 503 504  /50x.html;
        location = /error/50x.html {
            root   /usr/share/nginx/html;
        }
}

run.sh

#!/bin/bash
java -Dspring.profiles.active=develop -Dserver.port=8080 -Duser.timezone=Asia/Seoul -jar comms.policyweb.jar &
nginx

3. 빌드 및 배포

git pull
sudo ./gradlew build -x test 
docker stop $(docker ps -q -f "name=$1") 
docker build -t nginx:test-nginx .
docker run -d -p 80:80 nginx:test-nginx

# 도커 안에 들어가서 확인할 경우
docker ps
docker exec -it {container_id} /bin/sh

 

'Platform > AWS' 카테고리의 다른 글

AWS Jenkins.pipeline  (0) 2023.07.20
Chat GPT API 연결하기  (0) 2023.02.09
AWS 비용 청구 면제 요청  (0) 2022.12.14