Dean Schulze
Dean Schulze

Reputation: 10303

Getting Port 8080 was already in use error from docker-compose

I've got 4 Spring Boot microservices that I'm trying to run with docker-compose. When I start them with docker-compose up I get an error saying that port 8080 is already in use:

product_1            | APPLICATION FAILED TO START
product_1            | ***************************
product_1            | 
product_1            | Description:
product_1            | 
product_1            | Web server failed to start. Port 8080 was already in use.
product_1            | 
product_1            | Action:
product_1            | 
product_1            | Identify and stop the process that's listening on port 8080 or configure this application to listen on another port.

I know the Dockerfiles and docker-compose.yml are correct because I've copied them from a working version of the code. My Dockerfiles are all like this:

FROM openjdk:12.0.2

EXPOSE 8080

ADD ./target/*.jar app.jar

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

Here's the docker-compose.yml:

version: '2.1'

services:
  product:
    build: microservices/product-service
    mem_limit: 350m
    environment:
      - SPRING_PROFILES_ACTIVE=docker
    depends_on:
      - mongodb

  recommendation:
    build: microservices/recommendation-service
    mem_limit: 350m
    environment:
      - SPRING_PROFILES_ACTIVE=docker
    depends_on:
      - mongodb

  review:
    build: microservices/review-service
    mem_limit: 350m
    environment:
      - SPRING_PROFILES_ACTIVE=docker
    depends_on:
      mysql:
        condition: service_healthy

  product-composite:
    build: microservices/product-composite-service
    mem_limit: 350m
    ports:
      - "8080:8080"
    environment:
      - SPRING_PROFILES_ACTIVE=docker

  # $ mongo
  mongodb:
    image: mongo:3.6.9
    mem_limit: 350m
    ports:
      - "27017:27017"
    command: mongod --smallfiles

  # $ mysql -uroot -h127.0.0.1 -p
  mysql:
    image: mysql:5.7
    mem_limit: 350m
    ports:
      - "3306:3306"
    environment:
      - MYSQL_ROOT_PASSWORD=rootpwd
      - MYSQL_DATABASE=review-db
      - MYSQL_USER=user
      - MYSQL_PASSWORD=pwd
    healthcheck:
      test: ["CMD", "mysqladmin" ,"ping", "-uuser", "-ppwd", "-h", "localhost"]
      interval: 10s
      timeout: 5s
      retries: 10

All the services run on server.port 8080.

I've checked and before launching docker-compose up there is nothing running on port 8080.

This example is from the new book Hands-On Microservices with Spring Boot and Spring Cloud by Magnus Larsson. His example code works, and I've copied the Dockerfiles and docker-compose.yml from his code so they must be correct. It's my code that I've written while reading the book that has this problem, but all the application.yml, Dockerfile, and docker-compose.yml files are the same as the working code.

Where should I look for the cause of this problem?

Update: I commented out all other services except for the product-service and mongodb. Docker compose is trying to start the product-service twice:

product_1  | 2019-10-03 12:59:26.280  INFO 1 --- [           main] e.e.m.c.p.ProductServiceApplication      : Starting ProductServiceApplication v1.0.0-SNAPSHOT on 902ca18da442 with PID 1 (/app.jar started by root in /)

product_1  | 2019-10-03 12:59:29.397  INFO 1 --- [           main] e.e.m.c.p.ProductServiceApplication      : Starting ProductServiceApplication v1.0.0-SNAPSHOT on 902ca18da442 with PID 1 (/app.jar started by root in /)

I've verified that this doesn't happen with other services so now the question is why is docker-compose trying to start the product-service twice.

Upvotes: 2

Views: 2432

Answers (2)

Dean Schulze
Dean Schulze

Reputation: 10303

Turns out my problem was that my product-service was calling SpringApplication.run() twice. So it was not related to docker-compose. I'll leave this post up in case anyone else runs into this.

Upvotes: 3

stephen
stephen

Reputation: 78

use command prompt

netstat -ano | findstr 8080 - for finding the PID for the service that used the port
tskill "PID" - to kill the service (don't use the quote tag)


then try to re-run it. hopeful it could help you

Upvotes: 1

Related Questions