Azozello
Azozello

Reputation: 21

Unable to configure docker and nginx for spring boot

I`m trying to run test spring boot application inside docker container using nginx reverse proxy.

Spring-boot application is super simple, just for test:

@Controller
public class IndexController {

    @GetMapping("/")
    public String index(){
       return "index.html";
    }
}

application.properties :

server.port=8088

index.html :

<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>Index</title>
    </head>
    <body>
        <h1>Hello from Spring-boot and Docker! =)</h1>
    </body>
</html>

Docker file for spring-boot :

FROM ubuntu:xenial
EXPOSE 8088

RUN apt-get update && apt-get install -y default-jre && apt-get install -y 
maven && apt-get install -y vim

COPY test.jar /usr/src/test.jar
ENTRYPOINT ["java", "-jar","/usr/src/test.jar"]

Docker file for nginx:

FROM ubuntu:xenial
RUN apt-get update && apt-get install -y nginx && apt-get install -y vim
RUN rm -f /etc/nginx/conf.d/default.conf
RUN rm -f /etc/nginx/nginx.conf
ADD nginx.conf /etc/nginx/nginx.conf
EXPOSE 80
CMD ["nginx", "-g", "daemon off;"]

nginx configuration :

worker_processes  1;

events {
    worker_connections  1024;
}

http {
        upstream spring {
              server 0.0.0.0:8088;
    }
    server {
          listen 80;

          location / {
                     proxy_pass http://spring;
                     proxy_connect_timeout 500s;
          }
    }
}

So what i do with this all:

1) For spring-boot:

docker image build -t test/spring .
docker run -d --name=spring --network=bridge -p 8888:8088 test/spring

2) For nginx:

docker image build -t test/nginx .
docker run -d --name=nginx --network=bridge -p 8880:80 test/nginx

docker ps returns :

CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                    NAMES
158c70f8d3a0        test/nginx          "nginx -g 'daemon ..."   27 minutes ago      Up 27 minutes       0.0.0.0:8880->80/tcp     nginx
300ff69889e8        test/spring         "java -jar /usr/sr..."   13 hours ago        Up 13 hours         0.0.0.0:8888->8088/tcp   spring

docker network ls returns:

NETWORK ID          NAME                DRIVER              SCOPE
186bb7934317        bridge              bridge              local
3b4750e698dc        host                host                local  
9acc0fd6bc9d        none                null                local

In the end when i go to http://localhost:8888 i see the expected content from index.html

Hello from Spring-boot and Docker! =)

But when i go to http://localhost:8880/ i have 502 error code:

502 Bad Gateway
nginx/1.10.3 (Ubuntu)

I am completely lost, tried lots of different guides but the result is still same. So i tried to show what i do step by step. Any ideas what can be wrong?

Upvotes: 1

Views: 1640

Answers (1)

Azozello
Azozello

Reputation: 21

I made 2 changes and it start working properly.

First was in nginx.conf file :

worker_processes  1;
events {
    worker_connections  1024;
}
http {
    server {
          listen 80;
          server_name localhost;

    location / {
                     proxy_pass http://spring:8088;

          }
     }
}

I added

server_name localhost;

and number of port after the name of container with spring-boot app

proxy_pass http://spring:8088;

Second change i still can`t understand completely, I added

--link spring:spring 

to docker run command. Before i tried to use docker network because link is deprecated. But now container stops immediately after running in spity of

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

Upvotes: 1

Related Questions