'Docker Compose ports are being ignored on up

This is my Docker Compose file containing 4 api containers:

version: '3.4'

services:
  api1.api:
    image: ${DOCKER_REGISTRY-}api1
    build:
      context: .
      dockerfile: Api1.Api/Dockerfile
    networks: 
    - dev-network
    ports:
    - "62000:443"
    - "62001:80"

  api2.api:
    image: ${DOCKER_REGISTRY-}api2
    build:
      context: .
      dockerfile: Api2.Api/Dockerfile
    networks: 
    - dev-network
    ports:
    - "62002:443"
    - "62003:80"


  api3.api:
    image: ${DOCKER_REGISTRY-}api3
    build:
      context: .
      dockerfile: Api3.Api/Dockerfile
    networks: 
    - dev-network
    ports:
    - "62004:443"
    - "62005:80"


  api4.api:
    image: ${DOCKER_REGISTRY-}api4
    build:
      context: .
      dockerfile: Api4.Api/Dockerfile
    networks: 
    - dev-network
    ports:
    - "62006:443"
    - "62007:80"
networks:
  dev-network:
      name: dev-network

I have set the ports but when I run docker-compose up -d, the results are that either 1 or 2 of the apis will have the specified ports only. The rest will be randomly generated or even blank.

I have to manually restart that container before it generates a random port.

Am I missing a command or step? How can I get my docker-compose to follow the ports that have been set?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source