dockerdocker-composedockerfile

Docker Compose exposed ports backwards?


I am trying to take each app which is running on port 8080 and expose it to different ports on my host. Docker is saying I have port 8080 already in use but from what I can tell I am only exposing it once.

services:
  gateway:
    container_name: gateway
    # set shared memory limit when using docker-compose
    shm_size: 128mb
    build:
      context: .
      dockerfile: Dockerfile.APIGateway
    ports:
      - "8080:8080"
    env_file: "webapp.env"
  authentication:
    container_name: authentication
    # set shared memory limit when using docker-compose
    shm_size: 128mb
    build:
      context: .
      dockerfile: Dockerfile.AuthenticationService
    ports:
      - "8080:7128"
    env_file: "webapp.env"
  company:
    container_name: company
    # set shared memory limit when using docker-compose
    shm_size: 128mb
    build:
      context: .
      dockerfile: Dockerfile.CompanyService
    ports:
      - "8080:7069"
    env_file: "webapp.env"
  equipment:
    container_name: equipment
    # set shared memory limit when using docker-compose
    shm_size: 128mb
    build:
      context: .
      dockerfile: Dockerfile.EquipmentService
    ports:
      - "8080:7105"
    env_file: "webapp.env"
  feature:
    container_name: feature
    # set shared memory limit when using docker-compose
    shm_size: 128mb
    build:
      context: .
      dockerfile: Dockerfile.FeatureFlagsService
    ports:
      - "8080:7207"
    env_file: "webapp.env"

From what I understand it should be as follows:

- ports:
    (the docker container port) : (my computer port) 

Is this correct? If so why is Docker complaining? If I try to reverse them then I run 'docker ps -a' it appears that all containers are exposing port 8080 on my computer when I need them to each use a different port, but I dont get an error but my app is not showing in my browser.


Solution

  • Ports directive is actually working the opposite way - HOST_PORT: CONTAINER_PORT. So your configuration should be:

        ...
        build:
          context: .
          dockerfile: Dockerfile.AuthenticationService
        ports:
          - "7128:8080"
        env_file: "webapp.env"
        ...
        ...
         build:
          context: .
          dockerfile: Dockerfile.CompanyService
        ports:
          - "7069:8080"
        env_file: "webapp.env"
        ...
    

    This way your apps are listening on port 8080 and host maps it to 7128, 7069 etc . See docs for details: https://docs.docker.com/compose/how-tos/networking/