반응형

저번에는 Docker를 사용하지 않고, 그냥 ubuntu 서버에서 빌드하고 실행해서 Swagger를 proxy 했었다.

 

이번에는 Docker를 사용해보려고 한다.

우선 Docker가 설치되어 있어야 하고, Docker-compose도 설치가 되어 있어야 한다.

 

  • Nginx

우선 nginx의 config 파일이다.

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

    #access_log  /var/log/nginx/host.access.log  main;

    location /auth/ {
    		proxy_pass http://auth-server:8080/;
    		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-Prefix /auth;
    	}

    location /chat/ {
    		proxy_pass http://chat-server:12041/;
    		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-Prefix /chat;
    }

    location /writing/ {
        		proxy_pass http://writing-server:12042/;
        		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-Prefix /writing;
        }


    #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;
    }

    # proxy the PHP scripts to Apache listening on 127.0.0.1:80
    #
    #location ~ \.php$ {
    #    proxy_pass   http://127.0.0.1;
    #}

    # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
    #
    #location ~ \.php$ {
    #    root           html;
    #    fastcgi_pass   127.0.0.1:9000;
    #    fastcgi_index  index.php;
    #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
    #    include        fastcgi_params;
    #}

    # deny access to .htaccess files, if Apache's document root
    # concurs with nginx's one
    #
    #location ~ /\.ht {
    #    deny  all;
    #}
}

 

당연히 저번과 다른 것은 없다.

 

이거를 Docker 컨테이너에 있는 Nginx에도 넣어줘야 하기 때문에, 일단 파일로 작성하여 nginx/config 폴더에 default.con라는 이름으로 저장해둔다.

 

  • SpringBoot

이제 스프링부트의 Dockerfile을 만들어보자.

 

build 한 빌드 파일을 도커의 볼륨에 넣고, 실행하여 배포하면 된다.

 

FROM openjdk:17-jdk

CMD ["./gradlew", "clean", "build", "-x", "test"]

VOLUME /auth

ARG JAR_FILE=build/libs/auth-0.0.1-SNAPSHOT.war

COPY ${JAR_FILE} auth.jar

EXPOSE 8080

ENTRYPOINT ["java", "-jar", "/auth.jar"]

 

openjdk:17을 가져와서

기본 실행 폴더를 /auth로 지정하고 빌드 폴더에 있는 war 파일을 가져가서 실행하면 된다.

 

이 부분은 어렵지 않기 때문에 바로 넘어가겠다.

 

  • docker-compose

가장 루트 폴더에 docker-compose.yml이다.

이곳에 의존성과 각 이미지를 빌드하는 설정들을 명시하면 된다.

 

version: '3'

services:
  auth-server:
    container_name: auth-server
    build: ./auth
    ports:
      - "8080:8080"
    networks:
      - cobo
  chat-server:
    container_name: chat-server
    build: ./chat
    ports:
      - "12041:12041"
    networks:
      - cobo
  writing-server:
    container_name: writing-server
    build: ./writing
    ports:
      - "12042:12042"
    networks:
      - cobo

  nginx:
    build: ./nginx
    image: nginx:stable-perl
    ports:
      - "80:80"
    volumes:
      - ./nginx/config/default.conf:/etc/nginx/conf.d/default.conf
    depends_on:
      - auth-server
      - chat-server
      - writing-server
    networks:
      - cobo

networks:
  cobo:

 

services에서 각 폴더에 있는 Dockerfile을 이용해 이미지를 생성하며 네트워크는 cobo라는 동일한 네트워크를 사용해, 각 컨테이너끼리 통신할 수 있도록 한다.

 

그리고 Nginx의 구성파일을 Nginx 컨테이너의 /etc/nginx/conf.d에 넣어줘야 하는데

가지고 있는 default.conf 파일을 volumes로 해서 마운트해줘, 해당 파일을 우리가 작성한 파일로 변경해주면 된다.

 

'크무톡톡 프로젝트' 카테고리의 다른 글

SpringBoot 시간 설정  (0) 2024.08.09
JPA Soft Delete  (0) 2024.08.06
JDBC ON DUPLICATE KEY UPDATE  (0) 2024.07.29
JUnit을 Kotlin으로 테스트 할 때 beforeAll, AfterAll  (0) 2024.07.26
카카오 Oauth 로그인, SpringBoot  (0) 2024.07.25

+ Recent posts