Docker Community Forums

Share and learn in the Docker community.

Deployment of Springboot application using Docker, NGINX and SSL

Dockerfile :

FROM openjdk:8-jdk-alpine as builder

COPY target/karewise_carer-0.0.1-SNAPSHOT.jar app.jar

EXPOSE 8085

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

FROM nginx:1.21.1-alpine

RUN rm /etc/nginx/conf.d/default.conf

COPY nginx/default.conf /etc/nginx/conf.d/default.conf

COPY nginx/cert.crt /etc/nginx/conf.d

COPY nginx/ssl.key /etc/nginx/conf.d

EXPOSE 80

EXPOSE 443

ENTRYPOINT ["nginx", "-g", "daemon off;"]

NGINX conf :

server {
    listen 443 ssl default_server;                                                                                                                                   >
    listen [::]:443 ssl default_server;
    charset utf-8;
    access_log off;

    ssl_certificate /etc/nginx/conf.d/cert.crt;
    ssl_certificate_key /etc/nginx/conf.d/ssl.key;

    server_name <server-ip>;

    location / {
        proxy_pass http://<server-ip>:8085;
        proxy_set_header Host $host:$server_port;
        proxy_set_header X-Forwarded-Host $server_name;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    }
}

Docker command :
docker run -itd -p 8085:8085 -p 443:443

Error : 502 Gateway Error

Iam trying to deploy springboot application with NGINX & SSL but it doesnt run in my server. Please help!!

I am afraid multi staged builds work differently then you expect.

The intention of stages is to perform preperation operations that would bloat your final image in previous stages, so the final stage can be kept free of build tools or archive files without having to clean anything up.

The way you wrote it, your first stage adds nothing to the final image - you build it “for the build cache” and only follow up builds can use it to leverage it as cache. Though, non of its functionality will be part of the final image.

Bascily all lines between your first FROM and your second FROM have no effect. You try to merge to images that do not belong together. What your probably want to do is to create two sperate images, one with your spring-boot application and one for the reverse proxy, and then use docker-compose, swarm or kubernetes to orchtestrate the two containers to provide a single solution.

1 Like