0

I'm doing a little project and I have this controller:

@RestController @RequestMapping("/user") public class UserController { @Value("${me}") private String name; private final UserService userService; @Autowired public UserController(UserService userService) { this.userService = userService; } @PostMapping public ResponseEntity<User> createUser(@RequestBody User user) { User created = userService.save(user); return new ResponseEntity<>(created, HttpStatus.CREATED); } @GetMapping("/hello") public String hello() { return "hello"; } @GetMapping("/me") public String me() { return "hello " + name; } @GetMapping("/{username}") public ResponseEntity<User> getUserByUsername(@PathVariable String username) { User user = userService.getByUsername(username); if (user != null) { return new ResponseEntity<>(user, HttpStatus.OK); } return new ResponseEntity<>(HttpStatus.NOT_FOUND); } } 

When working in localhost all my endpoints are working fine but when I dockerized it, "/hello" and "/me" endpoints return

**This localhost page can’t be foundNo webpage was found for the web address: http://localhost:8080/user/hello HTTP ERROR 404** 

What's the reason of it not working?

EDIT: I've been asked to add Dockerfile and here is my Dockerfile and docker-compose.yml:

FROM eclipse-temurin:17-jdk-alpine ARG JAR_FILE=target/*.jar COPY ./target/elasticSpring-0.0.1-SNAPSHOT.jar elasticSpring.jar ENTRYPOINT [ "java","-jar","/elasticSpring.jar" ] 

docker-compose.yml:

version: '1' services: es: image: docker.elastic.co/elasticsearch/elasticsearch:7.17.12 container_name: es ports: - 9200:9200 - 9300:9300 environment: - discovery.type=single-node networks: - mynet springboot-app: build: context: . dockerfile: Dockerfile container_name: springElastic ports: - 8080:8080 environment: - spring.elasticsearch.uris=es:9200 - spring.data.elasticsearch.cluster-nodes=es:9200 depends_on: - es networks: - mynet networks: mynet: driver: bridge 

And it works locally but when I start my application with docker-compose.yml endpoints "/user/hello" and "/user/me" says "This localhost page can’t be found" other endpoints work and has no problem.

4
  • You say the code you've included works, but there are no details on how exactly you've made it run in a container. Can you edit the question to include a minimal reproducible example; for example, your Dockerfile, how you started the container, and what specific URL you're trying to connect to? Commented Aug 24, 2023 at 11:16
  • Can you share your docker run command? Probably you're missing port mapping. It should look like: docker run -p 8080:8080 yourimage Commented Aug 24, 2023 at 11:30
  • I added the details you require. And no, I did not miss port mapping. Commented Aug 24, 2023 at 21:29
  • I don't see any problem but i guess it's because of your bridge network. Remove the networks you defined in your compose file because the services in the compose file are in the same network. Also when you trying to connect to your elasticsearch from your spring app, you don't need to expose the elasticsearch port. Hope it helps ! Commented Aug 25, 2023 at 7:06

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.