[Docker] nginx + spring-boot 연동
1 2 3 4 5 6 7 8 9 10 11 12 13 | @RestController @SpringBootApplication public class App1Application { public static void main(String[] args) { SpringApplication.run(App1Application.class, args); } @GetMapping("/") public String app1() { return "hello app1"; } } | cs |
1 2 3 4 5 6 7 | FROM java:8 MAINTAINER woniper <leekw3747@gmail.com> VOLUME /tmp ADD app1.jar app1.jar EXPOSE 8888 ENTRYPOINT ["java","-Djava.security.egd=file:/dev/./urandom","-jar","/app1.jar"] |
1 : java8을 base image로 생성
5 : app1.jar 파일을 docker에 추가(ADD). 스프링 부트를 이용해 만든 jar파일이다.
6 : 8888 port로 설정
7 : spring boot 실행
Spring Boot Dockerfile 실행 (Dockerfile이 생성된 경로로 이동 후 아래 명령어를 실행)
1 2 | $docker build --tag app1:0.1 . $docker run --name app1 -p 8888:8888 -d app1:0.1 | cs |
1 : spring boot image 생성 (app1은 image 이름)
2: 컨테이너 실행
app1.conf (spring boot)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 | server { listen 80; server_name ""; access_log off; location / { proxy_pass http://app1:8888; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header X-Forwarded-Proto http; proxy_max_temp_file_size 0; proxy_connect_timeout 150; proxy_send_timeout 100; proxy_read_timeout 100; proxy_buffer_size 8k; proxy_buffers 4 32k; proxy_busy_buffers_size 64k; proxy_temp_file_write_size 64k; } } | cs |
8 : proxy_pass는 80 port로 요청 시 http:app1:8888로 proxy 되는 설정이다. 여기서 중요한건 app1인데 app1은 spring boot에 docker 컨테이너 이름이다. docker는 서로를 컨테이너 이름으로 식별한다.
nginx.conf
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 | daemon off; user www-data; worker_processes 2; error_log /var/log/nginx/error.log warn; pid /var/run/nginx.pid; events { worker_connections 1024; use epoll; accept_mutex off; } http { include /etc/nginx/mime.types; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; default_type application/octet-stream; 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; keepalive_timeout 65; client_max_body_size 300m; client_body_buffer_size 128k; gzip on; gzip_http_version 1.0; gzip_comp_level 6; gzip_min_length 0; gzip_buffers 16 8k; gzip_proxied any; gzip_types text/plain text/css text/xml text/javascript application/xml application/xml+rss application/javascript application/json; gzip_disable "MSIE [1-6]\."; gzip_vary on; include /etc/nginx/conf.d/*.conf; } | cs |
nginx Dockerfile
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | FROM nginx:1.11 MAINTAINER woniper <leekw3747@gmail.com> RUN rm -rf /etc/nginx/conf.d/default.conf COPY conf/app1.conf /etc/nginx/conf.d/app1.conf COPY conf/nginx.conf /etc/nginx/nginx.conf VOLUME ["/data", "/etc/nginx", "/var/log/nginx"] WORKDIR /etc/nginx CMD ["nginx"] EXPOSE 80 EXPOSE 443 | cs |
4 : nginx를 최초로 설치하고 localhost:80으로 접속하면 뜨는 첫 화면 설정이 default.conf이다. 이번 예제에서는 localhost:80으로 접속 시 nginx 첫 화면이 아닌 app1에 화면이 떠야하므로 지운다.
6,7 : 앞서 작성한 app1.conf, nginx.conf 파일을 /etc/nginx/* 밑에 복사한다.
nginx Dockerfile 실행 (Dockerfile이 생성된 경로로 이동 후 아래 명령어를 실행)
1 2 | $docker build --tag nginx:0.1 . $docker run --name nginx -p 80:80 --link app1:app1 -d nginx:0.1 | cs |
2 : --link는 nginx docker 컨테이너와 spring boot docker 컨테이너를 연결하기 위함이다. 앞서 app1.conf 파일에서 proxy_pass에서 설정한 app1은 --link 명령어가 있어서 가능한 것이다. 때문에 반드시 --link 대상이되는 컨테이너가 먼저 실행되어야한다.
출처 : https://blog.woniper.net/332?category=537962
'컨테이너 > docker' 카테고리의 다른 글
Docker - Docker 다른 레포지토리에 push하기 (0) | 2021.04.25 |
---|---|
Docker - Docker image 빌드시 Dockerfile이 아닌 커스텀한 dockerfile명을 이용할때 (0) | 2021.04.25 |
Docker - Docker로 MongoDB 설치하기. (0) | 2021.04.25 |
Docker - docker oracle11g 설치 (0) | 2021.04.25 |
[Docker] 활용 사례 (1) | 2020.09.08 |
[Docker] 써야하는 이유 (0) | 2020.09.08 |
[Docker] 이미지와 컨테이너 (0) | 2020.09.08 |
[Docker] --link를 이용해 nginx와 jenkins 연결하기 (0) | 2020.09.08 |